ryan-allen / modelling

Wraps some common-ish plain-ruby object modelling junk.

This URL has Read+Write access

Ryan Allen (author)
Sat Jan 03 15:26:46 -0800 2009
commit  eb44f2771c7935ddd0c4513dcf75dd8eecc5b2d8
tree    816e11146971ebef5a619aa5f0ecef001168075b
parent  e378d424d027cdee49c810388943c5f2d74053e2
name age message
file MIT-LICENSE Loading commit data...
file README
file modelling.rb
file modelling_test.rb
file rakefile.rb
README
This module, when included, enforces the typical instantiation pattern
that accepts a hash of attributes, like so:

  class Car
    include Modelling
    attr_accessor :make
    attr_accessor :model
  end

  car = Car.new(:make => 'Lotus', :model => 'Elise')
  car.make  # => 'Lotus'
  car.model # => 'Elise'
  
Additionally you get three meta methods for making your so called domain
modelling a little more concise, these three methods are:

  * attributes
  * collections
  * maps

Attributes simply is an alias for attr_accessor, collections defines accessors
that are arrays, and maps defines hashes. The constructor ensures that each
map and collection are initalized as [] and {} respectively by default:
 
  class Car
    include Modelling
    attributes :make, :model
    collections :tyres, :accessories
    maps :dealer_locations, :telephone_services
  end

  car = Car.new 
  car.make               # => nil
  car.model              # => nil
  car.tyres              # => []
  car.accessories        # => []
  car.dealer_locations   # => {}
  car.telephone_services # => {}

TODO: WRITE DOCS FOR NEW CUSTOM INITIALIZERS!!!1 (AND UPDATE DOCS ABOVE,
      THEY ARE LIKE OUT OF DATE OR SOMETHING).
 i.e.   collections :categories => CategoryCollection
      and
        attributes :total => Proc.new { Money.new(0) }
      and
        maps :domain => CustomDNSWithHashLookupAwesome

This makes modelling with PORO's [1] more eloquent, and stops us from
having to write and re-write constructors that accept hashes, as
is typically done.

[1] http://en.wikipedia.org/wiki/POJO