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
Project
modelling
We were doing PORO dev on Rails before it was hip.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
Pull Requests
Development
Dependencies
Development
>= 0
Project Readme