class Money
Attributes
amount[RW]
currency[RW]
Public Class Methods
conversion_rates(base_currency, conversion_rates)
click to toggle source
Setting the different conversion rates with respect to the base currency
Parameters:¶ ↑
- base_currency
-
The base currency upon which the conversion rates should calculate, example: 'EUR', 'USD'
conversion_rates
-
A hash containing the different exchange rates' currencies and values with respect to the base currency, example: {
'USD' => 1.11, 'Bitcoin' => 0.0047
}
# File lib/money_test.rb, line 29 def self.conversion_rates(base_currency, conversion_rates) @@base_currency = base_currency @@conversion_rates = conversion_rates end
getRate(from_currency, to_currency)
click to toggle source
Getting the exchange rate between 2 currencies
Returns:¶ ↑
- 1 if the 2 currencies are the same - the required rate, even if the 2 currencies are swaped (return inverse the value)
Raises:¶ ↑
- RateNotFound if no rates are set for those 2 currencies
# File lib/money_test.rb, line 99 def self.getRate(from_currency, to_currency) return 1 if to_currency == from_currency if !@@conversion_rates[to_currency].nil? && from_currency == @@base_currency return @@conversion_rates[to_currency] end if !@@conversion_rates[from_currency].nil? && to_currency == @@base_currency return 1.0 / @@conversion_rates[from_currency] end raise Exceptions::RateNotFound end
new(amount, currency)
click to toggle source
Constructor taking the money amount and the currency
# File lib/money_test.rb, line 14 def initialize(amount, currency) @amount = amount.to_f @currency = currency end
Public Instance Methods
*(value)
click to toggle source
# File lib/money_test.rb, line 59 def *(value) Money.new(self.amount * value.to_f, self.currency) end
+(other)
click to toggle source
Overriding arithmetic operators
# File lib/money_test.rb, line 47 def +(other) rate = Money.getRate(other.currency, self.currency) new_other_amount = other.amount * rate Money.new(self.amount + new_other_amount, self.currency) end
-(other)
click to toggle source
# File lib/money_test.rb, line 53 def -(other) rate = Money.getRate(other.currency, self.currency) new_other_amount = other.amount * rate Money.new(self.amount - new_other_amount, self.currency) end
/(value)
click to toggle source
# File lib/money_test.rb, line 63 def /(value) Money.new(self.amount / value.to_f, self.currency) end
<(other)
click to toggle source
# File lib/money_test.rb, line 79 def <(other) temp = other.convert_to(self.currency) return self.amount.round(2) < temp.amount.round(2) end
==(other)
click to toggle source
Overriding comparisons operators, to the nearest 2 decimal places
# File lib/money_test.rb, line 69 def ==(other) temp = other.convert_to(self.currency) return self.amount.round(2) == temp.amount.round(2) end
>(other)
click to toggle source
# File lib/money_test.rb, line 74 def >(other) temp = other.convert_to(self.currency) return self.amount.round(2) > temp.amount.round(2) end
convert_to(to_currency)
click to toggle source
Converting the amount in the calling object upon the currency entered, with respect to the base currency
Returns:¶ ↑
A money object with the new amount and currency
# File lib/money_test.rb, line 37 def convert_to(to_currency) from_currency = self.currency rate = Money.getRate(from_currency, to_currency) converted_amount = self.amount * rate Money.new(converted_amount, to_currency) end
inspect()
click to toggle source
# File lib/money_test.rb, line 85 def inspect return sprintf('%.2f %s', amount, currency) end
to_s()
click to toggle source
# File lib/money_test.rb, line 89 def to_s return sprintf('%.2f %s', amount, currency) end