money 3.6.1 → 3.6.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- Copyright (c) 2005 Tobias Lutke
2
- Copyright (c) 2008 Phusion
3
-
4
- Permission is hereby granted, free of charge, to any person obtaining
5
- a copy of this software and associated documentation files (the
6
- "Software"), to deal in the Software without restriction, including
7
- without limitation the rights to use, copy, modify, merge, publish,
8
- distribute, sublicense, and/or sell copies of the Software, and to
9
- permit persons to whom the Software is furnished to do so, subject to
10
- the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be
13
- included in all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1
+ Copyright (c) 2005 Tobias Lutke
2
+ Copyright (c) 2008 Phusion
3
+
4
+ Permission is hereby granted, free of charge, to any person obtaining
5
+ a copy of this software and associated documentation files (the
6
+ "Software"), to deal in the Software without restriction, including
7
+ without limitation the rights to use, copy, modify, merge, publish,
8
+ distribute, sublicense, and/or sell copies of the Software, and to
9
+ permit persons to whom the Software is furnished to do so, subject to
10
+ the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be
13
+ included in all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -1,209 +1,214 @@
1
- # RubyMoney - Money
2
-
3
- ## Introduction
4
-
5
- This library aids one in handling money and different currencies.
6
-
7
- ### Features
8
-
9
- - Provides a `Money` class which encapsulates all information about an certain
10
- amount of money, such as its value and its currency.
11
- - Provides a `Money::Currency` class which encapsulates all information about
12
- a monetary unit.
13
- - Represents monetary values as integers, in cents. This avoids floating point
14
- rounding errors.
15
- - Represents currency as `Money::Currency` instances providing an high level of
16
- flexibility.
17
- - Provides APIs for exchanging money from one currency to another.
18
- - Has the ability to parse a money and currency strings
19
- into the corresponding Money/Currency object.
20
-
21
- ### Resources
22
-
23
- - [Website](http://money.rubyforge.org)
24
- - [API Documentation](http://money.rubyforge.org)
25
- - [Git Repository](http://github.com/RubyMoney/money)
26
-
27
- ## Downloading
28
-
29
- Install stable releases with the following command:
30
-
31
- gem install money
32
-
33
- The development version (hosted on Github) can be installed with:
34
-
35
- git clone git://github.com/RubyMoney/money.git
36
- cd money
37
- rake install
38
-
39
- ## Usage
40
-
41
- require 'money'
42
-
43
- # 10.00 USD
44
- money = Money.new(1000, "USD")
45
- money.cents #=> 1000
46
- money.currency #=> Currency.new("USD")
47
-
48
- # Comparisons
49
- Money.new(1000, "USD") == Money.new(1000, "USD") #=> true
50
- Money.new(1000, "USD") == Money.new(100, "USD") #=> false
51
- Money.new(1000, "USD") == Money.new(1000, "EUR") #=> false
52
- Money.new(1000, "USD") != Money.new(1000, "EUR") #=> true
53
-
54
- # Arithmetic
55
- Money.new(1000, "USD") + Money.new(500, "USD") == Money.new(1500, "USD")
56
- Money.new(1000, "USD") - Money.new(200, "USD") == Money.new(800, "USD")
57
- Money.new(1000, "USD") / 5 == Money.new(200, "USD")
58
- Money.new(1000, "USD") * 5 == Money.new(5000, "USD")
59
-
60
- # Currency conversions
61
- some_code_to_setup_exchange_rates
62
- Money.new(1000, "USD").exchange_to("EUR") == Money.new(some_value, "EUR")
63
-
64
- ## Currency
65
-
66
- Currencies are consistently represented as instances of `Money::Currency`.
67
- The most part of `Money` APIs allows you to supply either a `String` or a
68
- `Money::Currency`.
69
-
70
- Money.new(1000, "USD") == Money.new(900, Currency.new("USD"))
71
- Money.new(1000, "EUR").currency == Currency.new("EUR")
72
-
73
- A `Money::Currency` instance holds all the information about the currency,
74
- including the currency symbol, name and much more.
75
-
76
- currency = Money.new(1000, "USD").currency
77
- currency.iso_code #=> "USD"
78
- currency.name #=> "United States Dollar"
79
-
80
- To define a new `Money::Currency` simply add a new item to the
81
- `Money::Currency::TABLE` hash, where the key is the identifier for the currency
82
- object and the value is a hash containing all the currency attributes.
83
-
84
- Money::Currency::TABLE[:USD] = {
85
- :priority => 1,
86
- :iso_code => "USD",
87
- :name => "United States Dollar",
88
- :symbol => "$",
89
- :subunit => "Cent"
90
- :subunit_to_unit => 100,
91
- :separator => ".",
92
- :delimiter => ","
93
- }
94
-
95
- The pre-defined set of attributes includes:
96
-
97
- - `:priority` a numerical value you can use to sort/group the currency list
98
- - `:iso_code` the international 3-letter code as defined by the ISO 4217 standard
99
- - `:name` the currency name
100
- - `:symbol` the currency symbol (UTF-8 encoded)
101
- - `:subunit` the name of the fractional monetary unit
102
- - `:subunit_to_unit` the proportion between the unit and the subunit
103
- - `:separator` character between the whole and fraction amounts
104
- - `:delimiter` character between each thousands place
105
-
106
- All attributes are optional. Some attributes, such as `:symbol`, are used by
107
- the Money class to print out a representation of the object. Other attributes,
108
- such as `:name` or `:priority`, exist to provide a basic API you can take
109
- advantage of to build your application.
110
-
111
- ### :priority
112
-
113
- The priority attribute is an arbitrary numerical value you can assign to the
114
- `Money::Currency` and use in sorting/grouping operation.
115
-
116
- For instance, let's assume your Rails application needs to render a currency
117
- selector like the one available
118
- [here](http://finance.yahoo.com/currency-converter/). You can create a couple of
119
- custom methods to return the list of major currencies and all currencies as
120
- follows:
121
-
122
- # Returns an array of currency id where
123
- # priority < 10
124
- def major_currencies(hash)
125
- hash.inject([]) do |array, (id, attributes)|
126
- priority = attributes[:priority]
127
- if priority && priority < 10
128
- array[priority] ||= []
129
- array[priority] << id
130
- end
131
- array
132
- end.compact.flatten
133
- end
134
-
135
- # Returns an array of all currency id
136
- def all_currencies(hash)
137
- hash.keys
138
- end
139
-
140
- major_currencies(Money::Currency::TABLE)
141
- # => [ :usd, :eur, :bgp, :cad ]
142
-
143
- all_currencies(Money::Currency::TABLE)
144
- # => [ :aed, :afn, all, ... ]
145
-
146
- ### Default Currency
147
-
148
- By default `Money` defaults to USD as its currency. This can be overwritten
149
- using:
150
-
151
- Money.default_currency = Money::Currency.new("CAD")
152
-
153
- If you use Rails, then `environment.rb` is a very good place to put this.
154
-
155
- ## Currency Exchange
156
-
157
- Exchanging money is performed through an exchange bank object. The default
158
- exchange bank object requires one to manually specify the exchange rate. Here's
159
- an example of how it works:
160
-
161
- Money.add_rate("USD", "CAD", 1.24515)
162
- Money.add_rate("CAD", "USD", 0.803115)
163
-
164
- Money.us_dollar(100).exchange_to("CAD") # => Money.new(124, "CAD")
165
- Money.ca_dollar(100).exchange_to("USD") # => Money.new(80, "USD")
166
-
167
- Comparison and arithmetic operations work as expected:
168
-
169
- Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
170
- Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
171
-
172
- Money.add_rate("USD", "EUR", 0.5)
173
- Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
174
-
175
- There is nothing stopping you from creating bank objects which scrapes
176
- [XE](http://www.xe.com) for the current rates or just returns `rand(2)`:
177
-
178
- Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new
179
-
180
- ### Implementations
181
-
182
- The following is a list of Money.gem compatible currency exchange rate
183
- implementations.
184
-
185
- - [eu_central_bank](http://github.com/RubyMoney/eu_central_bank)
186
- - [google_currency](http://github.com/RubyMoney/google_currency)
187
-
188
- ## Ruby on Rails
189
-
190
- Use the `compose_of` helper to let Active Record deal with embedding the money
191
- object in your models. The following example requires a `cents` and a
192
- `currency` field.
193
-
194
- composed_of :price,
195
- :class_name => "Money",
196
- :mapping => [%w(cents cents), %w(currency currency_as_string)],
197
- :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
198
- :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
199
-
200
- For Money 2.2.x and previous versions, simply use the following `composed_of`
201
- definition:
202
-
203
- composed_of :price,
204
- :class_name => "Money",
205
- :mapping => [%w(cents cents), %w(currency currency)],
206
- :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
207
-
208
- For further details read the full discussion
209
- [here](http://github.com/RubyMoney/money/issues/4#comment_224880).
1
+ # RubyMoney - Money
2
+
3
+ ## Introduction
4
+
5
+ This library aids one in handling money and different currencies.
6
+
7
+ ### Features
8
+
9
+ - Provides a `Money` class which encapsulates all information about an certain
10
+ amount of money, such as its value and its currency.
11
+ - Provides a `Money::Currency` class which encapsulates all information about
12
+ a monetary unit.
13
+ - Represents monetary values as integers, in cents. This avoids floating point
14
+ rounding errors.
15
+ - Represents currency as `Money::Currency` instances providing an high level of
16
+ flexibility.
17
+ - Provides APIs for exchanging money from one currency to another.
18
+ - Has the ability to parse a money and currency strings
19
+ into the corresponding Money/Currency object.
20
+
21
+ ### Resources
22
+
23
+ - [Website](http://money.rubyforge.org)
24
+ - [API Documentation](http://money.rubyforge.org)
25
+ - [Git Repository](http://github.com/RubyMoney/money)
26
+
27
+ ## Downloading
28
+
29
+ Install stable releases with the following command:
30
+
31
+ gem install money
32
+
33
+ The development version (hosted on Github) can be installed with:
34
+
35
+ git clone git://github.com/RubyMoney/money.git
36
+ cd money
37
+ rake install
38
+
39
+ ## Usage
40
+
41
+ require 'money'
42
+
43
+ # 10.00 USD
44
+ money = Money.new(1000, "USD")
45
+ money.cents #=> 1000
46
+ money.currency #=> Currency.new("USD")
47
+
48
+ # Comparisons
49
+ Money.new(1000, "USD") == Money.new(1000, "USD") #=> true
50
+ Money.new(1000, "USD") == Money.new(100, "USD") #=> false
51
+ Money.new(1000, "USD") == Money.new(1000, "EUR") #=> false
52
+ Money.new(1000, "USD") != Money.new(1000, "EUR") #=> true
53
+
54
+ # Arithmetic
55
+ Money.new(1000, "USD") + Money.new(500, "USD") == Money.new(1500, "USD")
56
+ Money.new(1000, "USD") - Money.new(200, "USD") == Money.new(800, "USD")
57
+ Money.new(1000, "USD") / 5 == Money.new(200, "USD")
58
+ Money.new(1000, "USD") * 5 == Money.new(5000, "USD")
59
+
60
+ # Currency conversions
61
+ some_code_to_setup_exchange_rates
62
+ Money.new(1000, "USD").exchange_to("EUR") == Money.new(some_value, "EUR")
63
+
64
+ ## Currency
65
+
66
+ Currencies are consistently represented as instances of `Money::Currency`.
67
+ The most part of `Money` APIs allows you to supply either a `String` or a
68
+ `Money::Currency`.
69
+
70
+ Money.new(1000, "USD") == Money.new(900, Currency.new("USD"))
71
+ Money.new(1000, "EUR").currency == Currency.new("EUR")
72
+
73
+ A `Money::Currency` instance holds all the information about the currency,
74
+ including the currency symbol, name and much more.
75
+
76
+ currency = Money.new(1000, "USD").currency
77
+ currency.iso_code #=> "USD"
78
+ currency.name #=> "United States Dollar"
79
+
80
+ To define a new `Money::Currency` simply add a new item to the
81
+ `Money::Currency::TABLE` hash, where the key is the identifier for the currency
82
+ object and the value is a hash containing all the currency attributes.
83
+
84
+ Money::Currency::TABLE[:USD] = {
85
+ :priority => 1,
86
+ :iso_code => "USD",
87
+ :name => "United States Dollar",
88
+ :symbol => "$",
89
+ :subunit => "Cent"
90
+ :subunit_to_unit => 100,
91
+ :separator => ".",
92
+ :delimiter => ","
93
+ }
94
+
95
+ The pre-defined set of attributes includes:
96
+
97
+ - `:priority` a numerical value you can use to sort/group the currency list
98
+ - `:iso_code` the international 3-letter code as defined by the ISO 4217 standard
99
+ - `:name` the currency name
100
+ - `:symbol` the currency symbol (UTF-8 encoded)
101
+ - `:subunit` the name of the fractional monetary unit
102
+ - `:subunit_to_unit` the proportion between the unit and the subunit
103
+ - `:separator` character between the whole and fraction amounts
104
+ - `:delimiter` character between each thousands place
105
+
106
+ All attributes are optional. Some attributes, such as `:symbol`, are used by
107
+ the Money class to print out a representation of the object. Other attributes,
108
+ such as `:name` or `:priority`, exist to provide a basic API you can take
109
+ advantage of to build your application.
110
+
111
+ ### :priority
112
+
113
+ The priority attribute is an arbitrary numerical value you can assign to the
114
+ `Money::Currency` and use in sorting/grouping operation.
115
+
116
+ For instance, let's assume your Rails application needs to render a currency
117
+ selector like the one available
118
+ [here](http://finance.yahoo.com/currency-converter/). You can create a couple of
119
+ custom methods to return the list of major currencies and all currencies as
120
+ follows:
121
+
122
+ # Returns an array of currency id where
123
+ # priority < 10
124
+ def major_currencies(hash)
125
+ hash.inject([]) do |array, (id, attributes)|
126
+ priority = attributes[:priority]
127
+ if priority && priority < 10
128
+ array[priority] ||= []
129
+ array[priority] << id
130
+ end
131
+ array
132
+ end.compact.flatten
133
+ end
134
+
135
+ # Returns an array of all currency id
136
+ def all_currencies(hash)
137
+ hash.keys
138
+ end
139
+
140
+ major_currencies(Money::Currency::TABLE)
141
+ # => [ :usd, :eur, :bgp, :cad ]
142
+
143
+ all_currencies(Money::Currency::TABLE)
144
+ # => [ :aed, :afn, all, ... ]
145
+
146
+ ### Default Currency
147
+
148
+ By default `Money` defaults to USD as its currency. This can be overwritten
149
+ using:
150
+
151
+ Money.default_currency = Money::Currency.new("CAD")
152
+
153
+ If you use Rails, then `environment.rb` is a very good place to put this.
154
+
155
+ ## Currency Exchange
156
+
157
+ Exchanging money is performed through an exchange bank object. The default
158
+ exchange bank object requires one to manually specify the exchange rate. Here's
159
+ an example of how it works:
160
+
161
+ Money.add_rate("USD", "CAD", 1.24515)
162
+ Money.add_rate("CAD", "USD", 0.803115)
163
+
164
+ Money.us_dollar(100).exchange_to("CAD") # => Money.new(124, "CAD")
165
+ Money.ca_dollar(100).exchange_to("USD") # => Money.new(80, "USD")
166
+
167
+ Comparison and arithmetic operations work as expected:
168
+
169
+ Money.new(1000, "USD") <=> Money.new(900, "USD") # => 1; 9.00 USD is smaller
170
+ Money.new(1000, "EUR") + Money.new(10, "EUR") == Money.new(1010, "EUR")
171
+
172
+ Money.add_rate("USD", "EUR", 0.5)
173
+ Money.new(1000, "EUR") + Money.new(1000, "USD") == Money.new(1500, "EUR")
174
+
175
+ There is nothing stopping you from creating bank objects which scrapes
176
+ [XE](http://www.xe.com) for the current rates or just returns `rand(2)`:
177
+
178
+ Money.default_bank = ExchangeBankWhichScrapesXeDotCom.new
179
+
180
+ ### Implementations
181
+
182
+ The following is a list of Money.gem compatible currency exchange rate
183
+ implementations.
184
+
185
+ - [eu_central_bank](http://github.com/RubyMoney/eu_central_bank)
186
+ - [google_currency](http://github.com/RubyMoney/google_currency)
187
+
188
+ ## Ruby on Rails
189
+
190
+ Use the `compose_of` helper to let Active Record deal with embedding the money
191
+ object in your models. The following example requires 2 columns:
192
+
193
+ :price_cents, :integer, :default => 0, :null => false
194
+ :currency, :string
195
+
196
+ Then in your model file:
197
+
198
+ composed_of :price,
199
+ :class_name => "Money",
200
+ :mapping => [%w(price_cents cents), %w(currency currency_as_string)],
201
+ :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) },
202
+ :converter => Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") }
203
+
204
+
205
+ For Money 2.2.x and previous versions, simply use the following `composed_of`
206
+ definition:
207
+
208
+ composed_of :price,
209
+ :class_name => "Money",
210
+ :mapping => [%w(cents cents), %w(currency currency)],
211
+ :constructor => Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }
212
+
213
+ For further details read the full discussion
214
+ [here](http://github.com/RubyMoney/money/issues/4#comment_224880).