Class: Danconia::Money

Inherits:
Object
  • Object
show all
Includes:
Comparable, Serializable
Defined in:
lib/danconia/money.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#as_json, #marshal_dump, #marshal_load

Constructor Details

#initialize(amount, currency_code = nil, decimals: 2, exchange_opts: {}) ⇒ Money

Returns a new instance of Money.



12
13
14
15
16
17
# File 'lib/danconia/money.rb', line 12

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



77
78
79
80
81
82
83
# File 'lib/danconia/money.rb', line 77

def method_missing method, *args
  if @amount.respond_to? method
    @amount.send method, *args
  else
    super
  end
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



10
11
12
# File 'lib/danconia/money.rb', line 10

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



10
11
12
# File 'lib/danconia/money.rb', line 10

def currency
  @currency
end

#decimalsObject (readonly)

Returns the value of attribute decimals.



10
11
12
# File 'lib/danconia/money.rb', line 10

def decimals
  @decimals
end

Instance Method Details

#<=>(other) ⇒ Object



46
47
48
# File 'lib/danconia/money.rb', line 46

def <=> other
  amount <=> amount_exchanged_to_this_currency(other)
end

#==(other) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/danconia/money.rb', line 30

def == other
  if other.is_a?(Money)
    amount == other.amount && currency == other.currency
  else
    amount == other && currency.code == Danconia.config.default_currency
  end
end

#default_currency?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/danconia/money.rb', line 73

def default_currency?
  currency.code == Danconia.config.default_currency
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/danconia/money.rb', line 38

def eql? other
  self == other
end

#exchange_to(other_currency, **opts) ⇒ Object



50
51
52
53
54
55
# File 'lib/danconia/money.rb', line 50

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

#format(decimals: @decimals, **other_options) ⇒ Object Also known as: to_s



19
20
21
22
# File 'lib/danconia/money.rb', line 19

def format decimals: @decimals, **other_options
  opts = other_options.reverse_merge precision: decimals, unit: currency.symbol
  ActiveSupport::NumberHelper.number_to_currency amount, opts
end

#hashObject



42
43
44
# File 'lib/danconia/money.rb', line 42

def hash
  [amount, currency].hash
end

#in_centsObject



69
70
71
# File 'lib/danconia/money.rb', line 69

def in_cents
  (self * 100).round
end

#inspectObject



26
27
28
# File 'lib/danconia/money.rb', line 26

def inspect
  "#{amount} #{currency.code}"
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/danconia/money.rb', line 85

def respond_to_missing? method, *args
  @amount.respond_to?(method, *args) || super
end

#round(*args) ⇒ Object



65
66
67
# File 'lib/danconia/money.rb', line 65

def round *args
  clone_with amount.round(*args)
end