Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 65 lines (51 sloc) 1.817 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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
  * structs

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 # => {}

Structs are as the name suggests, structs. Or more specifically OpenStructs:

  class Car
    include Modelling
    structs :features
  end
  
  car.features.abs true
  car.features.air_con true
  car.features.valves 8

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
Something went wrong with that request. Please try again.