Module: BrighterPlanet::Motorcycle::CarbonModel

Defined in:
lib/motorcycle/carbon_model.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/motorcycle/carbon_model.rb', line 8

def self.included(base)
  base.decide :emission, :with => :characteristics do
    committee :emission do # kg CO2
      quorum 'from fuel', :needs => [:fuel_consumed, :emission_factor] do |characteristics|
        characteristics[:fuel_consumed] * characteristics[:emission_factor]
      end
    end
    
    committee :fuel_consumed do # litres fuel
      quorum 'from distance and fuel efficiency', :needs => [:distance, :fuel_efficiency] do |characteristics|
        characteristics[:distance] / characteristics[:fuel_efficiency]
      end
    end
    
    committee :emission_factor do # kg CO2 per litre
      quorum 'from fuel type', :needs => :fuel_type do |characteristics|
        characteristics[:fuel_type].emission_factor
      end
    end
    
    committee :distance do # km
      quorum 'from annual distance', :needs => [:annual_distance, :active_subtimeframe] do |characteristics, timeframe|
        characteristics[:annual_distance] * (characteristics[:active_subtimeframe] / timeframe.year)
      end
    end
    
    committee :fuel_efficiency do # km per litre
      quorum 'default' do
        base.fallback.fuel_efficiency
      end
    end
    
    committee :fuel_type do
      quorum 'default' do
        AutomobileFuelType.find_by_code 'R'
      end
    end
    
    committee :annual_distance do # kilometres
      quorum 'from annual distance estimate', :needs => :annual_distance_estimate do |characteristics|
        characteristics[:annual_distance_estimate]
      end

      quorum 'from weekly distance estimate', :needs => :weekly_distance_estimate do |characteristics, timeframe|
        (characteristics[:weekly_distance_estimate] / 7 ) * timeframe.year.days
      end
      
      quorum 'default' do
        base.fallback.annual_distance_estimate
      end
    end

    committee :active_subtimeframe do
      quorum 'from acquisition and retirement', :needs => [:acquisition, :retirement] do |characteristics, timeframe|
        Timeframe.constrained_new characteristics[:acquisition].to_date, characteristics[:retirement].to_date, timeframe
      end
    end
    
    committee :acquisition do
      quorum 'from retirement', :appreciates => :retirement do |characteristics, timeframe|
        [ timeframe.from, characteristics[:retirement] ].compact.min
      end
    end
    
    committee :retirement do
      quorum 'from acquisition', :appreciates => :acquisition do |characteristics, timeframe|
        [ timeframe.to, characteristics[:acquisition] ].compact.max
      end
    end
  end
end