class Money

Constants

VERSION

Public Class Methods

new(amount, currency, date=nil) click to toggle source
Calls superclass method
# File lib/money/money.rb, line 5
def initialize(amount, currency, date=nil)
  super amount, Currency[currency], date
end

Public Instance Methods

*(other) click to toggle source
# File lib/money/money.rb, line 25
def *(other)
  calculate(:*, other)
end
+(other) click to toggle source
# File lib/money/money.rb, line 13
def +(other)
  calculate(:+, other)
end
-(other) click to toggle source
# File lib/money/money.rb, line 17
def -(other)
  calculate(:-, other)
end
/(other) click to toggle source
# File lib/money/money.rb, line 21
def /(other)
  calculate(:/, other)
end
coerce(other) click to toggle source
# File lib/money/money.rb, line 30
def coerce(other)
  [Money.new(other, currency, date), amount]
end
exchange_to(other_currency, on: date) click to toggle source
# File lib/money/money.rb, line 9
def exchange_to(other_currency, on: date)
  currency.to(Currency[other_currency], on: on) * amount
end
inspect() click to toggle source
# File lib/money/money.rb, line 42
def inspect
  "<#Money #{to_s}>"
end
to_s() click to toggle source
# File lib/money/money.rb, line 34
def to_s
  if date.nil?
    "#{amount} #{currency}"
  else
    "#{amount} #{currency} on #{date}"
  end
end

Protected Instance Methods

calculate(operation, other) click to toggle source
# File lib/money/money.rb, line 48
def calculate(operation, other)
  if other.kind_of?(Money)
    if other.date != date and other.date != nil and date != nil
      raise "Can't calculate the amount with different dates."
    end 
    new_date = date || other.date
    other = other.exchange_to(currency).amount
  else
    new_date = date
  end
  if other.kind_of?(Numeric)
    Money.new(amount.send(operation, other), currency, new_date)
  else
    raise TypeError, "#{other.class} can't be coerced into Money"
  end
end