begin29
12/9/2016 - 6:21 PM

composed_of for manipulating ValueObjects add composition class to active_record class with additional information and ability to write valu

composed_of for manipulating ValueObjects add composition class to active_record class with additional information and ability to write value converter

#If we have following code in model:

composed_of :temperature, :mapping => %w(celsius)

#Then our composition class can be this:

class Temperature
  def initialize(celsius)
    @celsius = celsius
  end

  # This method is called by ActiveRecord, when record is saved.
  # Result of this method will be stored in table in "celsius" field,
  # and later when the record is loaded again, this will go to 
  # our Temperature#new constructor.
  def celsius
    @celsius
  end

  # This is example of method that we can add to make this composition useful.
  def farenheit 
    @celsius * 9/5 + 32
  end
end

# detailed example http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html#method-i-composed_of