eco-friendly
metaprogramming
doodle is a Ruby gem for creating extended attribute accessors with defaults, conversions and validations.
doodle is eco-friendly - it does not globally modify Object, Class or Module, nor does it pollute instances with its own instance variables (e.g. it plays nice with yaml).
Doodle also supports reflection, serialization to and from YAML and XML, and command line argument processing.
Putting all this together, you can initialize objects like this:
event = Event "Festival" do date '2009-04-01' place "The muddy field" place "Beer tent" do event "Drinking" end end pp event # >> #<Event:0x5d2b2c # >> @date=#<Date: 4909845/2,0,2299161>, # >> @locations= # >> [#<Location:0x5baf2c @events=[], @name="The muddy field">, # >> #<Location:0x5aca58 # >> @events=[#<Event:0x59e3f4 @locations=[], @name="Drinking">], # >> @name="Beer tent">], # >> @name="Festival">
from a definition like this:
class Location < Doodle has :name, :kind => String has :events, :collect => :Event end class Event # or if you want to inherit from another class include Doodle::Core BASEDATE = Date.civil(2009, 01, 01) has :name, :kind => String has Date do default { Date.today } must "be >= #{BASEDATE}" do |value| value >= BASEDATE end from String do |s| Date.parse(s) end end has :locations, :collect => { :place => :Location } end
$ sudo gem install doodle
or
C:\> gem install doodle
depending on which way you swing ;)
doodle has been tested with Ruby 1.8.6, 1.8.7, 1.9.1 and JRuby 1.1.6. Getting it to work with Rubinius is underway.
You might also be interested in some of the many similar libraries.