0.0
No commit activity in last 3 years
No release in over 3 years
This library aids one in handling money and different currencies.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.8.0
>= 1.3.0
 Project Readme

Money $¶ ↑

This library aids one in handling money and different currencies. Features:

  • Provides a Money class which encapsulates all information about an certain amount of money, such as its value and its currency.

  • Represents monetary values as integers, in cents. This avoids floating point rounding errors.

  • Provides APIs for exchanging money from one currency to another.

  • Has the ability to parse a money string into a Money object.

  • Provides ActiveRecord “has_money” method.

Resources:

Download¶ ↑

Install stable releases with the following command:

gem install money

The development version (hosted on Github) can be installed with:

gem sources -a http://gems.github.com
gem install samlown-money

Usage¶ ↑

Synopsis¶ ↑

require 'money'

# 10.00 USD
money = Money.new(1000, "USD")
money.cents     # => 1000
money.currency  # => "USD"
money.format    # => "$10.00"

Money.new(880088, "EUR").format # => €8,800.88
Money.new(-8000).format(:no_cents => true)  # => $-80

Money.new(1000, "USD") == Money.new(1000, "USD")   # => true
Money.new(1000, "USD") == Money.new( 100, "USD")   # => false
Money.new(1000, "USD") == Money.new(1000, "EUR")   # => false

# Comparisons with other numbers
Money.new(10.10) > 10.0     # => true
Money.new(1001) > 10        # => true
Money.new(999) > 10         # => false

Currency Exchange¶ ↑

Exchanging money is performed through an exchange bank object. The default exchange bank object requires one to manually specify the exchange rate. Here’s an example of how it works:

Money.add_rate("CAD", 0.803115)
Money.add_rate("USD", 1.24515)

Money.us_dollar(100_00).exchange_to("CAD")  # => Money.new(15504, "CAD")
Money.ca_dollar(100_00).exchange_to("USD")  # => Money.new(6450, "USD")

or

Money.us_dollar(100).as_cad              # => Money.new(155, "CAD")
Money.ca_dollar(100).as_usd              # => Money.new(64, "USD")

Comparison and arithmetic operations work as expected:

Money.new(1000, "USD") <=> Money.new(900, "USD")   # => 1; 9.00 USD is smaller
Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")

Money.add_rate("EUR", 0.5)
Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")

Fetch the exchange rates published by the European Bank

Money.default_bank.fetch_rates          # Fetch the rates
Money.default_bank.auto_fetch 3600      # Fetch the rates every hour
Money.default_bank.stop_fetch           # Stop auto-fetch

There is nothing stopping you from creating bank objects which scrapes www.xe.com for the current rates or just returns rand(2):

Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new

Ruby on Rails¶ ↑

Use the has_money method to embed the money object in your models. The following example requires a price_cents and a price_currency fields on the database.

config/enviroment.rb

require.gem 'ShadowBelmolve-money', :lib => 'money'

app/models/product.rb

class Product < ActiveRecord::Base
  belongs_to :product
  has_money :price

  validates_numericality_of :price_cents, :greater_than => 0
end

migration:

create_table :products do |t|
  t.integer :price_cents
  t.string  :price_currency
end

Default Currency¶ ↑

By default Money defaults to USD as its currency. This can be overwritten using:

Money.default_currency = "CAD"

If you use Rails, then environment.rb is a very good place to put this.

TODO¶ ↑

  • Better validation (fix composed_of allow_nil)

  • Interest (almost there..)

  • Remote rate fetching