JRuby e DSL

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Group

    JRuby e DSL - Presentation Transcript

    1. JRuby e DSLs Luca Guidi - Sourcesense Java Day Roma - 1 Dic 2007
    2. DSLs cosa sono?
    3. Domain Specific Language
    4. DSLs • single purpose • singolo dominio
    5. Linguaggi di programmazione • general purpose • multi dominio
    6. YACC (1975) Stephen C. Johnson
    7. UNIX scripting
    8. ls | grep dsl >> ~/list.txt
    9. favoriscono le ontologie
    10. Ontologia: rappresentazione strutturata delle informazioni espressa tramite gerarchie e relazioni tra classi.
    11. Behavior Driven Development
    12. 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
    13. should
    14. Secondo Martin Fowler i DSL sono: • Esterni (XML) • Interni (Smalltalk, Ruby)
    15. Java • Non dinamico • Strongly typed
    16. <?xml version=\"1.0\" encoding=\"UTF-8\"?> <book> <attribute id=\"title\"> <required>true</required> <length> <minimum>1</minimum> <maximum>23</maximum> </length> </attribute> </book>
    17. Annotations
    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(\"title\") .getAnnotation(Size.class); return ( title != null && ( title.length() >= size.min() || title.length() <= size.max() ) ); } }
    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. 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. 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. Ruby • Symbol • Block • Module • Splat • eval, instance_eval, class_eval • define_method, alias_method
    23. Ruby DSLs
    24. Method Class class Carrot < ActiveRecord::Base belongs_to :bunny end class Bunny < ActiveRecord::Base has_many :carrots end
    25. Top Level namespace :db do desc \"Create a database connection.\" task :connect do # ... end desc \"Prepare the database for tests.\" task :prepare => :connect do # ... end end
    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. Piattaforma Java
    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. JRuby => Java import org.jruby.*; public class UsingRuby { public static void main(String[] args) { Ruby runtime = Ruby.getDefaultInstance(); runtime.evalScript(\"puts 1 + 2\"); } }
    30. www.lucaguidi.com www.sourcesense.com www.pronetics.it

    + jodoshajodosha, 2 years ago

    custom

    1347 views, 0 favs, 0 embeds more stats

    Panoramica sui DSL e sul loro utilizzo con i lingua more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1347
      • 1347 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 11
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories

    Groups / Events