Class: ResidenceFuelType

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/earth/residence/residence_fuel_type.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](fuel) ⇒ Object



33
34
35
# File 'lib/earth/residence/residence_fuel_type.rb', line 33

def [](fuel)
  find_by_name fuel.to_s.humanize.downcase
end

Instance Method Details

#price_per_unit(relaxations = []) ⇒ Object

col :energy_content, :type => :float col :energy_content_units



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/earth/residence/residence_fuel_type.rb', line 12

def price_per_unit(relaxations = [])
  conditions = { :residence_fuel_type_name => self }
  relaxations.push Hash.new
  relaxations.each do |relaxation|
    relaxation_conditions = Hash.new
    if timeframe = relaxation[:timeframe]
      relaxation_conditions[:year] = timeframe.from.year
      relaxation_conditions[:month] = timeframe.from.month..timeframe.to.yesterday.month
    end
    if location = relaxation[:location]
      relaxation_conditions[:locatable_type] = location.class.to_s
      relaxation_conditions[:locatable_id] = location.id
    end
    if non_nil_result = ResidenceFuelPrice.average(:price, :conditions => conditions.merge(relaxation_conditions))
      return non_nil_result
    end
  end
  nil
end