public
Fork of aflatter/money
Description: Leetsoft's Money library
Homepage:
Clone URL: git://github.com/collectiveidea/money.git
money / README
100644 85 lines (54 sloc) 2.977 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
== Money
 
This library makes it easier to deal with Money values, storing them as integers to avoid floating-point math errors.
 
== Download
 
Preferred method of installation is gem:
 
  gem install --source http://gems.github.com collectiveidea-money
 
You can find the source at:
  
  http://github.com/collectiveidea/money
 
== Rails
 
There is a rails extension that makes it easier to store money values in the database.
 
  class Product < ActiveRecord::Base
    money :price
    validates_numericality_of :price_in_cents, :greater_than => 0
  end
 
This assumes that there is a price_in_cents (integer) column in the database, which can
be changed by passing the :cents option. You can also specify the :currency option to
save the currency to a field in the database.
 
  class Room < ActiveRecord::Base
    money :rate, :cents => :rate_cents, :currency => :rate_currency
    money :discount, :cents => :discount_cents
  end
 
You can set the attribute to a String, Fixnum, or Float and it will call #to_money to
convert it to a Money object. This makes it convenient for using money fields in forms.
 
  r = Room.new :rate => "100.00"
  r.rate # returns <Money:0x249ef9c @currency="USD", @cents=10000>
 
By default, money values will be stored with a precision of 2 (cents). If you need to store different precisions, such as to the nearest tenth of a cent, you can specify the +:precision+ option:
 
  class Room < ActiveRecord::Base
    money :rate, :precision => 3
  end
 
  r = Room.new :rate => "100"
  r.rate.format # returns $100.000
  r.rate = "100.995"
  r.rate.format # returns $100.995
 
To use the Rails functionality, install money as a plugin, or require 'money/rails'.
This version is compatible with Rails 2.2. For compatibility with previous versions of
Rails, check out the rails-2.1 branch.
  
== Class configuration
 
Two const class variables are available to tailor Money to your needs.
If you don't need currency exchange at all, just ignore those.
 
=== Default Currency
 
By default Money defaults to USD as its currency. This can be overwritten using
 
  Money.default_currency = "CAD"
  
If you use rails, the environment.rb is a very good place to put this.
 
=== Currency Exchange
 
The second parameter is a bit more complex. It lets you provide your own implementation of the
currency exchange service. By default Money throws an exception when trying to call .exchange_to.
 
A second minimalist implementation is provided which lets you supply custom exchange rates:
 
  Money.bank = VariableExchangeBank.new
  Money.bank.add_rate("USD", "CAD", 1.24515)
  Money.bank.add_rate("CAD", "USD", 0.803115)
  Money.us_dollar(100).exchange_to("CAD") => Money.ca_dollar(124)
  Money.ca_dollar(100).exchange_to("USD") => Money.us_dollar(80)
 
There is nothing stopping you from creating bank objects which scrape www.xe.com for the current rates or just return rand(2)
  
== Code
 
If you have any improvements please email them to tobi [at] leetsoft.com