Module: BrighterPlanet::Pet::CarbonModel

Defined in:
lib/pet/carbon_model.rb

Constant Summary collapse

ANNUAL_VETERINARY_EMISSION =
0.00437.tons.to :kilograms

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/pet/carbon_model.rb', line 10

def self.included(base)
  base.decide :emission, :with => :characteristics do
    committee :emission do
      quorum 'from diet size', :needs => [:diet_size, :diet_emission_intensity, :active_subtimeframe] do |characteristics, timeframe|
        characteristics[:active_subtimeframe].days * characteristics[:diet_size] * characteristics[:diet_emission_intensity] + ANNUAL_VETERINARY_EMISSION * (characteristics[:active_subtimeframe] / timeframe.year)
      end
    end
    
    committee :diet_emission_intensity do # kg/joule
      quorum 'from species', :needs => :species do |characteristics|
        characteristics[:species].diet_emission_intensity
      end
      
      quorum 'default' do
        base.fallback.diet_emission_intensity
      end
    end
    
    committee :diet_size do # joules
      quorum 'from weight', :needs => [:weight, :marginal_dietary_requirement, :fixed_dietary_requirement] do |characteristics|
        characteristics[:weight] * characteristics[:marginal_dietary_requirement] + characteristics[:fixed_dietary_requirement]
      end
    end
    
    committee :marginal_dietary_requirement do # joules/kg
      quorum 'from species', :needs => :species do |characteristics|
        characteristics[:species].marginal_dietary_requirement
      end

      quorum 'default' do
        base.fallback.marginal_dietary_requirement
      end
    end
    
    committee :fixed_dietary_requirement do # joules
      quorum 'from species', :needs => :species do |characteristics|
        characteristics[:species].fixed_dietary_requirement
      end

      quorum 'default' do
        base.fallback.fixed_dietary_requirement
      end
    end
    
    committee :weight do # kg
      quorum 'from breed and gender', :needs => [:breed, :gender] do |characteristics|
        if breed_gender = BreedGender.find_by_breed_name_and_gender_name(characteristics[:breed].name, characteristics[:gender].name)
          breed_gender.weight
        end
      end
      
      quorum 'from breed', :needs => :breed do |characteristics|
        characteristics[:breed].weight
      end

      quorum 'from species', :needs => :species do |characteristics|
        characteristics[:species].weight
      end

      quorum 'default' do
        base.fallback.weight
      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