class AMEE::Db::Term

This class represents a database record for a calculation term used with the AMEE:DataAbstraction::OngoingCalculation class. This class stores the label, value and unit attributes of specific calculation terms, and is owned by an associated AMEE::Db::Calculation record

This class is typically used by proxy, via the #update_calculation!, and #to_hash methods of the AMEE::Db::Calculation class, and ultimately called from the find, find_by_type, #save, #delete, and #get_db_calculation methods associated with the AMEE:DataAbstraction::OngoingCalculation class.

Public Instance Methods

to_hash() click to toggle source

Returns a Hash representation of self, e.g.

my_term.to_hash       #=> { :value => 1600,
                            :unit => <Quantify::Unit::SI> }

my_term.to_hash       #=> { :value => 234.1,
                            :unit => <Quantify::Unit::NonSI>,
                            :per_unit => <Quantify::Unit::SI> }

This method is called as part of AMEE::Db::Calculation#to_hash in order to provide a full hash representation of a calculation.

# File lib/amee/db/term.rb, line 40
def to_hash
  sub_hash = {}
  # Float method called on term value in order to initialize
  # explicitly numeric values as numeric objects.
  #
  sub_hash[:value] = AMEE::DataAbstraction::Term.convert_value_to_type(self.value, self.value_type)
  sub_hash[:unit] = Unit.for(unit) if unit
  sub_hash[:per_unit] = Unit.for(per_unit) if per_unit
  { label.to_sym => sub_hash }
end

Private Instance Methods

initialize_units() click to toggle source

Serialize all unit attributes using the string provided by the label attribute of a Quantify::Unit::Base object

# File lib/amee/db/term.rb, line 56
def initialize_units
  if unit
    self.unit = unit.is_a?(Quantify::Unit::Base) ? unit.label : unit
  end
  if per_unit
    self.per_unit = per_unit.is_a?(Quantify::Unit::Base) ? per_unit.label : per_unit
  end
end
initialize_value() click to toggle source
# File lib/amee/db/term.rb, line 65
def initialize_value
  unless self.value.nil?
    self.value_type = self.value.class.to_s
    self.value      = self.value.to_s
  end
end