Sha256: e4584067a2fe67a925a56dad18eb30be6e7f39c5527b2f6dacf1593ce60c76fd

Contents?: true

Size: 1.36 KB

Versions: 8

Compression:

Stored size: 1.36 KB

Contents

require 'earth/model'

require 'earth/residence/residence_fuel_price'

class ResidenceFuelType < ActiveRecord::Base
  extend Earth::Model

  TABLE_STRUCTURE = <<-EOS

CREATE TABLE residence_fuel_types
  (
     name                  CHARACTER VARYING(255) NOT NULL PRIMARY KEY,
     emission_factor       FLOAT,
     emission_factor_units CHARACTER VARYING(255)
  );

EOS

  self.primary_key = "name"
  
  has_many :prices, :class_name => 'ResidenceFuelPrice', :foreign_key => 'residence_fuel_type_name'
  
  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

  warn_unless_size 7
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
earth-1.2.1 lib/earth/residence/residence_fuel_type.rb
earth-1.1.2 lib/earth/residence/residence_fuel_type.rb
earth-1.1.1 lib/earth/residence/residence_fuel_type.rb
earth-1.1.0 lib/earth/residence/residence_fuel_type.rb
earth-1.0.3 lib/earth/residence/residence_fuel_type.rb
earth-1.0.2 lib/earth/residence/residence_fuel_type.rb
earth-1.0.1 lib/earth/residence/residence_fuel_type.rb
earth-1.0.0 lib/earth/residence/residence_fuel_type.rb