Class: AMEE::Db::Calculation
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- AMEE::Db::Calculation
- Defined in:
- lib/amee/db/calculation.rb
Overview
This class represents a database record for a calculation performed using the AMEE:DataAbstraction::OngoingCalculation class. This class stores the primary calculation level attributes such as the calculation calculation_type
, profile_uid
and profile_item_uid
.
The values and attributes of specific calculation terms are stored via the related class AMEE::Db::Term.
This class is typically used by proxy, via the find
, find_by_type
, #save
, #delete
, and #get_db_calculation
methods associated with the AMEE:DataAbstraction::OngoingCalculation class.
Instance Method Summary collapse
-
#add_or_update_term!(label, data) ⇒ Object
Add, or update an existing, associated term represented by the label
label
and value, unit and/or per_unit attributes defined bydata
. -
#delete_unspecified_terms(options) ⇒ Object
Delete all of the terms which are not explicitly referenced in the
options
hash. -
#primary_calculation_attributes ⇒ Object
Returns the subset of all instance attributes which should be editable via mass update methods and which should be included in hash representations of self, i.e.
-
#to_hash ⇒ Object
Returns a Hash representation of
self
including a only the data explicitly passed in (those added by ActiveRecord -created
,updated
,id
- are ignored) as well as sub-hashes for all associated terms. -
#type ⇒ Object
Convenience method for returning the
calculation_type
attribute ofself
in canonical symbol form. -
#update_calculation!(options) ⇒ Object
Update the attributes of
self
and those of any related terms, according to the passedoptions
hash. -
#update_calculation_attribute!(key, value, save = true) ⇒ Object
Update the attribute of
self
represented by the labelkey
with the value ofvalue
. -
#validate_calculation_type ⇒ Object
Standardize the
calculation_type
attribute to String format.
Instance Method Details
#add_or_update_term!(label, data) ⇒ Object
Add, or update an existing, associated term represented by the label label
and value, unit and/or per_unit attributes defined by data
. The data
argument should be a hash with keys represent one or more of :value, :unit and :per_unit. E.g.,
data = { :value => 1400,
:unit => <Quantify::Unit::SI ... > }
my_calculation.add_or_update_term!(:distance, data)
This method is called as part of the #update_calculation!
method
112 113 114 115 |
# File 'lib/amee/db/calculation.rb', line 112 def add_or_update_term!(label,data) term = Term.find_or_initialize_by_calculation_id_and_label(id,label.to_s) term.update_attributes!(data) end |
#delete_unspecified_terms(options) ⇒ Object
Delete all of the terms which are not explicitly referenced in the options
hash.
This method is called as part of the #update_calculation!
method
123 124 125 126 127 |
# File 'lib/amee/db/calculation.rb', line 123 def delete_unspecified_terms() terms.each do |term| Term.delete(term.id) unless .keys.include? term.label.to_sym end end |
#primary_calculation_attributes ⇒ Object
Returns the subset of all instance attributes which should be editable via mass update methods and which should be included in hash representations of self, i.e. those passed in explcitly as data rather than added by ActiveRecord (e.g. id
, created_at
, etc.).
50 51 52 |
# File 'lib/amee/db/calculation.rb', line 50 def primary_calculation_attributes attributes.keys.reject {|attr| ['id','created_at','updated_at'].include? attr } end |
#to_hash ⇒ Object
Returns a Hash representation of self
including a only the data explicitly passed in (those added by ActiveRecord - created
, updated
, id
- are ignored) as well as sub-hashes for all associated terms. E.g.,
my_calculation.to_hash #=> { :profile_uid => "EYR758EY36WY",
:profile_item_uid => "W83URT48DY3W",
:type => { :value => 'car' },
:distance => { :value => 1600,
:unit => <Quantify::Unit::SI> },
:co2 => { :value => 234.1,
:unit => <Quantify::Unit::NonSI> }}
This method can be used to initialize instances of the class OngoingCalculation
by providing the hashed options for any of the choose...
methods.
146 147 148 149 150 151 152 153 |
# File 'lib/amee/db/calculation.rb', line 146 def to_hash hash = {} terms.each { |term| hash.merge!(term.to_hash) } [ :profile_item_uid, :profile_uid ].each do |attr| hash[attr] = self.send(attr) end return hash end |
#type ⇒ Object
Convenience method for returning the calculation_type
attribute of self
in canonical symbol form.
41 42 43 |
# File 'lib/amee/db/calculation.rb', line 41 def type calculation_type.to_sym end |
#update_calculation!(options) ⇒ Object
Update the attributes of self
and those of any related terms, according to the passed options
hash. Any associated terms which are not represented in options
are deleted.
Term attributes provided in options
should be keyed with the term label and include a sub-hash with keys represent one or more of :value, :unit and :per_unit. E.g.,
options = { :profile_item_uid => "W93UEY573U4E8",
:mass => { :value => 23 },
:distance => { :value => 1400,
:unit => <Quantify::Unit::SI ... > }}
my_calculation.update_calculation!(options)
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/amee/db/calculation.rb', line 69 def update_calculation!() primary_calculation_attributes.each do |attr| if .keys.include? attr.to_sym update_calculation_attribute!(attr,.delete(attr.to_sym),false) end end save! .each_pair do |attribute,value| add_or_update_term!(attribute,value) end delete_unspecified_terms() reload end |
#update_calculation_attribute!(key, value, save = true) ⇒ Object
Update the attribute of self
represented by the label key
with the value of value
. By default, the #save!
method is called, in turn calling the class validations.
Specify that the record should not be saved by passing false
as the final argument.
91 92 93 94 95 96 97 |
# File 'lib/amee/db/calculation.rb', line 91 def update_calculation_attribute!(key,value,save=true) # use attr_accessor (via #send) method rather than # #update_attribute so that validations are performed # send("#{key}=", (value.nil? ? nil : value.to_s)) save! if save end |
#validate_calculation_type ⇒ Object
Standardize the calculation_type
attribute to String format. Called using before filter prior to record saving to ensure string serialization.
34 35 36 |
# File 'lib/amee/db/calculation.rb', line 34 def validate_calculation_type self.calculation_type = calculation_type.to_s end |