Sha256: 85b03f7b563d7fc71c3bbca8ceb84ea739b0239b758e7fbe10dedd062da605a3

Contents?: true

Size: 1.75 KB

Versions: 7

Compression:

Stored size: 1.75 KB

Contents

require 'earth/model'

require 'earth/fuel/fuel_year'

class Fuel < ActiveRecord::Base
  data_miner do
    process "Data mine FuelYear because some methods are delegated to it" do
      FuelYear.run_data_miner!
    end
  end
  
  extend Earth::Model

  TABLE_STRUCTURE = <<-EOS

CREATE TABLE fuels
  (
     name                               CHARACTER VARYING(255) NOT NULL PRIMARY KEY,
     physical_units                     CHARACTER VARYING(255),
     density                            FLOAT,
     density_units                      CHARACTER VARYING(255),
     energy_content                     FLOAT,
     energy_content_units               CHARACTER VARYING(255),
     carbon_content                     FLOAT,
     carbon_content_units               CHARACTER VARYING(255),
     oxidation_factor                   FLOAT,
     biogenic_fraction                  FLOAT,
     co2_emission_factor                FLOAT,
     co2_emission_factor_units          CHARACTER VARYING(255),
     co2_biogenic_emission_factor       FLOAT,
     co2_biogenic_emission_factor_units CHARACTER VARYING(255)
  );

EOS

  self.primary_key = "name"
  
  has_many :fuel_years, :foreign_key => 'fuel_name'
  
  def latest_fuel_year
    fuel_years.find_by_year fuel_years.maximum(:year)
  end
  
  [
    :energy_content,
    :energy_content_units,
    :carbon_content,
    :carbon_content_units,
    :oxidation_factor,
    :biogenic_fraction,
    :co2_emission_factor,
    :co2_emission_factor_units,
    :co2_biogenic_emission_factor,
    :co2_biogenic_emission_factor_units
  ].each do |method_name|
    define_method method_name do
      if attribute = super()
        attribute
      elsif fuel_years.present?
        latest_fuel_year.send method_name
      end
    end
  end
  
  warn_unless_size 23
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
earth-1.2.1 lib/earth/fuel/fuel.rb
earth-1.1.2 lib/earth/fuel/fuel.rb
earth-1.1.1 lib/earth/fuel/fuel.rb
earth-1.1.0 lib/earth/fuel/fuel.rb
earth-1.0.3 lib/earth/fuel/fuel.rb
earth-1.0.2 lib/earth/fuel/fuel.rb
earth-1.0.1 lib/earth/fuel/fuel.rb