class Money

Attributes

amount[R]
currency[R]

Public Class Methods

conversion_rates(from,rates={}) click to toggle source
# File lib/currency_convert/money.rb, line 16
def self.conversion_rates(from,rates={})
  @@origin_currency = from
  @@rates = rates
end
new(amount, currency) click to toggle source
# File lib/currency_convert/money.rb, line 6
def initialize(amount, currency)
  @amount = amount
  @currency = currency 
end

Public Instance Methods

*(num) click to toggle source
# File lib/currency_convert/money.rb, line 46
def * num
  Money.new(amount*num, currency)
end
+(another_money) click to toggle source
# File lib/currency_convert/money.rb, line 32
def + another_money
  another_money_amount = another_money.convert_to(currency).amount
  Money.new(amount+another_money_amount, currency)
end
-(another_money) click to toggle source
# File lib/currency_convert/money.rb, line 37
def - another_money
  another_money_amount = another_money.convert_to(currency).amount
  Money.new(amount-another_money_amount, currency)
end
/(num) click to toggle source
# File lib/currency_convert/money.rb, line 42
def / num
  Money.new(amount/num, currency)
end
<=>(another_money) click to toggle source
# File lib/currency_convert/money.rb, line 50
def <=> another_money
  case (currency == another_money.currency)
    when true
      amount <=> another_money.amount
    when false
      money_1 = currency==@@origin_currency ? self.convert_to(another_money.currency).amount : amount 
      money_2 = another_money.currency==@@origin_currency ? another_money.convert_to(currency).amount : another_money.amount
      money_1 <=> money_2
  end
end
convert_to(to) click to toggle source
# File lib/currency_convert/money.rb, line 21
def convert_to(to)
  if to == @@origin_currency
    rate = @@rates[currency.to_sym]
    result = amount/rate
  else
    rate = @@rates[to.to_sym]
    result = amount * rate
  end
  Money.new(result, to)
end
inspect() click to toggle source
# File lib/currency_convert/money.rb, line 11
def inspect
 amount_f = "%0.2f" % @amount
 "#{amount_f} #{currency}"
end