Class: CurrencyConverter3000::Money

Inherits:
Object
  • Object
show all
Defined in:
lib/money.rb,
lib/currency_converter_3000.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency) ⇒ Money

Returns a new instance of Money.



12
13
14
15
# File 'lib/money.rb', line 12

def initialize amount, currency
  @amount = amount
  @currency = currency
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



3
4
5
# File 'lib/money.rb', line 3

def amount
  @amount
end

#currencyObject (readonly)

Returns the value of attribute currency.



3
4
5
# File 'lib/money.rb', line 3

def currency
  @currency
end

Class Method Details

.conversion_rates(default_currency, conversion_hash) ⇒ Object

Setup the Default Currency and the coresponding conversion rates



7
8
9
10
# File 'lib/currency_converter_3000.rb', line 7

def conversion_rates default_currency, conversion_hash
  @@default_currency = default_currency
  @@conversion_hash = conversion_hash
end

Instance Method Details

#*(number) ⇒ Object



33
34
35
# File 'lib/money.rb', line 33

def * number
  @amount * number
end

#+(money_object) ⇒ Object



25
26
27
# File 'lib/money.rb', line 25

def + money_object
  @amount + @@conversion_hash[money_object.currency]*money_object.amount
end

#-(money_object) ⇒ Object



29
30
31
# File 'lib/money.rb', line 29

def - money_object
  @amount - @@conversion_hash[money_object.currency]*money_object.amount
end

#/(number) ⇒ Object



37
38
39
# File 'lib/money.rb', line 37

def / number
  @amount / number
end

#<(money_object) ⇒ Object



77
78
79
# File 'lib/currency_converter_3000.rb', line 77

def < money_object
  apply_operator(:<, money_object)
end

#==(money_object) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/money.rb', line 41

def == money_object
  if @currency == money_object.currency
    @amount.round(2) == money_object.amount.round(2)
  else
    @amount.round(2) == (@@conversion_hash[money_object.currency] * money_object.amount).round(2)
  end
end

#>(money_object) ⇒ Object



73
74
75
# File 'lib/currency_converter_3000.rb', line 73

def > money_object
  apply_operator(:>, money_object)
end

#apply_operator(operator, money_object) ⇒ Object



81
82
83
84
85
86
87
# 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) ⇒ Object

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



31
32
33
34
35
36
37
# File 'lib/currency_converter_3000.rb', line 31

def convert_to currency
  if @@conversion_hash[currency]
    Money.new @amount * @@conversion_hash[currency], currency
  else
    'No conversion rate found.'
  end
end

#inspectObject



49
50
51
# File 'lib/money.rb', line 49

def inspect
  "#{sprintf('%.2f', @amount)} #@currency"
end