Class: Danconia::Money
- Inherits:
-
Object
show all
- Includes:
- Comparable
- Defined in:
- lib/danconia/money.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(amount, currency_code = nil, decimals: 2, exchange_opts: {}) ⇒ Money
Returns a new instance of Money.
9
10
11
12
13
14
|
# File 'lib/danconia/money.rb', line 9
def initialize(amount, currency_code = nil, decimals: 2, exchange_opts: {})
@amount = parse amount
@decimals = decimals
@currency = Currency.find(currency_code || Danconia.config.default_currency)
@exchange_opts = exchange_opts.reverse_merge(exchange: Danconia.config.default_exchange)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/danconia/money.rb', line 78
def method_missing method, *args
if @amount.respond_to? method
@amount.send method, *args
else
super
end
end
|
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
7
8
9
|
# File 'lib/danconia/money.rb', line 7
def amount
@amount
end
|
#currency ⇒ Object
Returns the value of attribute currency.
7
8
9
|
# File 'lib/danconia/money.rb', line 7
def currency
@currency
end
|
#decimals ⇒ Object
Returns the value of attribute decimals.
7
8
9
|
# File 'lib/danconia/money.rb', line 7
def decimals
@decimals
end
|
Instance Method Details
#<=>(other) ⇒ Object
43
44
45
|
# File 'lib/danconia/money.rb', line 43
def <=> other
amount <=> amount_exchanged_to_this_currency(other)
end
|
#==(other) ⇒ Object
27
28
29
30
31
32
33
|
# File 'lib/danconia/money.rb', line 27
def == other
if other.is_a?(Money)
amount == other.amount && currency == other.currency
else
amount == other && currency.code == Danconia.config.default_currency
end
end
|
#as_json(*args) ⇒ Object
70
71
72
|
# File 'lib/danconia/money.rb', line 70
def as_json *args
amount.as_json *args
end
|
#default_currency? ⇒ Boolean
74
75
76
|
# File 'lib/danconia/money.rb', line 74
def default_currency?
currency.code == Danconia.config.default_currency
end
|
#eql?(other) ⇒ Boolean
35
36
37
|
# File 'lib/danconia/money.rb', line 35
def eql? other
self == other
end
|
#exchange_to(other_currency, **opts) ⇒ Object
47
48
49
50
51
52
|
# File 'lib/danconia/money.rb', line 47
def exchange_to other_currency, **opts
opts = @exchange_opts.merge(opts)
other_currency = other_currency.presence && Currency.find(other_currency) || currency
rate = opts[:exchange].rate currency.code, other_currency.code, opts.except(:exchange)
clone_with amount * rate, other_currency, opts
end
|
16
17
18
19
|
# File 'lib/danconia/money.rb', line 16
def format decimals: @decimals, **other_options
opts = other_options.reverse_merge precision: decimals, unit: currency.symbol
ActiveSupport::NumberHelper.number_to_currency amount, opts
end
|
#hash ⇒ Object
39
40
41
|
# File 'lib/danconia/money.rb', line 39
def hash
[amount, currency].hash
end
|
#in_cents ⇒ Object
66
67
68
|
# File 'lib/danconia/money.rb', line 66
def in_cents
(self * 100).round
end
|
#inspect ⇒ Object
23
24
25
|
# File 'lib/danconia/money.rb', line 23
def inspect
"#{amount} #{currency.code}"
end
|
#respond_to?(method, *args) ⇒ Boolean
86
87
88
|
# File 'lib/danconia/money.rb', line 86
def respond_to? method, *args
super or @amount.respond_to?(method, *args)
end
|
#round(*args) ⇒ Object
62
63
64
|
# File 'lib/danconia/money.rb', line 62
def round *args
clone_with amount.round(*args)
end
|