money 3.7.1 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +384 -351
- data/LICENSE +21 -21
- data/README.md +243 -214
- data/Rakefile +49 -49
- data/lib/money.rb +28 -27
- data/lib/money/bank/base.rb +131 -131
- data/lib/money/bank/variable_exchange.rb +252 -252
- data/lib/money/core_extensions.rb +82 -82
- data/lib/money/currency.rb +263 -422
- data/lib/money/currency_loader.rb +19 -0
- data/lib/money/money.rb +405 -405
- data/lib/money/money/arithmetic.rb +246 -246
- data/lib/money/money/formatting.rb +260 -244
- data/lib/money/money/parsing.rb +350 -350
- data/money.gemspec +29 -35
- data/spec/bank/base_spec.rb +72 -72
- data/spec/bank/variable_exchange_spec.rb +238 -238
- data/spec/core_extensions_spec.rb +158 -158
- data/spec/currency_spec.rb +120 -133
- data/spec/money/arithmetic_spec.rb +479 -479
- data/spec/money/formatting_spec.rb +383 -375
- data/spec/money/parsing_spec.rb +197 -197
- data/spec/money_spec.rb +292 -292
- data/spec/spec_helper.rb +28 -28
- metadata +54 -126
- data/lib/money.rbc +0 -184
- data/lib/money/bank/base.rbc +0 -818
- data/lib/money/bank/variable_exchange.rbc +0 -2550
- data/lib/money/core_extensions.rbc +0 -664
- data/lib/money/currency.rbc +0 -22708
- data/lib/money/money.rbc +0 -3861
- data/lib/money/money/arithmetic.rbc +0 -2778
- data/lib/money/money/formatting.rbc +0 -2265
- data/lib/money/money/parsing.rbc +0 -2737
- data/spec/bank/base_spec.rbc +0 -2461
- data/spec/bank/variable_exchange_spec.rbc +0 -7541
- data/spec/core_extensions_spec.rbc +0 -5921
- data/spec/currency_spec.rbc +0 -4535
- data/spec/money/arithmetic_spec.rbc +0 -25140
- data/spec/money/formatting_spec.rbc +0 -12545
- data/spec/money/parsing_spec.rbc +0 -6511
- data/spec/money_spec.rbc +0 -9824
- data/spec/spec_helper.rbc +0 -575
@@ -1,82 +1,82 @@
|
|
1
|
-
# Open +Numeric+ to add new method.
|
2
|
-
class Numeric
|
3
|
-
|
4
|
-
# Converts this numeric into a +Money+ object in the given +currency+.
|
5
|
-
#
|
6
|
-
# @param [Currency, String, Symbol] currency
|
7
|
-
# The currency to set the resulting +Money+ object to.
|
8
|
-
#
|
9
|
-
# @return [Money]
|
10
|
-
#
|
11
|
-
# @example
|
12
|
-
# 100.to_money #=> #<Money @cents=10000>
|
13
|
-
# 100.37.to_money #=> #<Money @cents=10037>
|
14
|
-
# BigDecimal.new('100').to_money #=> #<Money @cents=10000>
|
15
|
-
#
|
16
|
-
# @see Money.from_numeric
|
17
|
-
#
|
18
|
-
def to_money(currency = nil)
|
19
|
-
Money.from_numeric(self, currency || Money.default_currency)
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
# Open +String+ to add new methods.
|
25
|
-
class String
|
26
|
-
|
27
|
-
# Parses the current string and converts it to a +Money+ object.
|
28
|
-
# Excess characters will be discarded.
|
29
|
-
#
|
30
|
-
# @param [Currency, String, Symbol] currency
|
31
|
-
# The currency to set the resulting +Money+ object to.
|
32
|
-
#
|
33
|
-
# @return [Money]
|
34
|
-
#
|
35
|
-
# @example
|
36
|
-
# '100'.to_money #=> #<Money @cents=10000>
|
37
|
-
# '100.37'.to_money #=> #<Money @cents=10037>
|
38
|
-
# '100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
39
|
-
# 'USD 100'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
40
|
-
# '$100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
41
|
-
# 'hello 2000 world'.to_money #=> #<Money @cents=200000 @currency=#<Money::Currency id: usd>>
|
42
|
-
#
|
43
|
-
# @see Money.from_string
|
44
|
-
#
|
45
|
-
def to_money(currency = nil)
|
46
|
-
Money.parse(self, currency)
|
47
|
-
end
|
48
|
-
|
49
|
-
# Converts the current string into a +Currency+ object.
|
50
|
-
#
|
51
|
-
# @return [Money::Currency]
|
52
|
-
#
|
53
|
-
# @raise [Money::Currency::UnknownCurrency]
|
54
|
-
# If this String reference an unknown currency.
|
55
|
-
#
|
56
|
-
# @example
|
57
|
-
# "USD".to_currency #=> #<Money::Currency id: usd>
|
58
|
-
#
|
59
|
-
def to_currency
|
60
|
-
Money::Currency.new(self)
|
61
|
-
end
|
62
|
-
|
63
|
-
end
|
64
|
-
|
65
|
-
# Open +Symbol+ to add new methods.
|
66
|
-
class Symbol
|
67
|
-
|
68
|
-
# Converts the current symbol into a +Currency+ object.
|
69
|
-
#
|
70
|
-
# @return [Money::Currency]
|
71
|
-
#
|
72
|
-
# @raise [Money::Currency::UnknownCurrency]
|
73
|
-
# If this String reference an unknown currency.
|
74
|
-
#
|
75
|
-
# @example
|
76
|
-
# :ars.to_currency #=> #<Money::Currency id: ars>
|
77
|
-
#
|
78
|
-
def to_currency
|
79
|
-
Money::Currency.new(self)
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
1
|
+
# Open +Numeric+ to add new method.
|
2
|
+
class Numeric
|
3
|
+
|
4
|
+
# Converts this numeric into a +Money+ object in the given +currency+.
|
5
|
+
#
|
6
|
+
# @param [Currency, String, Symbol] currency
|
7
|
+
# The currency to set the resulting +Money+ object to.
|
8
|
+
#
|
9
|
+
# @return [Money]
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# 100.to_money #=> #<Money @cents=10000>
|
13
|
+
# 100.37.to_money #=> #<Money @cents=10037>
|
14
|
+
# BigDecimal.new('100').to_money #=> #<Money @cents=10000>
|
15
|
+
#
|
16
|
+
# @see Money.from_numeric
|
17
|
+
#
|
18
|
+
def to_money(currency = nil)
|
19
|
+
Money.from_numeric(self, currency || Money.default_currency)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
# Open +String+ to add new methods.
|
25
|
+
class String
|
26
|
+
|
27
|
+
# Parses the current string and converts it to a +Money+ object.
|
28
|
+
# Excess characters will be discarded.
|
29
|
+
#
|
30
|
+
# @param [Currency, String, Symbol] currency
|
31
|
+
# The currency to set the resulting +Money+ object to.
|
32
|
+
#
|
33
|
+
# @return [Money]
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# '100'.to_money #=> #<Money @cents=10000>
|
37
|
+
# '100.37'.to_money #=> #<Money @cents=10037>
|
38
|
+
# '100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
39
|
+
# 'USD 100'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
40
|
+
# '$100 USD'.to_money #=> #<Money @cents=10000, @currency=#<Money::Currency id: usd>>
|
41
|
+
# 'hello 2000 world'.to_money #=> #<Money @cents=200000 @currency=#<Money::Currency id: usd>>
|
42
|
+
#
|
43
|
+
# @see Money.from_string
|
44
|
+
#
|
45
|
+
def to_money(currency = nil)
|
46
|
+
Money.parse(self, currency)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Converts the current string into a +Currency+ object.
|
50
|
+
#
|
51
|
+
# @return [Money::Currency]
|
52
|
+
#
|
53
|
+
# @raise [Money::Currency::UnknownCurrency]
|
54
|
+
# If this String reference an unknown currency.
|
55
|
+
#
|
56
|
+
# @example
|
57
|
+
# "USD".to_currency #=> #<Money::Currency id: usd>
|
58
|
+
#
|
59
|
+
def to_currency
|
60
|
+
Money::Currency.new(self)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
# Open +Symbol+ to add new methods.
|
66
|
+
class Symbol
|
67
|
+
|
68
|
+
# Converts the current symbol into a +Currency+ object.
|
69
|
+
#
|
70
|
+
# @return [Money::Currency]
|
71
|
+
#
|
72
|
+
# @raise [Money::Currency::UnknownCurrency]
|
73
|
+
# If this String reference an unknown currency.
|
74
|
+
#
|
75
|
+
# @example
|
76
|
+
# :ars.to_currency #=> #<Money::Currency id: ars>
|
77
|
+
#
|
78
|
+
def to_currency
|
79
|
+
Money::Currency.new(self)
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
data/lib/money/currency.rb
CHANGED
@@ -1,422 +1,263 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
#
|
13
|
-
|
14
|
-
|
15
|
-
#
|
16
|
-
#
|
17
|
-
# ==
|
18
|
-
#
|
19
|
-
# http://www.answers.com/topic/
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# http://
|
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
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
#
|
195
|
-
#
|
196
|
-
#
|
197
|
-
#
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
#
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
#
|
211
|
-
#
|
212
|
-
# @return [String]
|
213
|
-
|
214
|
-
|
215
|
-
#
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
#
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
else
|
265
|
-
Math.log10(subunit_to_unit).to_s.to_i+1
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
# Create a new +Currency+ object.
|
270
|
-
#
|
271
|
-
# @param [String, Symbol, #to_s] id Used to look into +TABLE+ and retrieve
|
272
|
-
# the applicable attributes.
|
273
|
-
#
|
274
|
-
# @return [Money::Currency]
|
275
|
-
#
|
276
|
-
# @example
|
277
|
-
# Money::Currency.new(:usd) #=> #<Money::Currency id: usd ...>
|
278
|
-
def initialize(id)
|
279
|
-
@id = id.to_s.downcase.to_sym
|
280
|
-
data = TABLE[@id] || raise(UnknownCurrency, "Unknown currency `#{id}'")
|
281
|
-
data.each_pair do |key, value|
|
282
|
-
instance_variable_set(:"@#{key}", value)
|
283
|
-
end
|
284
|
-
end
|
285
|
-
|
286
|
-
# Compares +self+ with +other_currency+ against the value of +priority+
|
287
|
-
# attribute.
|
288
|
-
#
|
289
|
-
# @param [Money::Currency] other_currency The currency to compare to.
|
290
|
-
#
|
291
|
-
# @return [-1,0,1] -1 if less than, 0 is equal to, 1 if greater than
|
292
|
-
#
|
293
|
-
# @example
|
294
|
-
# c1 = Money::Currency.new(:usd)
|
295
|
-
# c2 = Money::Currency.new(:jpy)
|
296
|
-
# c1 <=> c2 #=> 1
|
297
|
-
# c2 <=> c1 #=> -1
|
298
|
-
# c1 <=> c1 #=> 0
|
299
|
-
def <=>(other_currency)
|
300
|
-
self.priority <=> other_currency.priority
|
301
|
-
end
|
302
|
-
|
303
|
-
# Compares +self+ with +other_currency+ and returns +true+ if the are the
|
304
|
-
# same or if their +id+ attributes match.
|
305
|
-
#
|
306
|
-
# @param [Money::Currency] other_currency The currency to compare to.
|
307
|
-
#
|
308
|
-
# @return [Boolean]
|
309
|
-
#
|
310
|
-
# @example
|
311
|
-
# c1 = Money::Currency.new(:usd)
|
312
|
-
# c2 = Money::Currency.new(:jpy)
|
313
|
-
# c1 == c1 #=> true
|
314
|
-
# c1 == c2 #=> false
|
315
|
-
def ==(other_currency)
|
316
|
-
self.equal?(other_currency) ||
|
317
|
-
self.id == other_currency.id
|
318
|
-
end
|
319
|
-
|
320
|
-
# Compares +self+ with +other_currency+ and returns +true+ if the are the
|
321
|
-
# same or if their +id+ attributes match.
|
322
|
-
#
|
323
|
-
# @param [Money::Currency] other_currency The currency to compare to.
|
324
|
-
#
|
325
|
-
# @return [Boolean]
|
326
|
-
#
|
327
|
-
# @example
|
328
|
-
# c1 = Money::Currency.new(:usd)
|
329
|
-
# c2 = Money::Currency.new(:jpy)
|
330
|
-
# c1.eql? c1 #=> true
|
331
|
-
# c1.eql? c2 #=> false
|
332
|
-
def eql?(other_currency)
|
333
|
-
self == other_currency
|
334
|
-
end
|
335
|
-
|
336
|
-
# Returns a Fixnum hash value based on the +id+ attribute in order to use
|
337
|
-
# functions like & (intersection), group_by, etc.
|
338
|
-
#
|
339
|
-
# @return [Fixnum]
|
340
|
-
#
|
341
|
-
# @example
|
342
|
-
# Money::Currency.new(:usd).hash #=> 428936
|
343
|
-
def hash
|
344
|
-
id.hash
|
345
|
-
end
|
346
|
-
|
347
|
-
# Returns a string representation corresponding to the upcase +id+
|
348
|
-
# attribute.
|
349
|
-
#
|
350
|
-
# -–
|
351
|
-
# DEV: id.to_s.upcase corresponds to iso_code but don't use ISO_CODE for consistency.
|
352
|
-
#
|
353
|
-
# @return [String]
|
354
|
-
#
|
355
|
-
# @example
|
356
|
-
# Money::Currency.new(:usd).to_s #=> "USD"
|
357
|
-
# Money::Currency.new(:eur).to_s #=> "EUR"
|
358
|
-
def to_s
|
359
|
-
id.to_s.upcase
|
360
|
-
end
|
361
|
-
|
362
|
-
# Conversation to +self+.
|
363
|
-
#
|
364
|
-
# @return [self]
|
365
|
-
def to_currency
|
366
|
-
self
|
367
|
-
end
|
368
|
-
|
369
|
-
# Returns a human readable representation.
|
370
|
-
#
|
371
|
-
# @return [String]
|
372
|
-
#
|
373
|
-
# @example
|
374
|
-
# Money::Currency.new(:usd) #=> #<Currency id: usd ...>
|
375
|
-
def inspect
|
376
|
-
"#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, iso_code: #{iso_code}, subunit: #{subunit}>"
|
377
|
-
end
|
378
|
-
|
379
|
-
# Class Methods
|
380
|
-
class << self
|
381
|
-
|
382
|
-
# Lookup a currency with given +id+ an returns a +Currency+ instance on
|
383
|
-
# success, +nil+ otherwise.
|
384
|
-
#
|
385
|
-
# @param [String, Symbol, #to_s] id Used to look into +TABLE+ and
|
386
|
-
# retrieve the applicable attributes.
|
387
|
-
#
|
388
|
-
# @return [Money::Currency]
|
389
|
-
#
|
390
|
-
# @example
|
391
|
-
# Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
|
392
|
-
# Money::Currency.find(:foo) #=> nil
|
393
|
-
def find(id)
|
394
|
-
id = id.to_s.downcase.to_sym
|
395
|
-
new(id) if self::TABLE[id]
|
396
|
-
end
|
397
|
-
|
398
|
-
# Wraps the object in a +Currency+ unless it's already a +Currency+
|
399
|
-
# object.
|
400
|
-
#
|
401
|
-
# @param [Object] object The object to attempt and wrap as a +Currency+
|
402
|
-
# object.
|
403
|
-
#
|
404
|
-
# @return [Money::Currency]
|
405
|
-
#
|
406
|
-
# @example
|
407
|
-
# c1 = Money::Currency.new(:usd)
|
408
|
-
# Money::Currency.wrap(nil) #=> nil
|
409
|
-
# Money::Currency.wrap(c1) #=> #<Money::Currency id: usd ...>
|
410
|
-
# Money::Currency.wrap("usd") #=> #<Money::Currency id: usd ...>
|
411
|
-
def wrap(object)
|
412
|
-
if object.nil?
|
413
|
-
nil
|
414
|
-
elsif object.is_a?(Currency)
|
415
|
-
object
|
416
|
-
else
|
417
|
-
Currency.new(object)
|
418
|
-
end
|
419
|
-
end
|
420
|
-
end
|
421
|
-
end
|
422
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
|
5
|
+
class Money
|
6
|
+
|
7
|
+
# Represents a specific currency unit.
|
8
|
+
class Currency
|
9
|
+
include Comparable
|
10
|
+
extend CurrencyLoader
|
11
|
+
|
12
|
+
# Thrown when an unknown currency is requested.
|
13
|
+
class UnknownCurrency < StandardError; end
|
14
|
+
|
15
|
+
# List of known currencies.
|
16
|
+
#
|
17
|
+
# == monetary unit
|
18
|
+
# The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico.
|
19
|
+
# http://www.answers.com/topic/monetary-unit
|
20
|
+
# == fractional monetary unit, subunit
|
21
|
+
# A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit
|
22
|
+
# http://www.answers.com/topic/fractional-monetary-unit-subunit
|
23
|
+
#
|
24
|
+
# See http://en.wikipedia.org/wiki/List_of_circulating_currencies and
|
25
|
+
# http://search.cpan.org/~tnguyen/Locale-Currency-Format-1.28/Format.pm
|
26
|
+
|
27
|
+
TABLE = load_currencies
|
28
|
+
|
29
|
+
|
30
|
+
# The symbol used to identify the currency, usually the lowercase
|
31
|
+
# +iso_code+ attribute.
|
32
|
+
#
|
33
|
+
# @return [Symbol]
|
34
|
+
attr_reader :id
|
35
|
+
|
36
|
+
# A numerical value you can use to sort/group the currency list.
|
37
|
+
#
|
38
|
+
# @return [Integer]
|
39
|
+
attr_reader :priority
|
40
|
+
|
41
|
+
# The international 3-letter code as defined by the ISO 4217 standard.
|
42
|
+
#
|
43
|
+
# @return [String]
|
44
|
+
attr_reader :iso_code
|
45
|
+
#
|
46
|
+
# The international 3-numeric code as defined by the ISO 4217 standard.
|
47
|
+
#
|
48
|
+
# @return [String]
|
49
|
+
attr_reader :iso_numeric
|
50
|
+
|
51
|
+
# The currency name.
|
52
|
+
#
|
53
|
+
# @return [String]
|
54
|
+
attr_reader :name
|
55
|
+
|
56
|
+
# The currency symbol (UTF-8 encoded).
|
57
|
+
#
|
58
|
+
# @return [String]
|
59
|
+
attr_reader :symbol
|
60
|
+
|
61
|
+
# The html entity for the currency symbol
|
62
|
+
#
|
63
|
+
# @return [String]
|
64
|
+
attr_reader :html_entity
|
65
|
+
|
66
|
+
# The name of the fractional monetary unit.
|
67
|
+
#
|
68
|
+
# @return [String]
|
69
|
+
attr_reader :subunit
|
70
|
+
|
71
|
+
# The proportion between the unit and the subunit
|
72
|
+
#
|
73
|
+
# @return [Integer]
|
74
|
+
attr_reader :subunit_to_unit
|
75
|
+
|
76
|
+
# The decimal mark, or character used to separate the whole unit from the subunit.
|
77
|
+
#
|
78
|
+
# @return [String]
|
79
|
+
attr_reader :decimal_mark
|
80
|
+
alias :separator :decimal_mark
|
81
|
+
|
82
|
+
# The character used to separate thousands grouping of the whole unit.
|
83
|
+
#
|
84
|
+
# @return [String]
|
85
|
+
attr_reader :thousands_separator
|
86
|
+
alias :delimiter :thousands_separator
|
87
|
+
|
88
|
+
# Should the currency symbol precede the amount, or should it come after?
|
89
|
+
#
|
90
|
+
# @return [boolean]
|
91
|
+
attr_reader :symbol_first
|
92
|
+
|
93
|
+
def symbol_first?
|
94
|
+
!!@symbol_first
|
95
|
+
end
|
96
|
+
|
97
|
+
# The number of decimal places needed.
|
98
|
+
#
|
99
|
+
# @return [Integer]
|
100
|
+
def decimal_places
|
101
|
+
if subunit_to_unit == 1
|
102
|
+
0
|
103
|
+
elsif subunit_to_unit % 10 == 0
|
104
|
+
Math.log10(subunit_to_unit).to_s.to_i
|
105
|
+
else
|
106
|
+
Math.log10(subunit_to_unit).to_s.to_i+1
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# Create a new +Currency+ object.
|
111
|
+
#
|
112
|
+
# @param [String, Symbol, #to_s] id Used to look into +TABLE+ and retrieve
|
113
|
+
# the applicable attributes.
|
114
|
+
#
|
115
|
+
# @return [Money::Currency]
|
116
|
+
#
|
117
|
+
# @example
|
118
|
+
# Money::Currency.new(:usd) #=> #<Money::Currency id: usd ...>
|
119
|
+
def initialize(id)
|
120
|
+
@id = id.to_s.downcase.to_sym
|
121
|
+
data = TABLE[@id] || raise(UnknownCurrency, "Unknown currency `#{id}'")
|
122
|
+
data.each_pair do |key, value|
|
123
|
+
instance_variable_set(:"@#{key}", value)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
# Compares +self+ with +other_currency+ against the value of +priority+
|
128
|
+
# attribute.
|
129
|
+
#
|
130
|
+
# @param [Money::Currency] other_currency The currency to compare to.
|
131
|
+
#
|
132
|
+
# @return [-1,0,1] -1 if less than, 0 is equal to, 1 if greater than
|
133
|
+
#
|
134
|
+
# @example
|
135
|
+
# c1 = Money::Currency.new(:usd)
|
136
|
+
# c2 = Money::Currency.new(:jpy)
|
137
|
+
# c1 <=> c2 #=> 1
|
138
|
+
# c2 <=> c1 #=> -1
|
139
|
+
# c1 <=> c1 #=> 0
|
140
|
+
def <=>(other_currency)
|
141
|
+
self.priority <=> other_currency.priority
|
142
|
+
end
|
143
|
+
|
144
|
+
# Compares +self+ with +other_currency+ and returns +true+ if the are the
|
145
|
+
# same or if their +id+ attributes match.
|
146
|
+
#
|
147
|
+
# @param [Money::Currency] other_currency The currency to compare to.
|
148
|
+
#
|
149
|
+
# @return [Boolean]
|
150
|
+
#
|
151
|
+
# @example
|
152
|
+
# c1 = Money::Currency.new(:usd)
|
153
|
+
# c2 = Money::Currency.new(:jpy)
|
154
|
+
# c1 == c1 #=> true
|
155
|
+
# c1 == c2 #=> false
|
156
|
+
def ==(other_currency)
|
157
|
+
self.equal?(other_currency) ||
|
158
|
+
self.id == other_currency.id
|
159
|
+
end
|
160
|
+
|
161
|
+
# Compares +self+ with +other_currency+ and returns +true+ if the are the
|
162
|
+
# same or if their +id+ attributes match.
|
163
|
+
#
|
164
|
+
# @param [Money::Currency] other_currency The currency to compare to.
|
165
|
+
#
|
166
|
+
# @return [Boolean]
|
167
|
+
#
|
168
|
+
# @example
|
169
|
+
# c1 = Money::Currency.new(:usd)
|
170
|
+
# c2 = Money::Currency.new(:jpy)
|
171
|
+
# c1.eql? c1 #=> true
|
172
|
+
# c1.eql? c2 #=> false
|
173
|
+
def eql?(other_currency)
|
174
|
+
self == other_currency
|
175
|
+
end
|
176
|
+
|
177
|
+
# Returns a Fixnum hash value based on the +id+ attribute in order to use
|
178
|
+
# functions like & (intersection), group_by, etc.
|
179
|
+
#
|
180
|
+
# @return [Fixnum]
|
181
|
+
#
|
182
|
+
# @example
|
183
|
+
# Money::Currency.new(:usd).hash #=> 428936
|
184
|
+
def hash
|
185
|
+
id.hash
|
186
|
+
end
|
187
|
+
|
188
|
+
# Returns a string representation corresponding to the upcase +id+
|
189
|
+
# attribute.
|
190
|
+
#
|
191
|
+
# -–
|
192
|
+
# DEV: id.to_s.upcase corresponds to iso_code but don't use ISO_CODE for consistency.
|
193
|
+
#
|
194
|
+
# @return [String]
|
195
|
+
#
|
196
|
+
# @example
|
197
|
+
# Money::Currency.new(:usd).to_s #=> "USD"
|
198
|
+
# Money::Currency.new(:eur).to_s #=> "EUR"
|
199
|
+
def to_s
|
200
|
+
id.to_s.upcase
|
201
|
+
end
|
202
|
+
|
203
|
+
# Conversation to +self+.
|
204
|
+
#
|
205
|
+
# @return [self]
|
206
|
+
def to_currency
|
207
|
+
self
|
208
|
+
end
|
209
|
+
|
210
|
+
# Returns a human readable representation.
|
211
|
+
#
|
212
|
+
# @return [String]
|
213
|
+
#
|
214
|
+
# @example
|
215
|
+
# Money::Currency.new(:usd) #=> #<Currency id: usd ...>
|
216
|
+
def inspect
|
217
|
+
"#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}>"
|
218
|
+
end
|
219
|
+
|
220
|
+
# Class Methods
|
221
|
+
class << self
|
222
|
+
|
223
|
+
# Lookup a currency with given +id+ an returns a +Currency+ instance on
|
224
|
+
# success, +nil+ otherwise.
|
225
|
+
#
|
226
|
+
# @param [String, Symbol, #to_s] id Used to look into +TABLE+ and
|
227
|
+
# retrieve the applicable attributes.
|
228
|
+
#
|
229
|
+
# @return [Money::Currency]
|
230
|
+
#
|
231
|
+
# @example
|
232
|
+
# Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
|
233
|
+
# Money::Currency.find(:foo) #=> nil
|
234
|
+
def find(id)
|
235
|
+
id = id.to_s.downcase.to_sym
|
236
|
+
new(id) if self::TABLE[id]
|
237
|
+
end
|
238
|
+
|
239
|
+
# Wraps the object in a +Currency+ unless it's already a +Currency+
|
240
|
+
# object.
|
241
|
+
#
|
242
|
+
# @param [Object] object The object to attempt and wrap as a +Currency+
|
243
|
+
# object.
|
244
|
+
#
|
245
|
+
# @return [Money::Currency]
|
246
|
+
#
|
247
|
+
# @example
|
248
|
+
# c1 = Money::Currency.new(:usd)
|
249
|
+
# Money::Currency.wrap(nil) #=> nil
|
250
|
+
# Money::Currency.wrap(c1) #=> #<Money::Currency id: usd ...>
|
251
|
+
# Money::Currency.wrap("usd") #=> #<Money::Currency id: usd ...>
|
252
|
+
def wrap(object)
|
253
|
+
if object.nil?
|
254
|
+
nil
|
255
|
+
elsif object.is_a?(Currency)
|
256
|
+
object
|
257
|
+
else
|
258
|
+
Currency.new(object)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|