The purchase gem is used by Brighter Planet's Carbon Middleware platform to define a mathematical emissions model for a purchase activity. In addition, this gem defines how the model's calculation is displayed in a human readable format.
Contributions are welcome and encouraged, as we believe that help from the scientific and developer communities will help us provide correct and transparent models of greenhouse gas-emitting activities. See the section on the Collaboration Cycle below.
The purchase gem defines a module, BrighterPlanet::Purchase that should be included into an ActiveRecord model. Calling #emission on an instance of the model will determine the amount of emissions (CO2e) generated by the activity.
class PurchaseRecord < ActiveRecord::Base include BrighterPlanet::Purchase ... end p = PurchaseRecord.new :merchant_category_code => 2324, :amount => 873.45 p.emission # => 2472
Purchase uses bundler to manage dependencies. To prepare your environment, perform the following: git clone git://github.com/brighterplanet/purchase.git cd purchase gem install bundler (note: prior to bundler 1.0 release, add --pre) bundle install
RVM is a great tool for testing and development as it can create independent gem environments.
Included in the repository is a .rvmrc that will automatically create a ruby 1.8.7 gemset for you named purchase. If you already have a gemset with the same name, simply replace the `pwd` within the .rvmrc to whatever name you want.
Our goal is to have sufficient testing coverage for our emissions models. We have chosen to write our tests using cucumber as the table-formatted testing fits well with our calculations tests and the plain-english stories lower the barrier to entry for programmers of all skill levels.
There are two main features:
- purchase_emissions.feature, which tests emission values for various inputs
- purchase_committees.feature, which tests sub-calculations used within the model
To run all tests:
To run individual tests:
To run a specific scenario:
The sniff gem is used to provide a testing environment that loads dependent data models (defined in the earth gem), a database, and testing fixtures.
The earth gem contains the model definitions for dependent data models. For instance, this is zip code data or fuel prices used in the emissions calculations. Earth will look for any test fixtures defined in lib/test_support/db/fixtures. These sample records are in csv format and are imported into the testing database.
See the README for the sniff gem: http://github.com/brighterplanet/sniff/blob/master/README.markdown
Brighter Planet vigorously encourages collaborative improvement of its emitter libraries. Collaboration requires a (free) GitHub account.
- Fork the emitter repository on GitHub.
- Write a test proving the existing implementation's inadequacy. Ensure that the test fails. Commit the test.
- Improve the code until your new test passes and commit your changes.
- Push your changes to your GitHub fork.
- Submit a pull request to brighterplanet.
- Receive a pull request.
- Pull changes from forked repository.
- Ensure tests pass.
- Review changes for scientific accuracy.
- Merge changes to master repository and publish.
- Direct production environment to use new emitter version.
Copyright © 2010 Brighter Planet. See LICENSE for details. Contact Brighter Planet for dual-license arrangements.