Module: AMEE::DataAbstraction::PersistenceSupport::ClassMethods

Defined in:
lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object

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


182
183
184
185
186
187
188
189
190
191
192
193
194
# 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 = {}) ⇒ Object

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


215
216
217
# 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) ⇒ Object

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



222
223
224
225
226
227
228
229
230
231
# 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_configObject

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



234
235
236
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 234

def storage_config
  AMEE::Db::BaseConfig.new
end

#storage_methodObject

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



242
243
244
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 242

def storage_method
  storage_config.storage_method
end

#store_inputs?Boolean

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.

Returns:

  • (Boolean)


250
251
252
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 250

def store_inputs?
  storage_config.store_everything?
end

#store_metadata?Boolean

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.

Returns:

  • (Boolean)


266
267
268
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 266

def store_metadata?
  storage_config.store_metadata?
end

#store_outputs?Boolean

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.

Returns:

  • (Boolean)


258
259
260
# File 'lib/amee/data_abstraction/ongoing_calculation_persistence_support.rb', line 258

def store_outputs?
  storage_config.store_outputs?
end