This repository has been archived by the owner on Apr 6, 2018. It is now read-only.
forked from RubyMoney/money
/
money.rb
419 lines (375 loc) · 11.1 KB
/
money.rb
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
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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# encoding: utf-8
require 'money/bank/variable_exchange'
require 'money/money/arithmetic'
require 'money/money/parsing'
require 'money/money/formatting'
# Represents an amount of money in a given currency.
class Money
include Comparable
include Arithmetic
include Formatting
include Parsing
# The value of the money in cents.
#
# @return [Integer]
attr_reader :cents
# The currency the money is in.
#
# @return [Currency]
attr_reader :currency
# The +Money::Bank+ based object used to perform currency exchanges with.
#
# @return [Money::Bank::*]
attr_reader :bank
# Class Methods
class << self
# Each Money object is associated to a bank object, which is responsible
# for currency exchange. This property allows you to specify the default
# bank object. The default value for this property is an instance of
# +Bank::VariableExchange.+ It allows one to specify custom exchange rates.
#
# @return [Money::Bank::*]
attr_accessor :default_bank
# The default currency, which is used when +Money.new+ is called without an
# explicit currency argument. The default value is Currency.new("USD"). The
# value must be a valid +Money::Currency+ instance.
#
# @return [Money::Currency]
attr_accessor :default_currency
# Use this to disable i18n even if it's used by other objects in your app.
#
# @return [true,false]
attr_accessor :use_i18n
# Use this to enable the ability to assume the currency from a passed symbol
#
# @return [true,false]
attr_accessor :assume_from_symbol
end
# Set the default bank for creating new +Money+ objects.
self.default_bank = Bank::VariableExchange.instance
# Set the default currency for creating new +Money+ object.
self.default_currency = Currency.new("USD")
# Default to using i18n
self.use_i18n = true
# Default to not using currency symbol assumptions when parsing
self.assume_from_symbol = false
# Create a new money object with value 0.
#
# @param [Currency, String, Symbol] currency The currency to use.
#
# @return [Money]
#
# @example
# Money.empty #=> #<Money @cents=0>
def self.empty(currency = default_currency)
Money.new(0, currency)
end
# Creates a new Money object of the given value, using the Canadian
# dollar currency.
#
# @param [Integer] cents The cents value.
#
# @return [Money]
#
# @example
# n = Money.ca_dollar(100)
# n.cents #=> 100
# n.currency #=> #<Money::Currency id: cad>
def self.ca_dollar(cents)
Money.new(cents, "CAD")
end
# Creates a new Money object of the given value, using the American dollar
# currency.
#
# @param [Integer] cents The cents value.
#
# @return [Money]
#
# @example
# n = Money.us_dollar(100)
# n.cents #=> 100
# n.currency #=> #<Money::Currency id: usd>
def self.us_dollar(cents)
Money.new(cents, "USD")
end
# Creates a new Money object of the given value, using the Euro currency.
#
# @param [Integer] cents The cents value.
#
# @return [Money]
#
# @example
# n = Money.euro(100)
# n.cents #=> 100
# n.currency #=> #<Money::Currency id: eur>
def self.euro(cents)
Money.new(cents, "EUR")
end
# Creates a new Money object of +amount+ value in dollars,
# with given +currency+.
#
# The amount value is expressed in +dollars+
# where the +dollar+ is the main monetary unit,
# opposite to the subunit-based representation
# used internally by this library called +cents+.
#
# @param [Numeric] amount The money amount, in dollars.
# @param [Currency, String, Symbol] currency The currency format.
# @param [Money::Bank::*] bank The exchange bank to use.
#
# @return [Money]
#
# @example
# Money.new_with_dollars(100)
# #=> #<Money @cents=10000 @currency="USD">
# Money.new_with_dollars(100, "USD")
# #=> #<Money @cents=10000 @currency="USD">
# Money.new_with_dollars(100, "EUR")
# #=> #<Money @cents=10000 @currency="EUR">
#
# @see Money.new
#
def self.new_with_dollars(amount, currency = Money.default_currency, bank = Money.default_bank)
money = from_numeric(amount, currency)
# Hack! You can't change a bank
money.instance_variable_set("@bank", bank)
money
end
# Adds a new exchange rate to the default bank and return the rate.
#
# @param [Currency, String, Symbol] from_currency Currency to exchange from.
# @param [Currency, String, Symbol] to_currency Currency to exchange to.
# @param [Numeric] rate Rate to exchange with.
#
# @return [Numeric]
#
# @example
# Money.add_rate("USD", "CAD", 1.25) #=> 1.25
def self.add_rate(from_currency, to_currency, rate)
Money.default_bank.add_rate(from_currency, to_currency, rate)
end
# Creates a new Money object of +cents+ value in cents,
# with given +currency+.
#
# Alternatively you can use the convenience
# methods like {Money.ca_dollar} and {Money.us_dollar}.
#
# @param [Integer] cents The money amount, in cents.
# @param [Currency, String, Symbol] currency The currency format.
# @param [Money::Bank::*] bank The exchange bank to use.
#
# @return [Money]
#
# @example
# Money.new(100)
# #=> #<Money @cents=100 @currency="USD">
# Money.new(100, "USD")
# #=> #<Money @cents=100 @currency="USD">
# Money.new(100, "EUR")
# #=> #<Money @cents=100 @currency="EUR">
#
# @see Money.new_with_dollars
#
def initialize(cents, currency = Money.default_currency, bank = Money.default_bank)
@cents = cents.round.to_i
@currency = Currency.wrap(currency)
@bank = bank
end
# Returns the value of the money in dollars,
# instead of in cents.
#
# @return [Float]
#
# @example
# Money.new(100).dollars # => 1.0
# Money.new_with_dollars(1).dollar # => 1.0
#
# @see #to_f
# @see #cents
#
def dollars
to_f
end
# Return string representation of currency object
#
# @return [String]
#
# @example
# Money.new(100, :USD).currency_as_string #=> "USD"
def currency_as_string
currency.to_s
end
# Set currency object using a string
#
# @param [String] val The currency string.
#
# @return [Money::Currency]
#
# @example
# Money.new(100).currency_as_string("CAD") #=> #<Money::Currency id: cad>
def currency_as_string=(val)
@currency = Currency.wrap(val)
end
# Returns a Fixnum hash value based on the +cents+ and +currency+ attributes
# in order to use functions like & (intersection), group_by, etc.
#
# @return [Fixnum]
#
# @example
# Money.new(100).hash #=> 908351
def hash
[cents.hash, currency.hash].hash
end
# Uses +Currency#symbol+. If +nil+ is returned, defaults to "¤".
#
# @return [String]
#
# @example
# Money.new(100, "USD").symbol #=> "$"
def symbol
currency.symbol || "¤"
end
# Common inspect function
#
# @return [String]
def inspect
"#<Money cents:#{cents} currency:#{currency}>"
end
# Returns the amount of money as a string.
#
# @return [String]
#
# @example
# Money.ca_dollar(100).to_s #=> "1.00"
def to_s
unit, subunit = cents.abs.divmod(currency.subunit_to_unit).map{|o| o.to_s}
if currency.decimal_places == 0
return "-#{unit}" if cents < 0
return unit
end
subunit = (("0" * currency.decimal_places) + subunit)[(-1*currency.decimal_places)..-1]
return "-#{unit}#{decimal_mark}#{subunit}" if cents < 0
"#{unit}#{decimal_mark}#{subunit}"
end
# Return the amount of money as a BigDecimal.
#
# @return [BigDecimal]
#
# @example
# Money.us_dollar(100).to_d => BigDecimal.new("1.0")
def to_d
BigDecimal.new(cents.to_s) / BigDecimal.new(currency.subunit_to_unit.to_s)
end
# Return the amount of money as a float. Floating points cannot guarantee
# precision. Therefore, this function should only be used when you no longer
# need to represent currency or working with another system that requires
# decimals.
#
# @return [Float]
#
# @example
# Money.us_dollar(100).to_f => 1.0
def to_f
to_d.to_f
end
# Conversation to +self+.
#
# @return [self]
def to_money(given_currency = nil)
given_currency = Currency.wrap(given_currency) if given_currency
if given_currency.nil? || self.currency == given_currency
self
else
exchange_to(given_currency)
end
end
# Receive the amount of this money object in another Currency.
#
# @param [Currency, String, Symbol] other_currency Currency to exchange to.
#
# @return [Money]
#
# @example
# Money.new(2000, "USD").exchange_to("EUR")
# Money.new(2000, "USD").exchange_to(Currency.new("EUR"))
def exchange_to(other_currency)
other_currency = Currency.wrap(other_currency)
@bank.exchange_with(self, other_currency)
end
# Receive a money object with the same amount as the current Money object
# in american dollars.
#
# @return [Money]
#
# @example
# n = Money.new(100, "CAD").as_us_dollar
# n.currency #=> #<Money::Currency id: usd>
def as_us_dollar
exchange_to("USD")
end
# Receive a money object with the same amount as the current Money object
# in canadian dollar.
#
# @return [Money]
#
# @example
# n = Money.new(100, "USD").as_ca_dollar
# n.currency #=> #<Money::Currency id: cad>
def as_ca_dollar
exchange_to("CAD")
end
# Receive a money object with the same amount as the current Money object
# in euro.
#
# @return [Money]
#
# @example
# n = Money.new(100, "USD").as_euro
# n.currency #=> #<Money::Currency id: eur>
def as_euro
exchange_to("EUR")
end
# Allocates money between different parties without loosing pennies.
# After the mathmatically split has been performed, left over pennies will
# be distributed round-robin amongst the parties. This means that parties
# listed first will likely recieve more pennies then ones that are listed later
#
# @param [0.50, 0.25, 0.25] to give 50% of the cash to party1, 25% ot party2, and 25% to party3.
#
# @return [Array<Money, Money, Money>]
#
# @example
# Money.new(5, "USD").allocate([0.3,0.7)) #=> [Money.new(2), Money.new(3)]
# Money.new(100, "USD").allocate([0.33,0.33,0.33]) #=> [Money.new(34), Money.new(33), Money.new(33)]
def allocate(splits)
allocations = splits.inject(0.0) {|sum, i| sum += i }
raise ArgumentError, "splits add to more then 100%" if (allocations - 1.0) > Float::EPSILON
left_over = cents
amounts = splits.collect do |ratio|
fraction = (cents * ratio / allocations).floor
left_over -= fraction
fraction
end
left_over.times { |i| amounts[i % amounts.length] += 1 }
amounts.collect { |cents| Money.new(cents, currency) }
end
# Split money amongst parties evenly without loosing pennies.
#
# @param [2] number of parties.
#
# @return [Array<Money, Money, Money>]
#
# @example
# Money.new(100, "USD").split(3) #=> [Money.new(34), Money.new(33), Money.new(33)]
def split(num)
raise ArgumentError, "need at least one party" if num < 1
low = Money.new(cents / num)
high = Money.new(low.cents + 1)
remainder = cents % num
result = []
num.times do |index|
result[index] = index < remainder ? high : low
end
result
end
end