jruby e dsl

30
JRuby e DSLs Luca Guidi - Sourcesense Java Day Roma - 1 Dic 2007

Upload: jodosha

Post on 28-May-2015

2.109 views

Category:

Technology


0 download

DESCRIPTION

Panoramica sui DSL e sul loro utilizzo con i linguaggi dinamici.

TRANSCRIPT

Page 1: JRuby e DSL

JRuby e DSLsLuca Guidi - Sourcesense

Java Day Roma - 1 Dic 2007

Page 2: JRuby e DSL

DSLs cosa sono?

Page 3: JRuby e DSL

Domain Specific Language

Page 4: JRuby e DSL

DSLs

•single purpose

•singolo dominio

Page 5: JRuby e DSL

Linguaggi di programmazione

•general purpose

•multi dominio

Page 6: JRuby e DSL

YACC (1975)Stephen C. Johnson

Page 7: JRuby e DSL

UNIX scripting

Page 8: JRuby e DSL

ls | grep dsl >> ~/list.txt

Page 9: JRuby e DSL

favoriscono le ontologie

Page 10: JRuby e DSL

Ontologia: rappresentazione strutturata delle informazioni espressa tramite

gerarchie e relazioni tra classi.

Page 11: JRuby e DSL

Behavior Driven Development

Page 12: JRuby e DSL

it "tracks implicit observable models" do instance = FooObserver.new instance.send(:observed_classes).should include(Foo) instance.send(:observed_classes).should_not include(ObservedModel)end

Page 13: JRuby e DSL

should

Page 14: JRuby e DSL

Secondo Martin Fowleri DSL sono:

•Esterni (XML)

•Interni (Smalltalk, Ruby)

Page 15: JRuby e DSL

Java

•Non dinamico

•Strongly typed

Page 16: JRuby e DSL

<?xml version="1.0" encoding="UTF-8"?><book> <attribute id="title"> <required>true</required> <length> <minimum>1</minimum> <maximum>23</maximum> </length> </attribute></book>

Page 17: JRuby e DSL

Annotations

Page 18: JRuby e DSL

import java.lang.reflect.*;

public class Book { @Size(min=1, max=23) protected String title; public Book (String title) { setTitle(title); } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public boolean isValid() throws NoSuchFieldException, SecurityException { Size size = (Size) this.getClass().getDeclaredField("title") .getAnnotation(Size.class); return ( title != null && ( title.length() >= size.min() || title.length() <= size.max() ) ); }}

Page 19: JRuby e DSL

import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.FIELD)public @interface Size { public int max() default 0; public int min() default 0;}

Page 20: JRuby e DSL

module Validations def validates_presence_of(*attributes) # ... end def validates_size_of(*attributes) # ... endend

class Book extend Validations validates_presence_of :title validates_size_of :title, 1..23end

Page 21: JRuby e DSL

Nella mia esperienza,la cosa più affascinante è che i programmi Ruby ben scritti sono già un DSL, a causa della sintassi

del linguaggio.

Jamis Buck - 37 signals

Page 22: JRuby e DSL

Ruby

• Symbol

• Block

• Module

• Splat

• eval, instance_eval, class_eval

• define_method, alias_method

Page 23: JRuby e DSL

Ruby DSLs

Page 24: JRuby e DSL

Method Class

class Carrot < ActiveRecord::Base belongs_to :bunnyend

class Bunny < ActiveRecord::Base has_many :carrotsend

Page 25: JRuby e DSL

Top Levelnamespace :db do desc "Create a database connection." task :connect do # ... end desc "Prepare the database for tests." task :prepare => :connect do # ... endend

Page 26: JRuby e DSL

Sandbox

ActiveRecord::Schema.define do create_table :bunnies do |t| t.column :name, :string, :null => false endend

ActiveRecord::Schema.define do drop_table :bunniesend

Page 27: JRuby e DSL

Piattaforma Java

Page 28: JRuby e DSL

Java => JRubyrequire 'java'include_class 'java.util.Random'

class JavaRandom def self.next @@r ||= Random.new @@r.nextInt endend

JavaRandom.next # => 23

Page 29: JRuby e DSL

JRuby => Java

import org.jruby.*;

public class UsingRuby { public static void main(String[] args) { Ruby runtime = Ruby.getDefaultInstance(); runtime.evalScript("puts 1 + 2"); }}