class CurrencyConverter3000::Money

Attributes

currency[R]

Public Class Methods

conversion_rates(default_currency, conversion_hash) click to toggle source

Setup the Default Currency and the coresponding conversion rates

# File lib/currency_converter_3000.rb, line 7
def conversion_rates default_currency, conversion_hash
  @@default_currency = default_currency
  # Hash of conversion rates from Default Currency
  # Each key should represent a currency
  # Each value should represent the rate of conversion
  # Example :
  # {
  #   'USD' => 1.10,
  #   'BTC' => 0.00091
  # }
  @@conversion_hash = conversion_hash
end
new(amount, currency) click to toggle source
# File lib/currency_converter_3000.rb, line 21
def initialize amount, currency
  @amount = amount
  @currency = currency
end

Public Instance Methods

*(number) click to toggle source
# File lib/currency_converter_3000.rb, line 61
def * number
  Money.new (amount * number).round(2), @currency
end
+(money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 53
def + money_object
  Money.new apply_operator(:+, money_object), @currency
end
-(money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 57
def - money_object
  Money.new apply_operator(:-, money_object), @currency
end
/(number) click to toggle source
# File lib/currency_converter_3000.rb, line 65
def / number
  Money.new (amount / number).round(2), @currency
end
<(money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 77
def < money_object
  apply_operator(:<, money_object)
end
==(money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 69
def == money_object
  apply_operator(:==, money_object)
end
>(money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 73
def > money_object
  apply_operator(:>, money_object)
end
amount() click to toggle source
# File lib/currency_converter_3000.rb, line 49
def amount
  @amount.to_f.round(2)
end
apply_operator(operator, money_object) click to toggle source
# File lib/currency_converter_3000.rb, line 81
def apply_operator operator, money_object
  if @currency == money_object.currency
    amount.send(operator, money_object.amount)
  else
    amount.send(operator, money_object.convert_to(@currency).amount)
  end
end
convert_to(new_currency) click to toggle source

These are the 4 Covered Conversion cases: Currency -> Default Currency Same Currency -> Same Currency Currency -> New Currency Default Currency -> Currency

# File lib/currency_converter_3000.rb, line 31
def convert_to new_currency
  if new_currency == @@default_currency && new_currency != @currency
    # convert from non default currency to default currency
    Money.new @amount / @@conversion_hash[@currency], new_currency
  elsif new_currency == @currency
    # convert from non default currency to same non default currency
    self
  elsif new_currency != @@default_currency && @currency != @@default_currency
    # convert from non default currency to other non default currency
    # Currency -> Default Currency
    # Default Currency -> New Currency
    convert_to(@@default_currency).convert_to(new_currency)
  elsif @@conversion_hash[new_currency]
    # convert from default currency to non default currency
    Money.new @amount * @@conversion_hash[new_currency], new_currency
  end
end
inspect() click to toggle source
# File lib/currency_converter_3000.rb, line 89
def inspect
  "#{sprintf('%.2f', @amount)} #@currency"
end