class VlatkoDawanda::Money

Attributes

bank[W]

Public Class Methods

bank() click to toggle source
# File lib/vlatko_dawanda/money.rb, line 10
def bank
  @bank ||= Bank.new
end
conversion_rates(base_currency, currencies) click to toggle source
# File lib/vlatko_dawanda/money.rb, line 14
def conversion_rates(base_currency, currencies)
  bank.conversion_rates(base_currency, currencies)
end
new(amount, iso_code) click to toggle source
# File lib/vlatko_dawanda/money.rb, line 19
def initialize(amount, iso_code)
  @amount = amount.to_d
  @currency = bank.find_currency(iso_code)
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/vlatko_dawanda/money.rb, line 46
def <=>(other)
  if currency == other.currency
    amount.to_d.round(2) <=> other.amount.to_d.round(2)
  else
    amount.to_d.round(2) <=> other.convert_to(currency).amount.to_d.round(2)
  end
end
amount() click to toggle source
# File lib/vlatko_dawanda/money.rb, line 24
def amount
  to_f_or_i(@amount)
end
convert_to(currency) click to toggle source
# File lib/vlatko_dawanda/money.rb, line 36
def convert_to(currency)
  to_currency = bank.find_currency(currency)
  amount = if @currency == to_currency
    @amount
  else
    (@amount / @currency.rate) * to_currency.rate
  end
  self.class.new(amount, to_currency.iso_code)
end
currency() click to toggle source
# File lib/vlatko_dawanda/money.rb, line 28
def currency
  @currency.iso_code
end
inspect() click to toggle source
# File lib/vlatko_dawanda/money.rb, line 32
def inspect
  "#{"%.2f" % amount} #{currency}"
end

Private Instance Methods

bank() click to toggle source
# File lib/vlatko_dawanda/money.rb, line 56
def bank
  self.class.bank
end
to_f_or_i(v) click to toggle source
# File lib/vlatko_dawanda/money.rb, line 60
def to_f_or_i(v)
  ((float = v.to_f) && (float % 1.0 == 0) ? float.to_i : float) rescue v
end