carbon 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +10 -0
- data/README.markdown +44 -2
- data/lib/carbon.rb +5 -3
- data/lib/carbon/registry.rb +9 -6
- data/lib/carbon/version.rb +1 -1
- data/test/carbon_test.rb +13 -1
- metadata +78 -116
data/.gitignore
CHANGED
data/CHANGELOG
CHANGED
data/README.markdown
CHANGED
@@ -82,7 +82,38 @@ For more, see the "Console" section below.
|
|
82
82
|
|
83
83
|
Carbon works by extending any Ruby class to be an emission source. You `include Carbon` and then use the `emit_as` DSL...
|
84
84
|
|
85
|
-
|
85
|
+
# see Carbon::ClassMethods#emit_as for more details
|
86
|
+
class MyFlight
|
87
|
+
def airline
|
88
|
+
# ... => MyAirline(:name, :icao_code, ...)
|
89
|
+
end
|
90
|
+
def aircraft
|
91
|
+
# ... => MyAircraft(:name, :icao_code, ...)
|
92
|
+
end
|
93
|
+
def origin
|
94
|
+
# ... => String
|
95
|
+
end
|
96
|
+
def destination
|
97
|
+
# ... => String
|
98
|
+
end
|
99
|
+
def segments_per_trip
|
100
|
+
# ... => Integer
|
101
|
+
end
|
102
|
+
def trips
|
103
|
+
# ... => Integer
|
104
|
+
end
|
105
|
+
include Carbon
|
106
|
+
emit_as 'Flight' do
|
107
|
+
provide :segments_per_trip
|
108
|
+
provide :trips
|
109
|
+
provide :origin, :as => :origin_airport, :key => :iata_code
|
110
|
+
provide :destination, :as => :destination_airport, :key => :iata_code
|
111
|
+
provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
|
112
|
+
provide(:aircraft, :key => :icao_code) { { |f| f.aircraft.try(:icao_code) }
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
See [RDoc on `Carbon::ClassMethods#emit_as`](http://rdoc.info/github/brighterplanet/carbon/Carbon/ClassMethods#emit_as-instance_method) for all the details.
|
86
117
|
|
87
118
|
The final URL will be something like
|
88
119
|
|
@@ -90,7 +121,18 @@ The final URL will be something like
|
|
90
121
|
|
91
122
|
When you want to calculate impacts, simply call `MyFlight#impact`.
|
92
123
|
|
93
|
-
|
124
|
+
?> my_flight = MyFlight.new([...])
|
125
|
+
=> #<MyFlight [...]>
|
126
|
+
?> my_impact = my_flight.impact(:timeframe => Timeframe.new(:year => 2009))
|
127
|
+
=> #<Hashie::Mash [...]>
|
128
|
+
?> my_impact.decisions.carbon.object.value
|
129
|
+
=> 1014.92
|
130
|
+
?> my_impact.decisions.carbon.object.units
|
131
|
+
=> "kilograms"
|
132
|
+
?> my_impact.methodology
|
133
|
+
=> "http://impact.brighterplanet.com/flights?[...]"
|
134
|
+
|
135
|
+
See [RDoc on `Carbon#impact`](http://rdoc.info/github/brighterplanet/carbon/Carbon#impact-instance_method) for all the details.
|
94
136
|
|
95
137
|
## API keys
|
96
138
|
|
data/lib/carbon.rb
CHANGED
@@ -169,8 +169,8 @@ module Carbon
|
|
169
169
|
# provide :trips
|
170
170
|
# provide :origin, :as => :origin_airport, :key => :iata_code
|
171
171
|
# provide :destination, :as => :destination_airport, :key => :iata_code
|
172
|
-
# provide(:airline, :key => :iata_code) { |f| f.airline.iata_code }
|
173
|
-
# provide(:aircraft, :key => :icao_code) { { |f| f.aircraft.icao_code }
|
172
|
+
# provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
|
173
|
+
# provide(:aircraft, :key => :icao_code) { { |f| f.aircraft.try(:icao_code) }
|
174
174
|
# end
|
175
175
|
# end
|
176
176
|
def emit_as(emitter, &blk)
|
@@ -194,7 +194,9 @@ module Carbon
|
|
194
194
|
else
|
195
195
|
send method_id
|
196
196
|
end
|
197
|
-
|
197
|
+
if v.present?
|
198
|
+
memo[k] = v
|
199
|
+
end
|
198
200
|
memo
|
199
201
|
end
|
200
202
|
end
|
data/lib/carbon/registry.rb
CHANGED
@@ -2,17 +2,16 @@ require 'singleton'
|
|
2
2
|
|
3
3
|
module Carbon
|
4
4
|
# Used internally to hold the information about how each class that has called `emit_as`.
|
5
|
-
# @private
|
6
5
|
class Registry < ::Hash
|
7
6
|
include ::Singleton
|
8
7
|
|
9
8
|
# Used internally to record the emitter and parameters (characteristics) provided by a class that has called `emit_as`.
|
9
|
+
# Can't use my magic sprinkles (::Struct) because of yardoc
|
10
10
|
# @private
|
11
|
-
class Registration <
|
11
|
+
class Registration < Struct.new(:emitter, :characteristics)
|
12
12
|
end
|
13
13
|
|
14
|
-
# Used internally when instance-eval'ing the
|
15
|
-
# @private
|
14
|
+
# Used internally when instance-eval'ing the +emit_as+ DSL.
|
16
15
|
class Registrar
|
17
16
|
# @private
|
18
17
|
def initialize(klass, emitter)
|
@@ -29,16 +28,20 @@ module Carbon
|
|
29
28
|
# @option translation_options [Symbol] :as (name of the method) If your method name does not match the Brighter Planet characteristic name.
|
30
29
|
# @option translation_options [Symbol] :key (a number of columns) What you are keying on. By default, we do a fuzzy match against a number of fields, including full names and various codes.
|
31
30
|
#
|
31
|
+
# @return [nil]
|
32
|
+
#
|
33
|
+
# @note It's suggested that you use {http://api.rubyonrails.org/classes/Object.html#method-i-try Object#try} to cheaply avoid +undefined method `iata_code` for nil:NilClass+. It will be available because this class includes +active_support/core_ext+ anyway.
|
34
|
+
#
|
32
35
|
# @yield [] Pass a block for the common use case of calling a method on a object.
|
33
36
|
#
|
34
37
|
# @example Your method is named one thing but should be sent +:as+ something else.
|
35
38
|
# provide :my_distance, :as => :distance
|
36
39
|
#
|
37
40
|
# @example You are keying on something well-known like {http://en.wikipedia.org/wiki/Airline_codes IATA airline codes}.
|
38
|
-
# provide(:airline, :key => :iata_code) { |f| f.airline.iata_code }
|
41
|
+
# provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
|
39
42
|
#
|
40
43
|
# @example Better to use a block
|
41
|
-
# provide(:airline, :key => :iata_code) { |f| f.airline.iata_code }
|
44
|
+
# provide(:airline, :key => :iata_code) { |f| f.airline.try(:iata_code) }
|
42
45
|
# # is equivalent to
|
43
46
|
# def airline_iata_code
|
44
47
|
# airline.iata_code
|
data/lib/carbon/version.rb
CHANGED
data/test/carbon_test.rb
CHANGED
@@ -45,12 +45,17 @@ class MyNissanAltima
|
|
45
45
|
def model; 'Altima' end
|
46
46
|
def model_year; @model_year end # what BP knows as "year"
|
47
47
|
def fuel_type; 'R' end # what BP knows as "automobile_fuel" and keys on "code"
|
48
|
+
def nil_make; nil end
|
49
|
+
def nil_model; nil end
|
48
50
|
include Carbon
|
49
51
|
emit_as 'Automobile' do
|
50
|
-
provide(:make) { |my_nissan_altima| my_nissan_altima.make.name }
|
52
|
+
provide(:make) { |my_nissan_altima| my_nissan_altima.make.try(:name) }
|
51
53
|
provide :model
|
52
54
|
provide :model_year, :as => :year
|
53
55
|
provide :fuel_type, :as => :automobile_fuel, :key => :code
|
56
|
+
|
57
|
+
provide(:nil_make) { |my_nissan_altima| my_nissan_altima.nil_make.try(:blam!) }
|
58
|
+
provide :nil_model
|
54
59
|
end
|
55
60
|
end
|
56
61
|
|
@@ -147,6 +152,13 @@ describe Carbon do
|
|
147
152
|
Carbon::Registry.instance['MyFoo'].characteristics.keys.must_equal [:model]
|
148
153
|
end
|
149
154
|
end
|
155
|
+
describe '#impact_params' do
|
156
|
+
it "only takes non-nil params" do
|
157
|
+
a = MyNissanAltima.new(2006)
|
158
|
+
a.impact_params.keys.wont_include :nil_model
|
159
|
+
a.impact_params.keys.wont_include :nil_make
|
160
|
+
end
|
161
|
+
end
|
150
162
|
describe '#impact' do
|
151
163
|
it "works" do
|
152
164
|
impact = MyNissanAltima.new(2006).impact
|
metadata
CHANGED
@@ -1,132 +1,103 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: carbon
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.1
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 2
|
8
|
-
- 0
|
9
|
-
- 0
|
10
|
-
version: 2.0.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Seamus Abshere
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-03-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: em-http-request
|
22
|
-
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &2153238540 !ruby/object:Gem::Requirement
|
24
17
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
32
22
|
type: :runtime
|
33
|
-
version_requirements: *id001
|
34
|
-
- !ruby/object:Gem::Dependency
|
35
|
-
name: activesupport
|
36
23
|
prerelease: false
|
37
|
-
|
24
|
+
version_requirements: *2153238540
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &2153260780 !ruby/object:Gem::Requirement
|
38
28
|
none: false
|
39
|
-
requirements:
|
40
|
-
- -
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
46
33
|
type: :runtime
|
47
|
-
version_requirements: *id002
|
48
|
-
- !ruby/object:Gem::Dependency
|
49
|
-
name: multi_json
|
50
34
|
prerelease: false
|
51
|
-
|
35
|
+
version_requirements: *2153260780
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: multi_json
|
38
|
+
requirement: &2153260300 !ruby/object:Gem::Requirement
|
52
39
|
none: false
|
53
|
-
requirements:
|
54
|
-
- -
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
60
44
|
type: :runtime
|
61
|
-
version_requirements: *id003
|
62
|
-
- !ruby/object:Gem::Dependency
|
63
|
-
name: hashie
|
64
45
|
prerelease: false
|
65
|
-
|
46
|
+
version_requirements: *2153260300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: hashie
|
49
|
+
requirement: &2153259860 !ruby/object:Gem::Requirement
|
66
50
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
|
71
|
-
segments:
|
72
|
-
- 0
|
73
|
-
version: "0"
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
74
55
|
type: :runtime
|
75
|
-
version_requirements: *id004
|
76
|
-
- !ruby/object:Gem::Dependency
|
77
|
-
name: bombshell
|
78
56
|
prerelease: false
|
79
|
-
|
57
|
+
version_requirements: *2153259860
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: bombshell
|
60
|
+
requirement: &2153259340 !ruby/object:Gem::Requirement
|
80
61
|
none: false
|
81
|
-
requirements:
|
82
|
-
- -
|
83
|
-
- !ruby/object:Gem::Version
|
84
|
-
|
85
|
-
segments:
|
86
|
-
- 0
|
87
|
-
version: "0"
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
88
66
|
type: :runtime
|
89
|
-
version_requirements: *id005
|
90
|
-
- !ruby/object:Gem::Dependency
|
91
|
-
name: conversions
|
92
67
|
prerelease: false
|
93
|
-
|
68
|
+
version_requirements: *2153259340
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: conversions
|
71
|
+
requirement: &2153258840 !ruby/object:Gem::Requirement
|
94
72
|
none: false
|
95
|
-
requirements:
|
96
|
-
- -
|
97
|
-
- !ruby/object:Gem::Version
|
98
|
-
|
99
|
-
segments:
|
100
|
-
- 0
|
101
|
-
version: "0"
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
102
77
|
type: :runtime
|
103
|
-
version_requirements: *id006
|
104
|
-
- !ruby/object:Gem::Dependency
|
105
|
-
name: brighter_planet_metadata
|
106
78
|
prerelease: false
|
107
|
-
|
79
|
+
version_requirements: *2153258840
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: brighter_planet_metadata
|
82
|
+
requirement: &2153258380 !ruby/object:Gem::Requirement
|
108
83
|
none: false
|
109
|
-
requirements:
|
110
|
-
- -
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
|
113
|
-
segments:
|
114
|
-
- 0
|
115
|
-
version: "0"
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
116
88
|
type: :runtime
|
117
|
-
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2153258380
|
118
91
|
description: Brighter Planet API client for Ruby
|
119
|
-
email:
|
92
|
+
email:
|
120
93
|
- seamus@abshere.net
|
121
94
|
- dkastner@gmail.com
|
122
95
|
- andy@rossmeissl.net
|
123
|
-
executables:
|
96
|
+
executables:
|
124
97
|
- carbon
|
125
98
|
extensions: []
|
126
|
-
|
127
99
|
extra_rdoc_files: []
|
128
|
-
|
129
|
-
files:
|
100
|
+
files:
|
130
101
|
- .gitignore
|
131
102
|
- CHANGELOG
|
132
103
|
- Gemfile
|
@@ -146,38 +117,29 @@ files:
|
|
146
117
|
- test/carbon_test.rb
|
147
118
|
homepage: https://github.com/brighterplanet/carbon
|
148
119
|
licenses: []
|
149
|
-
|
150
120
|
post_install_message:
|
151
121
|
rdoc_options: []
|
152
|
-
|
153
|
-
require_paths:
|
122
|
+
require_paths:
|
154
123
|
- lib
|
155
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
156
125
|
none: false
|
157
|
-
requirements:
|
158
|
-
- -
|
159
|
-
- !ruby/object:Gem::Version
|
160
|
-
|
161
|
-
|
162
|
-
- 0
|
163
|
-
version: "0"
|
164
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ! '>='
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
165
131
|
none: false
|
166
|
-
requirements:
|
167
|
-
- -
|
168
|
-
- !ruby/object:Gem::Version
|
169
|
-
|
170
|
-
segments:
|
171
|
-
- 0
|
172
|
-
version: "0"
|
132
|
+
requirements:
|
133
|
+
- - ! '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
173
136
|
requirements: []
|
174
|
-
|
175
137
|
rubyforge_project:
|
176
|
-
rubygems_version: 1.8.
|
138
|
+
rubygems_version: 1.8.15
|
177
139
|
signing_key:
|
178
140
|
specification_version: 3
|
179
141
|
summary: Brighter Planet API client for Ruby
|
180
|
-
test_files:
|
142
|
+
test_files:
|
181
143
|
- features/shell.feature
|
182
144
|
- features/support/env.rb
|
183
145
|
- test/carbon_test.rb
|