--- !ruby/object:RI::ClassDescription 
attributes: []

class_methods: []

comment: 
- !ruby/struct:SM::Flow::P 
  body: "Active Record implements aggregation through a macro-like class method called <tt>composed_of</tt> for representing attributes as value objects. It expresses relationships like &quot;Account [is] composed of Money [among other things]&quot; or &quot;Person [is] composed of [an] address&quot;. Each call to the macro adds a description of how the value objects are created from the attributes of the entity object (when the entity is initialized either as a new object or from finding an existing object) and how it can be turned back into attributes (when the entity is saved to the database). Example:"
- !ruby/struct:SM::Flow::VERB 
  body: "  class Customer &lt; ActiveRecord::Base\n    composed_of :balance, :class_name =&gt; &quot;Money&quot;, :mapping =&gt; %w(balance amount)\n    composed_of :address, :mapping =&gt; [ %w(address_street street), %w(address_city city) ]\n  end\n"
- !ruby/struct:SM::Flow::P 
  body: "The customer class now has the following methods to manipulate the value objects:"
- !ruby/object:SM::Flow::LIST 
  contents: 
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Customer#balance, Customer#balance=(money)</tt>
  - !ruby/struct:SM::Flow::LI 
    label: "*"
    body: <tt>Customer#address, Customer#address=(address)</tt>
  type: :BULLET
- !ruby/struct:SM::Flow::P 
  body: "These methods will operate with value objects like the ones described below:"
- !ruby/struct:SM::Flow::VERB 
  body: " class Money\n   include Comparable\n   attr_reader :amount, :currency\n   EXCHANGE_RATES = { &quot;USD_TO_DKK&quot; =&gt; 6 }\n\n   def initialize(amount, currency = &quot;USD&quot;)\n     @amount, @currency = amount, currency\n   end\n\n   def exchange_to(other_currency)\n     exchanged_amount = (amount * EXCHANGE_RATES[&quot;#{currency}<em>TO</em>#{other_currency}&quot;]).floor\n     Money.new(exchanged_amount, other_currency)\n   end\n\n   def ==(other_money)\n     amount == other_money.amount &amp;&amp; currency == other_money.currency\n   end\n\n   def &lt;=&gt;(other_money)\n     if currency == other_money.currency\n       amount &lt;=&gt; amount\n     else\n       amount &lt;=&gt; other_money.exchange_to(currency).amount\n     end\n   end\n end\n\n class Address\n   attr_reader :street, :city\n   def initialize(street, city)\n     @street, @city = street, city\n   end\n\n   def close_to?(other_address)\n     city == other_address.city\n   end\n\n   def ==(other_address)\n     city == other_address.city &amp;&amp; street == other_address.street\n   end\n end\n"
- !ruby/struct:SM::Flow::P 
  body: "Now it's possible to access attributes from the database through the value objects instead. If you choose to name the composition the same as the attribute's name, it will be the only way to access that attribute. That's the case with our <tt>balance</tt> attribute. You interact with the value objects just like you would any other attribute, though:"
- !ruby/struct:SM::Flow::VERB 
  body: "  customer.balance = Money.new(20)     # sets the Money value object and the attribute\n  customer.balance                     # =&gt; Money value object\n  customer.balance.exchanged_to(&quot;DKK&quot;) # =&gt; Money.new(120, &quot;DKK&quot;)\n  customer.balance &gt; Money.new(10)     # =&gt; true\n  customer.balance == Money.new(20)    # =&gt; true\n  customer.balance &lt; Money.new(5)      # =&gt; false\n"
- !ruby/struct:SM::Flow::P 
  body: "Value objects can also be composed of multiple attributes, such as the case of Address. The order of the mappings will determine the order of the parameters. Example:"
- !ruby/struct:SM::Flow::VERB 
  body: "  customer.address_street = &quot;Hyancintvej&quot;\n  customer.address_city   = &quot;Copenhagen&quot;\n  customer.address        # =&gt; Address.new(&quot;Hyancintvej&quot;, &quot;Copenhagen&quot;)\n  customer.address = Address.new(&quot;May Street&quot;, &quot;Chicago&quot;)\n  customer.address_street # =&gt; &quot;May Street&quot;\n  customer.address_city   # =&gt; &quot;Chicago&quot;\n"
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Writing value objects
- !ruby/struct:SM::Flow::P 
  body: Value objects are immutable and interchangeable objects that represent a given value, such as a Money object representing $5. Two Money objects both representing $5 should be equal (through methods such as <tt>==</tt> and <tt>&lt;=&gt;</tt> from Comparable if ranking makes sense). This is unlike entity objects where equality is determined by identity. An entity class such as Customer can easily have two different objects that both have an address on Hyancintvej. Entity identity is determined by object or relational unique identifiers (such as primary keys). Normal ActiveRecord::Base classes are entity objects.
- !ruby/struct:SM::Flow::P 
  body: It's also important to treat the value objects as immutable. Don't allow the Money object to have its amount changed after creation. Create a new Money object with the new value instead. This is exemplified by the Money#exchanged_to method that returns a new value object instead of changing its own values. Active Record won't persist value objects that have been changed through means other than the writer method.
- !ruby/struct:SM::Flow::P 
  body: The immutable requirement is enforced by Active Record by freezing any object assigned as a value object. Attempting to change it afterwards will result in a ActiveSupport::FrozenObjectError.
- !ruby/struct:SM::Flow::P 
  body: Read more about value objects on http://c2.com/cgi/wiki?ValueObject and on the dangers of not keeping value objects immutable on http://c2.com/cgi/wiki?ValueObjectsShouldBeImmutable
- !ruby/struct:SM::Flow::H 
  level: 2
  text: Finding records by a value object
- !ruby/struct:SM::Flow::P 
  body: "Once a <tt>composed_of</tt> relationship is specified for a model, records can be loaded from the database by specifying an instance of the value object in the conditions hash. The following example finds all customers with <tt>balance_amount</tt> equal to 20 and <tt>balance_currency</tt> equal to &quot;USD&quot;:"
- !ruby/struct:SM::Flow::VERB 
  body: "  Customer.find(:all, :conditions =&gt; {:balance =&gt; Money.new(20, &quot;USD&quot;)})\n"
constants: []

full_name: ActiveRecord::Aggregations::ClassMethods
includes: []

instance_methods: 
- !ruby/object:RI::MethodSummary 
  name: composed_of
name: ClassMethods
superclass: 
