class Danconia::Money

Attributes

amount[R]
currency[R]
decimals[R]

Public Class Methods

new(amount, currency_code = nil, decimals: 2, exchange_opts: {}) click to toggle source
# 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

Public Instance Methods

<=>(other) click to toggle source
# File lib/danconia/money.rb, line 46
def <=> other
  amount <=> amount_exchanged_to_this_currency(other)
end
==(other) click to toggle source
# 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?() click to toggle source
# File lib/danconia/money.rb, line 73
def default_currency?
  currency.code == Danconia.config.default_currency
end
eql?(other) click to toggle source
# File lib/danconia/money.rb, line 38
def eql? other
  self == other
end
exchange_to(other_currency, **opts) click to toggle source
# 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) click to toggle source
# 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
Also aliased as: to_s
hash() click to toggle source
# File lib/danconia/money.rb, line 42
def hash
  [amount, currency].hash
end
in_cents() click to toggle source
# File lib/danconia/money.rb, line 69
def in_cents
  (self * 100).round
end
inspect() click to toggle source
# File lib/danconia/money.rb, line 26
def inspect
  "#{amount} #{currency.code}"
end
method_missing(method, *args) click to toggle source
Calls superclass method
# 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
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/danconia/money.rb, line 85
def respond_to_missing? method, *args
  @amount.respond_to?(method, *args) || super
end
round(*args) click to toggle source
# File lib/danconia/money.rb, line 65
def round *args
  clone_with amount.round(*args)
end
to_s(decimals: @decimals, **other_options)
Alias for: format

Private Instance Methods

amount_exchanged_to_this_currency(other) click to toggle source
# File lib/danconia/money.rb, line 99
def amount_exchanged_to_this_currency other
  if other.is_a? Money
    other.exchange_to(currency, @exchange_opts).amount
  else
    other
  end
end
clone_with(amount, currency = @currency, exchange_opts = @exchange_opts) click to toggle source
# File lib/danconia/money.rb, line 95
def clone_with amount, currency = @currency, exchange_opts = @exchange_opts
  Money.new amount, currency, decimals: @decimals, exchange_opts: exchange_opts
end
parse(object) click to toggle source
# File lib/danconia/money.rb, line 91
def parse object
  BigDecimal(object.to_s) rescue BigDecimal(0)
end