Sha256: 1ad901a7821a62481ed5b86022967576ed145037c706ae0631544cf12436e496

Contents?: true

Size: 1.16 KB

Versions: 10

Compression:

Stored size: 1.16 KB

Contents

class ResidenceFuelType < ActiveRecord::Base
  set_primary_key :name
  
  has_many :prices, :class_name => 'ResidenceFuelPrice', :foreign_key => 'residence_fuel_type_name'
  
  col :name
  col :emission_factor, :type => :float
  col :emission_factor_units
  # col :energy_content, :type => :float
  # col :energy_content_units
  
  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

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

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
earth-0.11.10 lib/earth/residence/residence_fuel_type.rb
earth-0.11.9 lib/earth/residence/residence_fuel_type.rb
earth-0.11.8 lib/earth/residence/residence_fuel_type.rb
earth-0.11.7 lib/earth/residence/residence_fuel_type.rb
earth-0.11.6 lib/earth/residence/residence_fuel_type.rb
earth-0.11.5 lib/earth/residence/residence_fuel_type.rb
earth-0.11.4 lib/earth/residence/residence_fuel_type.rb
earth-0.11.3 lib/earth/residence/residence_fuel_type.rb
earth-0.11.2 lib/earth/residence/residence_fuel_type.rb
earth-0.11.1 lib/earth/residence/residence_fuel_type.rb