Module: AMEE::DataAbstraction::PersistenceSupport
- Defined in:
- lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb
Overview
This module provides a number of class and instance methods which are added to the AMEE::DataAbstraction::OngoingCalculation class if the amee-data-persistence gem is required. These methods provide an interface between the AMEE::DataAbstraction::OngoingCalculation class (and its instances) and the the AMEE::Db::Calculation class which provides database persistence for calculations.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#db_calculation ⇒ Object
Represents the instance of AMEE::Db::Calculation which is associated with
self
.
Class Method Summary collapse
Instance Method Summary collapse
-
#calculate_and_save ⇒ Object
Performs the calculation against AMEE and saves it to the database.
-
#calculate_and_save! ⇒ Object
As calculate_and_save but raises an exception on error.
-
#delete ⇒ Object
Deletes the database record for
self
and any associated profile item value in the AMEE platform. -
#get_db_calculation ⇒ Object
Finds the instance of database record associated with
self
based on theprofile_item_uid
attribute ofself
and sets thedb_calculation
attribute ofself
to the associated instance of AMEE::Db::Calculation. -
#id ⇒ Object
Represents the primary key of the associated database record (instance of AMEE::Db::Calculation) if a database record for
self
is defined. -
#save ⇒ Object
Saves a representation of <tt>self<tt> to the database.
-
#save! ⇒ Object
Same as save but raises an exception on error.
-
#stored_terms ⇒ Object
Returns the subset of terms associated with
self
which should be passed for database persistence, based on the configuration set in AMEE::Db::Config#storage_method. -
#to_hash(representation = :stored_terms_only) ⇒ Object
Returns a hash representation of
self
.
Instance Attribute Details
#db_calculation ⇒ Object
Represents the instance of AMEE::Db::Calculation which is associated with self
.
25 26 27 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 25 def db_calculation @db_calculation end |
Class Method Details
.included(base) ⇒ Object
18 19 20 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 18 def self.included(base) base.extend ClassMethods end |
Instance Method Details
#calculate_and_save ⇒ Object
Performs the calculation against AMEE and saves it to the database
69 70 71 72 73 74 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 69 def calculate_and_save calculate_and_save! true rescue ActiveRecord::RecordNotFound, AMEE::DataAbstraction::Exceptions::DidNotCreateProfileItem false end |
#calculate_and_save! ⇒ Object
As calculate_and_save but raises an exception on error
63 64 65 66 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 63 def calculate_and_save! calculate! save! end |
#delete ⇒ Object
Deletes the database record for self
and any associated profile item value in the AMEE platform.
55 56 57 58 59 60 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 55 def delete record = db_calculation || get_db_calculation AMEE::Db::Calculation.delete record.id self.db_calculation = nil delete_profile_item end |
#get_db_calculation ⇒ Object
Finds the instance of database record associated with self
based on the profile_item_uid
attribute of self
and sets the db_calculation
attribute of self
to the associated instance of AMEE::Db::Calculation.
81 82 83 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 81 def get_db_calculation self.db_calculation = AMEE::Db::Calculation.find_or_initialize_by_profile_item_uid(send :profile_item_uid) end |
#id ⇒ Object
Represents the primary key of the associated database record (instance of AMEE::Db::Calculation) if a database record for self
is defined.
31 32 33 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 31 def id db_calculation.nil? ? nil : db_calculation.id end |
#save ⇒ Object
Saves a representation of self<tt> to the database. Returns <tt>true
if successful, otherwise false
.
45 46 47 48 49 50 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 45 def save save! true rescue ActiveRecord::RecordNotSaved false end |
#save! ⇒ Object
Same as save but raises an exception on error
36 37 38 39 40 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 36 def save! validate! record = db_calculation || get_db_calculation record.update_calculation!(to_hash) end |
#stored_terms ⇒ Object
Returns the subset of terms associated with self
which should be passed for database persistence, based on the configuration set in AMEE::Db::Config#storage_method.
89 90 91 92 93 94 95 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 89 def stored_terms stored_terms = [] stored_terms += if OngoingCalculation. stored_terms += inputs if OngoingCalculation.store_inputs? stored_terms += outputs if OngoingCalculation.store_outputs? stored_terms end |
#to_hash(representation = :stored_terms_only) ⇒ Object
Returns a hash representation of self
. By default, only the terms which are configured for persistence (according to AMEE::Db::Config#storage_method) are included. All terms can be explicitly required by passing the symbol :full
as an argument. E.g.
# Set storage to include everything
AMEE::Db::Config.storage_method=:everything
my_calculation.to_hash #=> { :calculation_type => :fuel,
:profile_item_uid => nil,
:profile_uid => "A8D8R95EE7DH",
:type => { :value => 'coal'},
:location => { :value => 'facility' },
:mass => { :value => 250,
:unit => <Quantify::Unit ... > },
:co2 => { :value => 60.5,
:unit => <Quantify::Unit ... > }}
# Set storage to include only oputputs and metadata
AMEE::Db::Config.storage_method=:outputs
my_calculation.to_hash #=> { :calculation_type => :fuel,
:profile_item_uid => nil,
:profile_uid => "A8D8R95EE7DH",
:location => { :value => 'facility' },
:co2 => { :value => 60.5,
:unit => <Quantify::Unit ... > }}
# Set storage to include only metadata
AMEE::Db::Config.storage_method=:metadata
my_calculation.to_hash #=> { :calculation_type => :fuel,
:profile_item_uid => nil,
:profile_uid => "A8D8R95EE7DH",
:location => { :value => 'facility' },
# Get full hash represenation regardless of storage level
my_calculation.to_hash :full #=> { :calculation_type => :fuel,
:profile_item_uid => nil,
:profile_uid => "A8D8R95EE7DH",
:type => { :value => 'coal'},
:location => { :value => 'facility' },
:mass => { :value => 250,
:unit => <Quantify::Unit ... > },
:co2 => { :value => 60.5,
:unit => <Quantify::Unit ... > }}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 145 def to_hash(representation=:stored_terms_only) hash = {} hash[:calculation_type] = label hash[:profile_item_uid] = send :profile_item_uid hash[:profile_uid] = send :profile_uid (representation == :full ? terms : stored_terms ).each do |term| sub_hash = {} sub_hash[:value] = term.value sub_hash[:unit] = term.unit if term.unit sub_hash[:per_unit] = term.per_unit if term.per_unit hash[term.label.to_sym] = sub_hash end return hash end |