-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
money.rb
94 lines (77 loc) · 2.57 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
class Money
include Comparable, Arithmetic
include ActiveModel::Validations
attr_reader :amount, :currency
validate :source_must_be_of_known_type
class << self
def default_currency
@default ||= Money::Currency.new(:usd)
end
def default_currency=(object)
@default = Money::Currency.new(object)
end
end
def initialize(obj, currency = Money.default_currency)
@source = obj
@amount = obj.is_a?(Money) ? obj.amount : BigDecimal(obj.to_s)
@currency = obj.is_a?(Money) ? obj.currency : Money::Currency.new(currency)
validate!
end
# TODO: Replace with injected rate store
def exchange_to(other_currency, date = Date.current)
if currency == Money::Currency.new(other_currency)
self
elsif rate = ExchangeRate.find_rate(from: currency, to: other_currency, date: date)
Money.new(amount * rate.rate, other_currency)
end
end
def cents_str(precision = currency.default_precision)
format_str = "%.#{precision}f"
amount_str = format_str % amount
parts = amount_str.split(currency.separator)
if parts.length < 2
""
else
parts.last.ljust(precision, "0")
end
end
# Use `format` for basic formatting only.
# Use the Rails number_to_currency helper for more advanced formatting.
def format
whole_part, fractional_part = sprintf("%.#{currency.default_precision}f", amount).split(".")
whole_with_delimiters = whole_part.chars.to_a.reverse.each_slice(3).map(&:join).join(currency.delimiter).reverse
formatted_amount = "#{whole_with_delimiters}#{currency.separator}#{fractional_part}"
currency.default_format.gsub("%n", formatted_amount).gsub("%u", currency.symbol)
end
alias_method :to_s, :format
def as_json
{ amount: amount, currency: currency.iso_code }.as_json
end
def <=>(other)
raise TypeError, "Money can only be compared with other Money objects except for 0" unless other.is_a?(Money) || other.eql?(0)
if other.is_a?(Numeric)
amount <=> other
else
amount_comparison = amount <=> other.amount
if amount_comparison == 0
currency <=> other.currency
else
amount_comparison
end
end
end
def default_format_options
{
unit: currency.symbol,
precision: currency.default_precision,
delimiter: currency.delimiter,
separator: currency.separator
}
end
private
def source_must_be_of_known_type
unless @source.is_a?(Money) || @source.is_a?(Numeric) || @source.is_a?(BigDecimal)
errors.add :source, "must be a Money, Numeric, or BigDecimal"
end
end
end