Instantiating With Sequel::Model
One nice thing about Sequel::Model is that it allows both hash and block instantiation of models.
1 values = { :name => "Bob Roberts" } 2 organisation = Organisation.new( values ) do |o| 3 o.name ||= random_organisation_name 4 o.email_address ||= random_organisation_email 5 o.phone_number ||= random_phone_number 6 end
The call to Organisation.new( values )
sets the
attributes from the hash and then using Ruby’s ||=
operator (remember a ||= b
is equivalent to a = a ||
b
) in the block we set any attributes not present in the
hash.
Aimred is a specialist Ruby and Ruby on Rails development house and consultancy based in Cape Town, South Africa.