module AMEE::DataAbstraction::PersistenceSupport

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.

Attributes

db_calculation[RW]

Represents the instance of AMEE::Db::Calculation which is associated with self.

Public Class Methods

included(base) click to toggle source
# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 18
def self.included(base)
  base.extend ClassMethods
end

Public Instance Methods

calculate_and_save() click to toggle source

Performs the calculation against AMEE and saves it to the database

# 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!() click to toggle source

As calculate_and_save but raises an exception on error

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 63
def calculate_and_save!
  calculate!
  save!
end
delete() click to toggle source

Deletes the database record for self and any associated profile item value in the AMEE platform.

# 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() click to toggle source

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.

# 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() click to toggle source

Represents the primary key of the associated database record (instance of AMEE::Db::Calculation) if a database record for self is defined.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 31
def id
  db_calculation.nil? ? nil : db_calculation.id
end
save() click to toggle source

Saves a representation of self<tt> to the database. Returns <tt>true if successful, otherwise false.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 45
def save
  save!
  true
rescue ActiveRecord::RecordNotSaved
  false
end
save!() click to toggle source

Same as save but raises an exception on error

# 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() click to toggle source

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.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 89
def stored_terms
  stored_terms = []
  stored_terms += metadata if OngoingCalculation.store_metadata?
  stored_terms += inputs if OngoingCalculation.store_inputs?
  stored_terms += outputs if OngoingCalculation.store_outputs?
  stored_terms
end
to_hash(representation=:stored_terms_only) click to toggle source

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 ... > }}
# 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