papercavalier-money 3.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/CHANGELOG.md +344 -0
- data/LICENSE +21 -0
- data/README.md +214 -0
- data/Rakefile +49 -0
- data/lib/money.rb +27 -0
- data/lib/money/bank/base.rb +131 -0
- data/lib/money/bank/variable_exchange.rb +252 -0
- data/lib/money/core_extensions.rb +82 -0
- data/lib/money/currency.rb +422 -0
- data/lib/money/money.rb +405 -0
- data/lib/money/money/arithmetic.rb +246 -0
- data/lib/money/money/formatting.rb +244 -0
- data/lib/money/money/parsing.rb +350 -0
- data/money.gemspec +35 -0
- data/spec/bank/base_spec.rb +72 -0
- data/spec/bank/variable_exchange_spec.rb +238 -0
- data/spec/core_extensions_spec.rb +158 -0
- data/spec/currency_spec.rb +133 -0
- data/spec/money/arithmetic_spec.rb +479 -0
- data/spec/money/formatting_spec.rb +375 -0
- data/spec/money/parsing_spec.rb +197 -0
- data/spec/money_spec.rb +292 -0
- data/spec/spec_helper.rb +28 -0
- metadata +166 -0
data/.gemtest
ADDED
File without changes
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,344 @@
|
|
1
|
+
Money 3.7.0
|
2
|
+
===========
|
3
|
+
|
4
|
+
Features
|
5
|
+
--------
|
6
|
+
- add Money#to_d (thanks Andrew White)
|
7
|
+
- Add Money.use_i18n, this allows you to enable/disable i18n from being used,
|
8
|
+
even if it's required in your app.
|
9
|
+
|
10
|
+
Money 3.6.2
|
11
|
+
===========
|
12
|
+
|
13
|
+
Features
|
14
|
+
--------
|
15
|
+
- i18n enhancements (thanks eloyesp [link](https://github.com/RubyMoney/money/commit/b2cab76c78ae04f40251fa20c4ab18faa968dc53))
|
16
|
+
- README updates (thanks pconnor)
|
17
|
+
- Break into modules
|
18
|
+
- Added `:no_cents_if_whole` format option
|
19
|
+
- Update HKD from Ho to Cent
|
20
|
+
- Performance improvements (thanks weppos)
|
21
|
+
- Added Symbol#to_currency
|
22
|
+
- Added Gemfile for development
|
23
|
+
- Updated HUF currency to use `symbol_first => false`
|
24
|
+
- Updated "Turkish New Lira" to "Turkish Lira"
|
25
|
+
|
26
|
+
Money 3.6.1
|
27
|
+
===========
|
28
|
+
|
29
|
+
Bugfixes
|
30
|
+
--------
|
31
|
+
- Floating point comparison needs to Epsilon aware (thanks Tobias Luetke)
|
32
|
+
- reimplement fix for #issue/43, enable Marshal.(load/dump)
|
33
|
+
|
34
|
+
Money 3.6.0
|
35
|
+
===========
|
36
|
+
|
37
|
+
Features
|
38
|
+
--------
|
39
|
+
- Add a symbol position option for Money#format (thanks Romain, Gil and
|
40
|
+
Julien)
|
41
|
+
- Updated CNY to use "Fen" and subunit_to_unit of 100
|
42
|
+
- Updates to work with gem-testers.org
|
43
|
+
|
44
|
+
Bugfixes
|
45
|
+
--------
|
46
|
+
- Fixed issue with #format(:no_cents => true) (thanks Romain & Julien)
|
47
|
+
|
48
|
+
Money 3.5.5
|
49
|
+
===========
|
50
|
+
|
51
|
+
Features
|
52
|
+
--------
|
53
|
+
- Enhancements to Money::Currency (thanks Matthew McEachen)
|
54
|
+
- Replace delimiter with thousands_separator
|
55
|
+
- Replace separator with decimal_mark
|
56
|
+
- Added symbol_first and html_entity
|
57
|
+
- Added allocation algorithm for fair(ish) splitting of money between parties
|
58
|
+
without loosing pennies (thanks Tobias Luetke)
|
59
|
+
|
60
|
+
Bugfixes
|
61
|
+
--------
|
62
|
+
- Always store cents as an Integer (thanks Abhay Kumar)
|
63
|
+
- Fixed TypeError in rate exchange (thanks Christian Billen)
|
64
|
+
- Cleanup #parse (thanks Tom Lianza)
|
65
|
+
|
66
|
+
Money 3.5.4
|
67
|
+
===========
|
68
|
+
|
69
|
+
Features
|
70
|
+
--------
|
71
|
+
- Added Currency#decimal_places.
|
72
|
+
|
73
|
+
Bugfixes
|
74
|
+
--------
|
75
|
+
- Fixed error with Money#to_s error with negative amounts that are only cents.
|
76
|
+
|
77
|
+
Money 3.5.3
|
78
|
+
===========
|
79
|
+
|
80
|
+
Bugfixes
|
81
|
+
--------
|
82
|
+
- Fixed an error in #to_s when cents is negative
|
83
|
+
|
84
|
+
Money 3.5.2
|
85
|
+
===========
|
86
|
+
|
87
|
+
Bugfixes
|
88
|
+
--------
|
89
|
+
- Fixed an error in #to_s which appended extra 0s incorrectly
|
90
|
+
|
91
|
+
Money 3.5.1
|
92
|
+
===========
|
93
|
+
|
94
|
+
Bugfixes
|
95
|
+
--------
|
96
|
+
- Removed erroneous require.
|
97
|
+
|
98
|
+
Money 3.5.0
|
99
|
+
===========
|
100
|
+
|
101
|
+
Features
|
102
|
+
--------
|
103
|
+
- Updated to RSpec2.
|
104
|
+
- Use i18n to lookup separator and delimiter signs.
|
105
|
+
- Removed all deprecated methods up to v3.5.0, including the following:
|
106
|
+
- Using Money#format with params instead of a Hash.
|
107
|
+
- Using a Hash with Money#new.
|
108
|
+
- Using Bank#exchange, use Bank#exchange_with instead.
|
109
|
+
|
110
|
+
Bugfixes
|
111
|
+
--------
|
112
|
+
- Updated Money#to_s to respect :separator and :subunit_to_unit.
|
113
|
+
- Fixed Money#format for :subunit_to_unit != 100.
|
114
|
+
([#issue/37](http://github.com/RubyMoney/money/issue/37))
|
115
|
+
- Fixed String#to_money for :subunit_to_unit != 100.
|
116
|
+
([#issue/36](http://github.com/RubyMoney/money/issue/36))
|
117
|
+
- Removed duplicate currencies.
|
118
|
+
([#issue/38](http://github.com/RubyMoney/money/issue/38))
|
119
|
+
- Fixed issue related to JRuby returning 2 for Math.log10(1000).floor instead
|
120
|
+
of correctly returning 3.
|
121
|
+
|
122
|
+
Money 3.1.5
|
123
|
+
===========
|
124
|
+
|
125
|
+
Features
|
126
|
+
--------
|
127
|
+
- Added support for creating objects with the main monetary unit instead of
|
128
|
+
cents.
|
129
|
+
([#issue/25](http://github.com/RubyMoney/money/issues/25))
|
130
|
+
- Deprecated `Money#format` with separate params instead of Hash. Deprecation
|
131
|
+
target set to Money 3.5.0.
|
132
|
+
([#issue/31](http://github.com/RubyMoney/money/issues/31))
|
133
|
+
- Deprecated `Money#new(0, :currency => "EUR")` in favor of
|
134
|
+
`Money#new(0, "EUR")`. Deprecation target set to Money 3.5.0.
|
135
|
+
([#issue/31](http://github.com/RubyMoney/money/issues/31))
|
136
|
+
- Throw ArgumentError when trying to multiply two Money objects together.
|
137
|
+
([#issue/29](http://github.com/RubyMoney/money/issues/29))
|
138
|
+
- Update Money#parse to use :subunit_to_unit
|
139
|
+
([#issue/30](http://github.com/RubyMoney/money/issues/30))
|
140
|
+
|
141
|
+
Bugfixes
|
142
|
+
--------
|
143
|
+
- Downgraded required_rubygems_version to >= 1.3.6.
|
144
|
+
([#issue/26](http://github.com/RubyMoney/money/issues/26))
|
145
|
+
- Use BigDecimal when floating point calculations are needed.
|
146
|
+
- Ruby 1.9.2 compatibility enhancements.
|
147
|
+
|
148
|
+
|
149
|
+
Money 3.1.0
|
150
|
+
===========
|
151
|
+
|
152
|
+
Features
|
153
|
+
--------
|
154
|
+
- Implemented `Money::Bank::Base`.
|
155
|
+
([#issue/14](http://github.com/RubyMoney/money/issues/14))
|
156
|
+
- Added `Money::Bank::Base#exchange_with`.
|
157
|
+
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
158
|
+
3.2.0.
|
159
|
+
- Implented `Money::Bank::VariableExchange`
|
160
|
+
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
161
|
+
3.2.0.
|
162
|
+
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
163
|
+
Deprecation target set to Money 3.2.0.
|
164
|
+
([#issue/16](http://github.com/RubyMoney/money/issues/16))
|
165
|
+
- Implemented `#has` for `Money` and `Money::Currency`.
|
166
|
+
- Refactored test suite to conform to RSpec conventions.
|
167
|
+
- Moved project from [FooBarWidget](http://github.com/FooBarWidget) to
|
168
|
+
[RubyMoney](http://github.com/RubyMoney)
|
169
|
+
- Added Simone Carletti to list of authors.
|
170
|
+
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
171
|
+
`Money::Bank::Base`.
|
172
|
+
([#issue/18](http://github.com/RubyMoney/money/issues/18))
|
173
|
+
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
174
|
+
([#issue/19](http://github.com/RubyMoney/money/issues/19))
|
175
|
+
- Added [google_currency](http://github.com/RubyMoney/google_currency) to list
|
176
|
+
of Currency Exchange Implementations.
|
177
|
+
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
178
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
179
|
+
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
180
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
181
|
+
- Removed dependency on Jeweler.
|
182
|
+
- Replaced usage of hanna with yardoc.
|
183
|
+
- Rewrote/reformatted all documentation.
|
184
|
+
|
185
|
+
Bugfixes
|
186
|
+
--------
|
187
|
+
- Fixed incorrect URLs in documentation.
|
188
|
+
([#issue/17](http://github.com/RubyMoney/money/issues/17))
|
189
|
+
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
190
|
+
([#issue/20](http://github.com/RubyMoney/money/issues/20))
|
191
|
+
- Updated Ghanaian Cedi to use correct ISO Code, GHS.
|
192
|
+
([#issue/22](http://github.com/RubyMoney/money/issues/22))
|
193
|
+
- Make `default` rake task call `spec`.
|
194
|
+
([#issue/23](http://github.com/RubyMoney/money/issues/23))
|
195
|
+
|
196
|
+
Money 3.1.0.pre3
|
197
|
+
================
|
198
|
+
|
199
|
+
Features
|
200
|
+
--------
|
201
|
+
- Added [google_currency](http://github.com/RubyMoney/google_currency) to list
|
202
|
+
of Currency Exchange Implementations.
|
203
|
+
- Added `#export_rates` to `Money::Bank::VariableExchange`.
|
204
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
205
|
+
- Added `#import_rates` to `Money::Bank::VariableExchange`.
|
206
|
+
([#issue/21](http://github.com/RubyMoney/money/issues/21))
|
207
|
+
|
208
|
+
Bugfixes
|
209
|
+
--------
|
210
|
+
- Updated `:subunit_to_unit` for HKD from 10 to 100.
|
211
|
+
([#issue/20](http://github.com/RubyMoney/money/issues/20))
|
212
|
+
|
213
|
+
Money 3.1.0.pre2
|
214
|
+
================
|
215
|
+
|
216
|
+
Features
|
217
|
+
--------
|
218
|
+
- Moved `@rounding_method` from `Money::Bank::VariableExchange` to
|
219
|
+
`Money::Bank::Base`.
|
220
|
+
([#issue/18](http://github.com/RubyMoney/money/issues/18))
|
221
|
+
- Added `#setup` to `Money::Bank::Base`. Called from `#initialize`.
|
222
|
+
([#issue/19](http://github.com/RubyMoney/money/issues/19))
|
223
|
+
|
224
|
+
Bugfixes
|
225
|
+
--------
|
226
|
+
- Fixed incorrect URLs in documentation.
|
227
|
+
([#issue/17](http://github.com/RubyMoney/money/issues/17))
|
228
|
+
|
229
|
+
Money 3.1.0.pre1
|
230
|
+
================
|
231
|
+
|
232
|
+
Features
|
233
|
+
--------
|
234
|
+
- Implemented `Money::Bank::Base`.
|
235
|
+
([#issue/14](http://github.com/RubyMoney/money/issues/14))
|
236
|
+
- Added `Money::Bank::Base#exchange_with`.
|
237
|
+
- Deprecated `Money::Bank::Base#exchange`. Deprecation target set to Money
|
238
|
+
3.2.0.
|
239
|
+
- Implented `Money::Bank::VariableExchange`
|
240
|
+
- Deprecated `Money::VariableExchangeBank`. Deprecation target set to Money
|
241
|
+
3.2.0.
|
242
|
+
- Deprecate `Money::SYMBOLS`, `Money::SEPARATORS` and `Money::DELIMITERS`.
|
243
|
+
Deprecation target set to Money 3.2.0.
|
244
|
+
([#issue/16](http://github.com/RubyMoney/money/issues/16))
|
245
|
+
- Implemented `#has` for `Money` and `Money::Currency`.
|
246
|
+
- Refactored test suite to conform to RSpec conventions.
|
247
|
+
- Moved project from [FooBarWidget](http://github.com/FooBarWidget) to
|
248
|
+
[RubyMoney](http://github.com/RubyMoney)
|
249
|
+
- Added Simone Carletti to list of authors.
|
250
|
+
|
251
|
+
Bugfixes
|
252
|
+
--------
|
253
|
+
- Fixed rounding error in `Numeric#to_money`.
|
254
|
+
([#issue/15](http://github.com/RubyMoney/money/issues/15))
|
255
|
+
|
256
|
+
Money 3.0.5
|
257
|
+
===========
|
258
|
+
|
259
|
+
Features
|
260
|
+
--------
|
261
|
+
- Added `Money#abs`.
|
262
|
+
- Added ability to pass a block to `VariableExchangeBank#new` or `#exchange`,
|
263
|
+
specifying a custom truncation method
|
264
|
+
- Added optional `currency` argument to` Numeric#to_money`.
|
265
|
+
([#issue/11](http://github.com/RubyMoney/money/issues/11))
|
266
|
+
- Added optional `currency` argument to `String#to_money`.
|
267
|
+
([#issue/11](http://github.com/RubyMoney/money/issues/11))
|
268
|
+
- Use '¤' as the default currency symbol.
|
269
|
+
([#issue/10](http://github.com/RubyMoney/money/issues/10))
|
270
|
+
|
271
|
+
Bugfixes
|
272
|
+
--------
|
273
|
+
- Updated `Currency#subunit_to_unit` documentation (it's an integer not a
|
274
|
+
string).
|
275
|
+
- Fixed issue when exchanging currencies with different `:subunit_to_unit`
|
276
|
+
values.
|
277
|
+
- `Numeric#to_money` now respects `:subunit_to_unit`.
|
278
|
+
([#issue/12](http://github.com/RubyMoney/money/issues/12))
|
279
|
+
|
280
|
+
Money 3.0.4
|
281
|
+
===========
|
282
|
+
|
283
|
+
Features
|
284
|
+
--------
|
285
|
+
- Use `:subunit_to_unit` in `#to_s`, `#to_f` and `#format`.
|
286
|
+
- Deprecated `Money#SEPARATORS` and `Money#DELIMITERS`.
|
287
|
+
|
288
|
+
Bugfixes
|
289
|
+
--------
|
290
|
+
- Updated `#exchange` to avoid floating point rounding errors.
|
291
|
+
- Added `:separator` and `:delimiter` to `Currency`
|
292
|
+
- Updated the attributes of the Chilean Peso.
|
293
|
+
|
294
|
+
Money 3.0.3
|
295
|
+
===========
|
296
|
+
|
297
|
+
Features
|
298
|
+
--------
|
299
|
+
- Added `#currency_as_string` and `#currency_as_string=` for easier
|
300
|
+
integration with ActiveRecord/Rails
|
301
|
+
|
302
|
+
Money 3.0.2
|
303
|
+
===========
|
304
|
+
|
305
|
+
Features
|
306
|
+
--------
|
307
|
+
- Added `#div`, `#divmod`, `#modulo`, `#%` and `#remainder` to `Money`.
|
308
|
+
|
309
|
+
Money 3.0.1
|
310
|
+
===========
|
311
|
+
|
312
|
+
Features
|
313
|
+
--------
|
314
|
+
- Added `#eql?` to `Money`
|
315
|
+
- Updated `Numeric#to_money` to work with all children of `Numeric` (i.e.
|
316
|
+
`BigDecimal`, `Integer`, `Fixnum`, `Float`, etc)
|
317
|
+
|
318
|
+
Money 3.0.0
|
319
|
+
===========
|
320
|
+
|
321
|
+
Features
|
322
|
+
--------
|
323
|
+
- Version Bump due to compatibility changes with ActiveRecord. See
|
324
|
+
conversation
|
325
|
+
[here](http://github.com/RubyMoney/money/issues#issue/4/comment/224880)
|
326
|
+
for more information.
|
327
|
+
|
328
|
+
Money 2.3.0
|
329
|
+
===========
|
330
|
+
|
331
|
+
Features
|
332
|
+
--------
|
333
|
+
- Currency is now represented by a `Currency` Object instead of a `String`.
|
334
|
+
|
335
|
+
Money 2.2.0
|
336
|
+
===========
|
337
|
+
|
338
|
+
Features
|
339
|
+
--------
|
340
|
+
- Can now divide two Money objects by one another using `#/`.
|
341
|
+
- Can now convert a Money object to a float using `#to_f`.
|
342
|
+
- Users can now specify Separators and Delimiters when using `#format`.
|
343
|
+
- Support for Brazilian Real `Money.new(1_00, :BRL)`
|
344
|
+
- Migrated to Jeweler
|
data/LICENSE
ADDED
@@ -0,0 +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.
|
data/README.md
ADDED
@@ -0,0 +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 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).
|