module AMEE::DataAbstraction::PersistenceSupport::ClassMethods

Public Instance Methods

find(*args) click to toggle source

Find and initialize instance(s) of OngoingCalculation from the database using standard ActiveRecord find options. Returns nil if no records are found. If multiple records are found they are returned via an instance of the CalculationCollection class. E.g.,

 OngoingCalculation.find(:first)

                  #=> <AMEE::DataAbstraction::OngoingCalculation ... >

OngoingCalculation.find(:first,
                        :conditions => {:profile_item_uid => "K588DH47SMN5"})

                  #=> <AMEE::DataAbstraction::OngoingCalculation ... >

 OngoingCalculation.find(:all)

                  #=> <AMEE::DataAbstraction::CalculationCollection ... >
# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 182
def find(*args)
  unless args.last.is_a? Symbol or args.last.is_a? Integer
    raise ActiveRecord::ActiveRecordError.new("Using :include with terms and then conditioning on terms doesn't work due to rails caching.  Use the :joins option instead.") if args.last[:include].to_s.match(/terms/) && args.last[:conditions].to_s.match(/terms/)
    args.last[:include] = "terms" if args.last[:joins].to_s.match(/terms/)
  end
  result = AMEE::Db::Calculation.find(*args)
  return nil unless result
  if result.respond_to?(:map)
    CalculationCollection.new(result.compact.map { |calc| initialize_from_db_record(calc) })
  else
    initialize_from_db_record(result)
  end
end
find_by_type(ordinality,type,options={}) click to toggle source

Find calculations of type type in the database and initialize as instances of OngoingCalculation. Returns nil if no records are found. If multiple records are found they are returned via an instance of the CalculationCollection class.

Specify that either the first or all records should be returns by passing :first or :all as the first argument. The unique label of the calcualtion type required should be passed as the second argument. Standard options associated with the ActiveRecord find class method can be passed as the third argument. E.g.,

OngoingCalculation.find_by_type(:first,:electricity)

                 #=> <AMEE::DataAbstraction::OngoingCalculation ... >

OngoingCalculation.find_by_type(:all,:fuel)

                 #=> <AMEE::DataAbstraction::CalculationCollection ... >
# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 215
def find_by_type(ordinality,type,options={})
  OngoingCalculation.find(ordinality, options.merge(:conditions => {:calculation_type => type.to_s}))
end
initialize_from_db_record(record) click to toggle source

Initialize and return an instance of OngoingCalculation based on the database record represented by record.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 222
def initialize_from_db_record(record)
  unless record.is_a? AMEE::Db::Calculation
    raise ArgumentError.new("Argument is not of class AMEE::Db::Calculation")
  end
  calc = AMEE::DataAbstraction::CalculationSet.find_prototype_calculation(record.type).begin_calculation
  calc.db_calculation = record
  # Means that validation needs to occur before calcs are saved
          calc.choose_without_validation!(record.to_hash)
  return calc
end
storage_config() click to toggle source

Returns a new instance of the AMEE::Db::BaseConfig class

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 234
def storage_config
  AMEE::Db::BaseConfig.new
end
storage_method() click to toggle source

Returns the currently configured storage level for database persistence, i.e. whether all terms should be persisted versus outputs and/or metadata only.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 242
def storage_method
  storage_config.storage_method
end
store_inputs?() click to toggle source

Returns true if all terms should be persisted within the database according to the currently configured storage level (See AMEE::Db::BaseConfig). Otherwise, returns false.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 250
def store_inputs?
  storage_config.store_everything?
end
store_metadata?() click to toggle source

Returns true if metadata terms should be persisted within the database according to the currently configured storage level (See AMEE::Db::BaseConfig). Otherwise, returns false.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 266
def store_metadata?
  storage_config.store_metadata?
end
store_outputs?() click to toggle source

Returns true if output terms should be persisted within the database according to the currently configured storage level (See AMEE::Db::BaseConfig). Otherwise, returns false.

# File lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb, line 258
def store_outputs?
  storage_config.store_outputs?
end