class DwMoney::Money

Attributes

amount[R]
currency[R]

Public Class Methods

conversion_rates(base_currency, rates) click to toggle source
# File lib/dw_money/money.rb, line 14
def self.conversion_rates(base_currency, rates)
  @conversion_rates[base_currency] = rates
end
conversion_rates_configuration() click to toggle source
# File lib/dw_money/money.rb, line 18
def self.conversion_rates_configuration
  @conversion_rates
end
new(amount, currency) click to toggle source
# File lib/dw_money/money.rb, line 22
def initialize(amount, currency)
  @amount = amount
  @currency = currency
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/dw_money/money.rb, line 40
def <=>(other)
  amount.round(2) <=> other.convert_to(currency).amount.round(2)
end
convert_to(new_currency) click to toggle source
# File lib/dw_money/money.rb, line 31
def convert_to(new_currency)
  return self if new_currency == currency

  raise ConversionRateNotFound, "#{new_currency} => #{currency}" unless rates && rates[new_currency]

  new_amount = amount * rates[new_currency]
  Money.new(new_amount, new_currency)
end
inspect() click to toggle source
# File lib/dw_money/money.rb, line 27
def inspect
  "#{format('%.2f', amount)} #{currency}"
end

Private Instance Methods

rates() click to toggle source
# File lib/dw_money/money.rb, line 46
def rates
  self.class.conversion_rates_configuration[currency]
end