class Devilicious::Money

Attributes

currency[R]

Public Class Methods

new(amount, currency) click to toggle source
Calls superclass method
# File lib/devilicious/money.rb, line 7
def initialize(amount, currency)
  @currency = currency

  super(amount)
end

Public Instance Methods

*(other) click to toggle source
Calls superclass method
# File lib/devilicious/money.rb, line 37
def *(other)
  assert_currency! other
  self.class.new super, currency
end
+(other) click to toggle source
Calls superclass method
# File lib/devilicious/money.rb, line 27
def +(other)
  assert_currency! other
  self.class.new super, currency
end
-(other) click to toggle source
Calls superclass method
# File lib/devilicious/money.rb, line 32
def -(other)
  assert_currency! other
  self.class.new super, currency
end
/(other) click to toggle source
Calls superclass method
# File lib/devilicious/money.rb, line 42
def /(other)
  assert_currency! other
  self.class.new super, currency
end
exchange_to(new_currency) click to toggle source
# File lib/devilicious/money.rb, line 13
def exchange_to new_currency
  new_amount = CurrencyConverter.convert(self, currency, new_currency)

  self.class.new new_amount, new_currency
end
inspect() click to toggle source
# File lib/devilicious/money.rb, line 23
def inspect
  "#<Devilicious::Money amount=#{to_s}>"
end
to_s() click to toggle source
# File lib/devilicious/money.rb, line 19
def to_s
  sprintf("%.#{decimal_places}f", self) << " " << currency
end

Private Instance Methods

assert_currency!(other) click to toggle source
# File lib/devilicious/money.rb, line 59
def assert_currency!(other)
  raise "Currency mismatch: #{self.inspect} #{other.inspect}" if other.is_a?(self.class) && other.currency != currency
end
decimal_places() click to toggle source
# File lib/devilicious/money.rb, line 49
def decimal_places
  max = 6 # NOTE: don't care about extra decimals

  2.upto(max) do |i|
    return i if self == self.round(i)
  end

  max
end