HTTPS clone URL
Subversion checkout URL
Scrooge will keep all your money safe. Or at least in the database.
Fetching latest commit…
Cannot retrieve the latest commit at this time
|Failed to load latest commit information.|
Scrooge ========= Make it easy to store money values in an ActiveRecord model, avoiding the annoying floating point math. The idea is to store the monetary values as the amount of cents in the database to avoid the math. class Product < ActiveRecord::Base attr_cents :price end product = Product.new(:price => 10.99) product.price #=> #<Scrooge::Money @cents="1099"> The Scrooge::Money objects will act as a Numeric in all regards, so you can do math with other numbers easily. The `products` table should have an integer column `price_in_cents` for this to work. Calculations ------------ If you need aggregated calculations on your model, you can use the provided method Numeric#as_cents, which converts a number to a Scrooge::Money instance, assuming that the number is already the number of cents. For example: class Sale < ActiveRecord::Base belongs_to :product attr_cents :value end class Product < ActiveRecord::Base has_many :sales def gross_sales sales.sum(:value_in_cents).as_cents end end product = Product.create product.sales.create(:value => 20) product.sales.create(:value => 20.5) product.sales.create(:value => 25) product.gross_sales #=> #<Scrooge::Money @cents="6550"> product.gross_sales == 65.5 #=> true Why? ==== Because we only needed this, and most of the gems that do this stuff are too damn big and have a ton of un-needed functionality. Install ======= gem install scrooge Then, in order to require it, if you want the ActiveRecord helper methods: require "scrooge/active_record" Or: config.gem "scrooge" License ====== (The MIT License) Copyright (c) 2010 Nicolas Sanguinetti, http://nicolassanguinetti.info Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.