JRuby e DSL

2,461 views

Published on

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

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,461
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
14
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

JRuby e DSL

  1. 1. JRuby e DSLs Luca Guidi - Sourcesense Java Day Roma - 1 Dic 2007
  2. 2. DSLs cosa sono?
  3. 3. Domain Specific Language
  4. 4. DSLs • single purpose • singolo dominio
  5. 5. Linguaggi di programmazione • general purpose • multi dominio
  6. 6. YACC (1975) Stephen C. Johnson
  7. 7. UNIX scripting
  8. 8. ls | grep dsl >> ~/list.txt
  9. 9. favoriscono le ontologie
  10. 10. Ontologia: rappresentazione strutturata delle informazioni espressa tramite gerarchie e relazioni tra classi.
  11. 11. Behavior Driven Development
  12. 12. it quot;tracks implicit observable modelsquot; do instance = FooObserver.new instance.send(:observed_classes).should include(Foo) instance.send(:observed_classes).should_not include(ObservedModel) end
  13. 13. should
  14. 14. Secondo Martin Fowler i DSL sono: • Esterni (XML) • Interni (Smalltalk, Ruby)
  15. 15. Java • Non dinamico • Strongly typed
  16. 16. <?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?> <book> <attribute id=quot;titlequot;> <required>true</required> <length> <minimum>1</minimum> <maximum>23</maximum> </length> </attribute> </book>
  17. 17. Annotations
  18. 18. 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(quot;titlequot;) .getAnnotation(Size.class); return ( title != null && ( title.length() >= size.min() || title.length() <= size.max() ) ); } }
  19. 19. 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; }
  20. 20. module Validations def validates_presence_of(*attributes) # ... end def validates_size_of(*attributes) # ... end end class Book extend Validations validates_presence_of :title validates_size_of :title, 1..23 end
  21. 21. 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
  22. 22. Ruby • Symbol • Block • Module • Splat • eval, instance_eval, class_eval • define_method, alias_method
  23. 23. Ruby DSLs
  24. 24. Method Class class Carrot < ActiveRecord::Base belongs_to :bunny end class Bunny < ActiveRecord::Base has_many :carrots end
  25. 25. Top Level namespace :db do desc quot;Create a database connection.quot; task :connect do # ... end desc quot;Prepare the database for tests.quot; task :prepare => :connect do # ... end end
  26. 26. Sandbox ActiveRecord::Schema.define do create_table :bunnies do |t| t.column :name, :string, :null => false end end ActiveRecord::Schema.define do drop_table :bunnies end
  27. 27. Piattaforma Java
  28. 28. Java => JRuby require 'java' include_class 'java.util.Random' class JavaRandom def self.next @@r ||= Random.new @@r.nextInt end end JavaRandom.next # => 23
  29. 29. JRuby => Java import org.jruby.*; public class UsingRuby { public static void main(String[] args) { Ruby runtime = Ruby.getDefaultInstance(); runtime.evalScript(quot;puts 1 + 2quot;); } }
  30. 30. www.lucaguidi.com www.sourcesense.com www.pronetics.it

×