co2_equivalents 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +11 -0
- data/.gitignore +40 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.markdown +59 -0
- data/Rakefile +42 -0
- data/co2_equivalents.gemspec +36 -0
- data/lib/co2_equivalents.rb +27 -0
- data/lib/co2_equivalents/citation.rb +12 -0
- data/lib/co2_equivalents/equivalent.rb +13 -0
- data/lib/co2_equivalents/group.rb +38 -0
- data/lib/co2_equivalents/measurements.rb +11 -0
- data/lib/co2_equivalents/measurements/driving.rb +20 -0
- data/lib/co2_equivalents/measurements/flight.rb +13 -0
- data/lib/co2_equivalents/measurements/food.rb +14 -0
- data/lib/co2_equivalents/measurements/fuel.rb +12 -0
- data/lib/co2_equivalents/measurements/home_energy.rb +25 -0
- data/lib/co2_equivalents/measurements/lightbulbs.rb +23 -0
- data/lib/co2_equivalents/measurements/recycling.rb +13 -0
- data/lib/co2_equivalents/result_set.rb +24 -0
- data/lib/co2_equivalents/version.rb +3 -0
- data/rspec.watchr +30 -0
- data/spec/co2_equivalents/group_spec.rb +61 -0
- data/spec/co2_equivalents/result_set_spec.rb +25 -0
- data/spec/co2_equivalents_spec.rb +30 -0
- data/spec/spec_helper.rb +19 -0
- metadata +158 -0
data/.document
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# .document is used by rdoc and yard to know how to generate documentation
|
2
|
+
# for example, it can be used to control how rdoc gets built when you do `gem install foo`
|
3
|
+
|
4
|
+
README.rdoc
|
5
|
+
lib/**/*.rb
|
6
|
+
bin/*
|
7
|
+
|
8
|
+
# Files below this - are treated as 'extra files', and aren't parsed for ruby code
|
9
|
+
-
|
10
|
+
features/**/*.feature
|
11
|
+
LICENSE
|
data/.gitignore
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
*.gem
|
2
|
+
.bundle
|
3
|
+
Gemfile.lock
|
4
|
+
pkg/*
|
5
|
+
# rcov generated
|
6
|
+
coverage
|
7
|
+
|
8
|
+
# rdoc generated
|
9
|
+
rdoc
|
10
|
+
|
11
|
+
# yard generated
|
12
|
+
doc
|
13
|
+
.yardoc
|
14
|
+
|
15
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
16
|
+
#
|
17
|
+
# * Create a file at ~/.gitignore
|
18
|
+
# * Include files you want ignored
|
19
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
20
|
+
#
|
21
|
+
# After doing this, these files will be ignored in all your git projects,
|
22
|
+
# saving you from having to 'pollute' every project you touch with them
|
23
|
+
#
|
24
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
25
|
+
#
|
26
|
+
# For MacOS:
|
27
|
+
#
|
28
|
+
#.DS_Store
|
29
|
+
#
|
30
|
+
# For TextMate
|
31
|
+
#*.tmproj
|
32
|
+
#tmtags
|
33
|
+
#
|
34
|
+
# For emacs:
|
35
|
+
#*~
|
36
|
+
#\#*
|
37
|
+
#.\#*
|
38
|
+
#
|
39
|
+
# For vim:
|
40
|
+
#*.swp
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Derek Kastner
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# CO2 Equivalents
|
2
|
+
|
3
|
+
`co2_equivalents` converts CO2 measurements into human-[grok](http://en.wikipedia.org/wiki/Grok)able units. For instance, you can convert kilograms of CO2 into "number of cars on the road for a day." It also adds the ability to list out a group of equivalents.
|
4
|
+
|
5
|
+
# Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require 'co2_equivalents'
|
9
|
+
|
10
|
+
# simple unit conversion
|
11
|
+
puts CO2Equivalents.grok(2840.7, :cars_driven_for_a_day).to_f
|
12
|
+
#=> 343.6
|
13
|
+
|
14
|
+
# return the default hash of equivalents
|
15
|
+
puts CO2Equivalents.grok(2840.7).inspect
|
16
|
+
#=> {
|
17
|
+
:light_bulbs_for_a_year: 1234.5,
|
18
|
+
:light_bulbs_for_a_day: 454534.5,
|
19
|
+
...
|
20
|
+
:cars_driven_for_a_day: 343.6
|
21
|
+
}
|
22
|
+
|
23
|
+
# return a hash of equivalents for the electricity category
|
24
|
+
puts CO2Equivalents.grok(2840.7, :electricity).inspect
|
25
|
+
#=> {
|
26
|
+
:light_bulbs_for_a_year: 1234.5,
|
27
|
+
:light_bulbs_for_a_day: 454534.5,
|
28
|
+
...
|
29
|
+
}
|
30
|
+
|
31
|
+
# defining your own equivalents
|
32
|
+
class SemiSubmersible < CO2Equivalents::Group
|
33
|
+
description 'Conversions for semi-submersible vehicles, such as oil platforms'
|
34
|
+
citation 'http://shipinfo.org/index4.htm', :title => 'Ship Information Warehouse Statistics', :author => 'John Doe'
|
35
|
+
|
36
|
+
equivalent :mobile_offshore_drilling_unit_operations_for_a_day, 0.465
|
37
|
+
equivalent :mobile_offshore_drilling_unit_operations_for_a_year, 169.725
|
38
|
+
equivalent :semi_submersible_crane_vessel_operations_for_a_day, 0.572
|
39
|
+
equivalent :semi_submersible_crane_vessel_operations_for_a_year, 208.78
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
You can view the default set of equivalents in [lib/co2_equivalents/measurements](http://github.com/dkastner/co2_equivalents/tree/master/lib/co2_equivalents/measurements).
|
44
|
+
|
45
|
+
Each equivalence definition means "1 kg of CO2 is equal to X amount of my custom unit," e.g. 1 kg CO2 is equal to 0.465 Mobile Offshore Drilling Unit operations for a day.
|
46
|
+
|
47
|
+
# Note on Patches/Pull Requests
|
48
|
+
|
49
|
+
* Fork the project.
|
50
|
+
* Make your feature addition or bug fix.
|
51
|
+
* Add tests for it. This is important so I don't break it in a
|
52
|
+
future version unintentionally.
|
53
|
+
* Commit, do not mess with rakefile, version, or history.
|
54
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
55
|
+
* Send me a pull request. Bonus points for topic branches.
|
56
|
+
|
57
|
+
# Copyright
|
58
|
+
|
59
|
+
Copyright (c) 2011 Derek Kastner. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler'
|
5
|
+
rescue LoadError
|
6
|
+
$stderr.puts "You must install bundler - run `gem install bundler`"
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
Bundler.setup
|
11
|
+
rescue Bundler::BundlerError => e
|
12
|
+
$stderr.puts e.message
|
13
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
14
|
+
exit e.status_code
|
15
|
+
end
|
16
|
+
require 'rake'
|
17
|
+
|
18
|
+
require 'bueller'
|
19
|
+
Bueller::Tasks.new
|
20
|
+
|
21
|
+
require 'rspec/core/rake_task'
|
22
|
+
RSpec::Core::RakeTask.new(:examples) do |examples|
|
23
|
+
examples.rspec_opts = '-Ispec'
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
27
|
+
spec.rspec_opts = '-Ispec'
|
28
|
+
spec.rcov = true
|
29
|
+
end
|
30
|
+
|
31
|
+
task :default => :examples
|
32
|
+
|
33
|
+
require 'rake/rdoctask'
|
34
|
+
Rake::RDocTask.new do |rdoc|
|
35
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
36
|
+
|
37
|
+
rdoc.main = 'README.rdoc'
|
38
|
+
rdoc.rdoc_dir = 'rdoc'
|
39
|
+
rdoc.title = "equivalents #{version}"
|
40
|
+
rdoc.rdoc_files.include('README*')
|
41
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
42
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
require 'co2_equivalents/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'co2_equivalents'
|
6
|
+
s.version = CO2Equivalents::VERSION
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.date = '2011-06-06'
|
9
|
+
s.authors = ['Derek Kastner']
|
10
|
+
s.email = 'dkastner@gmail.com'
|
11
|
+
s.homepage = 'http://github.com/dkastner/equivalents'
|
12
|
+
s.summary = %Q{Convert everyday units into human-grokable terms}
|
13
|
+
s.description = %Q{CO2Equivalents converts everyday units of measurement into human-grokable units. For instance, you can convert kilograms of CO2 into "number of cars on the road for a day." It also adds the ability to list out a group of equivalences.}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
'LICENSE',
|
16
|
+
'README.markdown',
|
17
|
+
]
|
18
|
+
|
19
|
+
s.required_rubygems_version = Gem::Requirement.new('>= 1.3.7')
|
20
|
+
s.rubygems_version = '1.3.7'
|
21
|
+
s.specification_version = 3
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ['lib']
|
27
|
+
|
28
|
+
s.add_development_dependency 'rspec'
|
29
|
+
s.add_development_dependency 'bundler'
|
30
|
+
s.add_development_dependency 'bueller'
|
31
|
+
s.add_development_dependency 'rake'
|
32
|
+
s.add_development_dependency 'rcov'
|
33
|
+
s.add_dependency 'activesupport'
|
34
|
+
s.add_dependency 'i18n'
|
35
|
+
end
|
36
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'co2_equivalents/citation'
|
2
|
+
require 'co2_equivalents/equivalent'
|
3
|
+
require 'co2_equivalents/group'
|
4
|
+
require 'co2_equivalents/result_set'
|
5
|
+
|
6
|
+
module CO2Equivalents
|
7
|
+
class << self
|
8
|
+
def register(*args)
|
9
|
+
equivalent = Equivalent.new *args
|
10
|
+
registry[equivalent.name] = equivalent
|
11
|
+
end
|
12
|
+
|
13
|
+
def registry
|
14
|
+
@registry ||= {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def grok(kgs_co2, *args)
|
18
|
+
ResultSet.compute kgs_co2, *args
|
19
|
+
end
|
20
|
+
|
21
|
+
def groups
|
22
|
+
@groups ||= []
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'co2_equivalents/measurements'
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'active_support/core_ext/string/conversions'
|
2
|
+
|
3
|
+
module CO2Equivalents
|
4
|
+
class Equivalent
|
5
|
+
attr_accessor :group, :name, :factor
|
6
|
+
|
7
|
+
def initialize(group, name, factor)
|
8
|
+
self.group = group.to_s.underscore.to_sym
|
9
|
+
self.name = name
|
10
|
+
self.factor = factor
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
class Group
|
3
|
+
class << self
|
4
|
+
def inherited(subclass)
|
5
|
+
CO2Equivalents.groups.push subclass
|
6
|
+
end
|
7
|
+
|
8
|
+
def description(value = nil)
|
9
|
+
if value
|
10
|
+
@description = value
|
11
|
+
end
|
12
|
+
@description
|
13
|
+
end
|
14
|
+
|
15
|
+
def citation(*args)
|
16
|
+
@citations ||= []
|
17
|
+
citation = Citation.new *args
|
18
|
+
@citations.push(citation)
|
19
|
+
citation
|
20
|
+
end
|
21
|
+
|
22
|
+
def citations
|
23
|
+
@citations
|
24
|
+
end
|
25
|
+
|
26
|
+
def equivalent(*args)
|
27
|
+
args.unshift self
|
28
|
+
equivalent = CO2Equivalents.register *args
|
29
|
+
equivalences[equivalent.name] = equivalent
|
30
|
+
equivalent
|
31
|
+
end
|
32
|
+
|
33
|
+
def equivalences
|
34
|
+
@equivalences ||= {}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'co2_equivalents/measurements/driving'
|
2
|
+
require 'co2_equivalents/measurements/flight'
|
3
|
+
require 'co2_equivalents/measurements/food'
|
4
|
+
require 'co2_equivalents/measurements/fuel'
|
5
|
+
require 'co2_equivalents/measurements/home_energy'
|
6
|
+
require 'co2_equivalents/measurements/lightbulbs'
|
7
|
+
require 'co2_equivalents/measurements/recycling'
|
8
|
+
|
9
|
+
module CO2Equivalents
|
10
|
+
module Measurements; end
|
11
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Driving < CO2Equivalents::Group
|
4
|
+
description 'Average automobile travel'
|
5
|
+
citation 'http://www.epa.gov/otaq/climate/420f05004.htm'
|
6
|
+
citation 'http://www.bts.gov/publications/national_transportation_statistics/html/table_04_23.html'
|
7
|
+
citation 'http://www.fueleconomy.gov/feg/2008car1tablef.jsp?id=24882'
|
8
|
+
citation 'http://abcnews.go.com/Technology/Traffic/story?id=485098'
|
9
|
+
|
10
|
+
equivalent :cars_off_the_road_for_a_year, 0.000182 # taking X cars off the road for a year
|
11
|
+
equivalent :cars_off_the_road_for_a_month, 0.002182
|
12
|
+
equivalent :cars_off_the_road_for_a_week, 0.009455
|
13
|
+
equivalent :cars_off_the_road_for_a_day, 0.066366
|
14
|
+
equivalent :cars_to_priuses_for_a_year, 0.000364 #switching X cars to Priuses for a year
|
15
|
+
equivalent :cars_to_priuses_for_a_month, 0.004364
|
16
|
+
equivalent :cars_to_priuses_for_a_week, 0.018910
|
17
|
+
equivalent :cars_to_priuses_for_a_day, 0.132732
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Flight < CO2Equivalents::Group
|
4
|
+
description 'Average flight emissions'
|
5
|
+
citation 'http://www.bts.gov/press_releases/2007/bts042_07/html/bts042_07.html#table_7', :title => 'June 2007 Airline Traffic Data', :author => 'Dave Smallen'
|
6
|
+
|
7
|
+
equivalent :one_way_domestic_flight, 0.003250
|
8
|
+
equivalent :round_trip_domestic_flight, 0.001625
|
9
|
+
equivalent :one_way_cross_country_flight, 0.001142
|
10
|
+
equivalent :round_trip_cross_country_flight, 0.000571
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Food < CO2Equivalents::Group
|
4
|
+
description 'Average food consumption'
|
5
|
+
citation 'http://switchboard.nrdc.org/blogs/dlashof/prius_v_vegan.html'
|
6
|
+
|
7
|
+
equivalent :vegan_meals_instead_of_non_vegan_ones, 0.804687
|
8
|
+
equivalent :days_of_veganism, 0.268229
|
9
|
+
equivalent :weeks_of_veganism, 0.038318
|
10
|
+
equivalent :months_of_veganism, 0.008941
|
11
|
+
equivalent :years_of_veganism, 0.000735
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Fuel < CO2Equivalents::Group
|
4
|
+
description 'Average fuel consumption'
|
5
|
+
citation 'http://www.epa.gov/cleanenergy/energy-resources/refs.html'
|
6
|
+
|
7
|
+
equivalent :barrels_of_petroleum, 0.002326
|
8
|
+
equivalent :canisters_of_bbq_propane, 0.041667
|
9
|
+
equivalent :railroad_cars_full_of_coal, 0.000005
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class HomeEnergy < CO2Equivalents::Group
|
4
|
+
description 'Average home energy usage'
|
5
|
+
citation 'http://www.eia.doe.gov/cneaf/electricity/epa/epates.html'
|
6
|
+
citation 'http://www.epa.gov/climatechange/emissions/ind_calculator.html'
|
7
|
+
citation 'http://buildingsdatabook.eren.doe.gov/docs/7.3.2.pdf'
|
8
|
+
citation 'http://ecomall.com/greenshopping/20things.htm'
|
9
|
+
citation 'http://www.oag.state.ny.us/consumer/tips/energy_conservation.html'
|
10
|
+
|
11
|
+
equivalent :homes_energy_in_a_year, 0.000097
|
12
|
+
equivalent :homes_energy_in_a_month, 0.001159
|
13
|
+
equivalent :homes_energy_in_a_week, 0.005021
|
14
|
+
equivalent :homes_energy_in_a_day, 0.035247
|
15
|
+
equivalent :homes_electricity_in_a_year, 0.000147
|
16
|
+
equivalent :homes_electricity_in_a_month, 0.001759
|
17
|
+
equivalent :homes_electricity_in_a_week, 0.007622
|
18
|
+
equivalent :homes_electricity_in_a_day, 0.053499
|
19
|
+
equivalent :homes_with_lowered_thermostat_2_degrees_for_a_winter, 0.005249
|
20
|
+
equivalent :homes_with_raised_thermostat_3_degrees_for_a_summer, 0.002360
|
21
|
+
equivalent :replaced_refrigerators, 0.001007 # old (1970s) refigerators replaced with new, energy efficient ones
|
22
|
+
equivalent :loads_of_cold_laundry, 0.458562
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Lightbulbs < CO2Equivalents::Group
|
4
|
+
description 'Average lightbulb energy usage'
|
5
|
+
citation 'http://www.popularmechanics.com/home_journal/home_improvement/4215199.html'
|
6
|
+
citation 'http://www.energystar.gov/ia/partners/promotions/change_light/downloads/CALFacts_and_Assumptions.pdf'
|
7
|
+
|
8
|
+
equivalent :lightbulbs_for_a_year, 0.001846
|
9
|
+
equivalent :lightbulbs_for_a_month, 0.022465
|
10
|
+
equivalent :lightbulbs_for_a_week, 0.096278
|
11
|
+
equivalent :lightbulbs_for_a_day, 0.673949
|
12
|
+
equivalent :lightbulbs_for_an_evening, 4.043695
|
13
|
+
equivalent :lightbulbs_to_CFLs_for_a_day, 11.471476
|
14
|
+
equivalent :lightbulbs_to_CFLs_for_a_week, 1.638782
|
15
|
+
equivalent :lightbulbs_to_CFLs_for_a_month, 0.382383
|
16
|
+
equivalent :lightbulbs_to_CFLs_for_a_year, 0.031429
|
17
|
+
equivalent :days_with_lightbulbs_to_CFLs, 0.254922
|
18
|
+
equivalent :weeks_with_lightbulbs_to_CFLs, 0.036417
|
19
|
+
equivalent :months_with_lightbulbs_to_CFLs, 0.008497
|
20
|
+
equivalent :years_with_lightbulbs_to_CFLs, 0.000698
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
module Measurements
|
3
|
+
class Recyling < CO2Equivalents::Group
|
4
|
+
description 'Energy saved by recycling'
|
5
|
+
citation 'http://www.epa.gov/cleanenergy/energy-resources/refs.html'
|
6
|
+
citation 'http://onetonco2.com/oneton.php'
|
7
|
+
citation 'http://www.epa.gov/epaoswer/non-hw/muncpl/pubs/msw06.pdf'
|
8
|
+
|
9
|
+
equivalent :recycled_kgs_of_trash, 0.689655
|
10
|
+
equivalent :recycled_bags_of_trash, 0.382135
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module CO2Equivalents
|
2
|
+
class ResultSet < Hash
|
3
|
+
def self.compute(kgs_co2, *args)
|
4
|
+
set = new kgs_co2, *args
|
5
|
+
set.compute
|
6
|
+
set
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_accessor :kgs_co2, :constraints
|
10
|
+
|
11
|
+
def initialize(kgs_co2, *args)
|
12
|
+
self.kgs_co2 = kgs_co2
|
13
|
+
self.constraints = args
|
14
|
+
end
|
15
|
+
|
16
|
+
def compute
|
17
|
+
CO2Equivalents.groups.each do |group|
|
18
|
+
group.equivalences.each do |name, equivalent|
|
19
|
+
self[name] = kgs_co2.to_f * equivalent.factor
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/rspec.watchr
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# Run me with:
|
2
|
+
# $ watchr rspec.watchr
|
3
|
+
|
4
|
+
# --------------------------------------------------
|
5
|
+
# Rules
|
6
|
+
# --------------------------------------------------
|
7
|
+
watch( '^spec/.*_spec\.rb' ) { |m| rspec m[0] }
|
8
|
+
watch( '^lib/(.*)\.rb' ) { |m| rspec "spec/#{m[1]}_spec.rb" }
|
9
|
+
watch( '^spec/spec_helper\.rb' ) { rspec specs }
|
10
|
+
|
11
|
+
# --------------------------------------------------
|
12
|
+
# Signal Handling
|
13
|
+
# --------------------------------------------------
|
14
|
+
Signal.trap('QUIT') { rspec specs } # Ctrl-\
|
15
|
+
Signal.trap('INT' ) { abort("\n") } # Ctrl-C
|
16
|
+
|
17
|
+
# --------------------------------------------------
|
18
|
+
# Helpers
|
19
|
+
# --------------------------------------------------
|
20
|
+
def rspec(*paths)
|
21
|
+
paths = paths.flatten.select { |p| File.exist?(p) }
|
22
|
+
unless paths.empty?
|
23
|
+
puts "bundle exec rspec #{paths.join(' ')}"
|
24
|
+
puts `bundle exec rspec #{paths.join(' ')}`
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def specs
|
29
|
+
Dir['spec/**/*_spec.rb']
|
30
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Firework < CO2Equivalents::Group
|
4
|
+
description 'Pretty'
|
5
|
+
citation 'http://boom.com', :title => 'Sparkle', :author => 'John Doe'
|
6
|
+
|
7
|
+
equivalent :firecracker, 0.465
|
8
|
+
equivalent :roman_candle, 169.725
|
9
|
+
equivalent :sparkler, 0.572
|
10
|
+
equivalent :whipper_snapper, 208.78
|
11
|
+
end
|
12
|
+
|
13
|
+
describe CO2Equivalents::Group do
|
14
|
+
it 'defines an equivalence group' do
|
15
|
+
Firework.description.should == 'Pretty'
|
16
|
+
citation = Firework.citations.first
|
17
|
+
citation.url.should == 'http://boom.com'
|
18
|
+
citation.title.should == 'Sparkle'
|
19
|
+
citation.author.should == 'John Doe'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'tracks the group in a global list' do
|
23
|
+
CO2Equivalents.groups.should include(Firework)
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '.description' do
|
27
|
+
it 'sets a description' do
|
28
|
+
c = Class.new CO2Equivalents::Group
|
29
|
+
c.description 'Testing'
|
30
|
+
c.description.should == 'Testing'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '.citation' do
|
35
|
+
it 'creates a citation' do
|
36
|
+
c = Class.new CO2Equivalents::Group
|
37
|
+
c.citation 'http://example.com', :title => 'Title', :author => 'John Doe'
|
38
|
+
citation = c.citations.first
|
39
|
+
citation.should be_a(CO2Equivalents::Citation)
|
40
|
+
citation.url.should == 'http://example.com'
|
41
|
+
citation.title.should == 'Title'
|
42
|
+
citation.author.should == 'John Doe'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '.equivalent' do
|
47
|
+
it 'creates an equivalent' do
|
48
|
+
c = Class.new CO2Equivalents::Group
|
49
|
+
c.equivalent :test_equivalent, 0.001
|
50
|
+
CO2Equivalents.registry[:test_equivalent].should be_a(CO2Equivalents::Equivalent)
|
51
|
+
end
|
52
|
+
it 'keeps track of which equivalences belong to the group' do
|
53
|
+
c = Class.new CO2Equivalents::Group
|
54
|
+
c.equivalent :test_a, 0.001
|
55
|
+
c.equivalent :test_b, 0.002
|
56
|
+
c.equivalences[:test_a].factor.should == 0.001
|
57
|
+
c.equivalences[:test_b].factor.should == 0.002
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CO2Equivalents::ResultSet do
|
4
|
+
describe '.compute' do
|
5
|
+
it 'returns a computed ResultSet' do
|
6
|
+
result = CO2Equivalents::ResultSet.compute(344)
|
7
|
+
result.should be_a(CO2Equivalents::ResultSet)
|
8
|
+
result.should_not be_empty
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
describe '#compute' do
|
13
|
+
it 'popultes the results for all groups' do
|
14
|
+
set = CO2Equivalents::ResultSet.new 322
|
15
|
+
set.compute
|
16
|
+
set.should_not be_empty
|
17
|
+
set.each do |name, value|
|
18
|
+
value.should > 0
|
19
|
+
end
|
20
|
+
end
|
21
|
+
it 'populates results for specified groups'
|
22
|
+
it 'populates results for specified units'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe CO2Equivalents do
|
4
|
+
describe '.register' do
|
5
|
+
it 'registers an equivalent' do
|
6
|
+
CO2Equivalents.register 'Computer', :test_eq, 0.013
|
7
|
+
equivalent = CO2Equivalents.registry[:test_eq]
|
8
|
+
equivalent.should be_a(CO2Equivalents::Equivalent)
|
9
|
+
equivalent.group.should == :computer
|
10
|
+
equivalent.name.should == :test_eq
|
11
|
+
equivalent.factor.should == 0.013
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '.grok' do
|
16
|
+
it 'returns the result of an equivalence calculation' do
|
17
|
+
result_set = CO2Equivalents.grok(334.5)
|
18
|
+
result_set.should be_a(CO2Equivalents::ResultSet)
|
19
|
+
result_set[:days_of_veganism].should > 0
|
20
|
+
end
|
21
|
+
it 'returns correct results' do
|
22
|
+
CO2Equivalents.grok(1300)[:one_way_domestic_flight].to_i.should == 4
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'limits results to a chosen category'
|
26
|
+
it 'limits results to a set of chosen categories'
|
27
|
+
it 'limits results to a chosen unit'
|
28
|
+
it 'limits results to a set of chosen units'
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
begin
|
3
|
+
Bundler.setup
|
4
|
+
rescue Bundler::BundlerError => e
|
5
|
+
$stderr.puts e.message
|
6
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
7
|
+
exit e.status_code
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rspec'
|
11
|
+
require 'co2_equivalents'
|
12
|
+
|
13
|
+
# Requires supporting files with custom matchers and macros, etc,
|
14
|
+
# in ./support/ and its subdirectories.
|
15
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: co2_equivalents
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Derek Kastner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-06-06 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &2154797340 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2154797340
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bundler
|
27
|
+
requirement: &2154796880 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2154796880
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: bueller
|
38
|
+
requirement: &2154796460 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2154796460
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &2154796040 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2154796040
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rcov
|
60
|
+
requirement: &2154795620 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *2154795620
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activesupport
|
71
|
+
requirement: &2154795200 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :runtime
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2154795200
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: i18n
|
82
|
+
requirement: &2154794780 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :runtime
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2154794780
|
91
|
+
description: CO2Equivalents converts everyday units of measurement into human-grokable
|
92
|
+
units. For instance, you can convert kilograms of CO2 into "number of cars on the
|
93
|
+
road for a day." It also adds the ability to list out a group of equivalences.
|
94
|
+
email: dkastner@gmail.com
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files:
|
98
|
+
- LICENSE
|
99
|
+
- README.markdown
|
100
|
+
files:
|
101
|
+
- .document
|
102
|
+
- .gitignore
|
103
|
+
- Gemfile
|
104
|
+
- LICENSE
|
105
|
+
- README.markdown
|
106
|
+
- Rakefile
|
107
|
+
- co2_equivalents.gemspec
|
108
|
+
- lib/co2_equivalents.rb
|
109
|
+
- lib/co2_equivalents/citation.rb
|
110
|
+
- lib/co2_equivalents/equivalent.rb
|
111
|
+
- lib/co2_equivalents/group.rb
|
112
|
+
- lib/co2_equivalents/measurements.rb
|
113
|
+
- lib/co2_equivalents/measurements/driving.rb
|
114
|
+
- lib/co2_equivalents/measurements/flight.rb
|
115
|
+
- lib/co2_equivalents/measurements/food.rb
|
116
|
+
- lib/co2_equivalents/measurements/fuel.rb
|
117
|
+
- lib/co2_equivalents/measurements/home_energy.rb
|
118
|
+
- lib/co2_equivalents/measurements/lightbulbs.rb
|
119
|
+
- lib/co2_equivalents/measurements/recycling.rb
|
120
|
+
- lib/co2_equivalents/result_set.rb
|
121
|
+
- lib/co2_equivalents/version.rb
|
122
|
+
- rspec.watchr
|
123
|
+
- spec/co2_equivalents/group_spec.rb
|
124
|
+
- spec/co2_equivalents/result_set_spec.rb
|
125
|
+
- spec/co2_equivalents_spec.rb
|
126
|
+
- spec/spec_helper.rb
|
127
|
+
homepage: http://github.com/dkastner/equivalents
|
128
|
+
licenses: []
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
segments:
|
140
|
+
- 0
|
141
|
+
hash: -2761317064590986918
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: 1.3.7
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 1.8.5
|
151
|
+
signing_key:
|
152
|
+
specification_version: 3
|
153
|
+
summary: Convert everyday units into human-grokable terms
|
154
|
+
test_files:
|
155
|
+
- spec/co2_equivalents/group_spec.rb
|
156
|
+
- spec/co2_equivalents/result_set_spec.rb
|
157
|
+
- spec/co2_equivalents_spec.rb
|
158
|
+
- spec/spec_helper.rb
|