class ForeignCurrencyExchange::Money
This class represents a money object.
Constants
- AMOUNT_FORMAT
- InvalidBaseCurrencyRateError
- InvalidRatesError
- ROUND_LEVEL
- UnknownCurrencyError
Attributes
amount[R]
currency[R]
Public Class Methods
new(amount, currency)
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 17 def initialize(amount, currency) check_configuration # Configuration should be valid. check_currency(currency) # Currency should be configured before. @amount = prepare_amount(amount) @currency = currency end
Public Instance Methods
*(multiplier)
click to toggle source
Returns new money object with multiplied current object amount,
parameter multiplier should be kind of Numeric object.
# File lib/foreign_currency_exchange/money.rb, line 47 def *(multiplier) new(amount * multiplier, currency) end
+(other)
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 35 def +(other) new_amount = amount + other.convert_to(currency).amount new(new_amount, currency) end
-(other)
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 40 def -(other) new_amount = amount - other.convert_to(currency).amount new(new_amount, currency) end
/(divider)
click to toggle source
Returns new money object with divided current object amount,
parameter divider should be kind of Numeric object.
# File lib/foreign_currency_exchange/money.rb, line 53 def /(divider) raise ZeroDivisionError if divider.zero? new(amount / divider.to_f, currency) end
<=>(other)
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 31 def <=>(other) base_amount <=> other.base_amount end
base_amount()
click to toggle source
Returns current object amount if currency equals to base_currency
or converted current object amount to base_currency otherwise.
# File lib/foreign_currency_exchange/money.rb, line 26 def base_amount return amount if currency == base_currency prepare_amount(amount / rates[currency].to_f) end
convert_to(other_currency)
click to toggle source
Returns new money object converted to given currency,
raises UnknownCurrencyError in case when given currency is unknown.
# File lib/foreign_currency_exchange/money.rb, line 68 def convert_to(other_currency) new_amount = calculate_amount(other_currency) new(new_amount, other_currency) end
inspect()
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 62 def inspect to_s end
to_s()
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 58 def to_s "#{formatted_amount} #{currency}" end
Private Instance Methods
calculate_amount(other_currency)
click to toggle source
Returns amount based on given currency,
raises UnknownCurrencyError in case when given other_currency is unknown.
# File lib/foreign_currency_exchange/money.rb, line 85 def calculate_amount(other_currency) check_currency(other_currency) return amount if other_currency == currency return base_amount if other_currency == base_currency base_amount * rates[other_currency] end
check_currency(currency)
click to toggle source
Checks that currency is known otherwise raises UnknownCurrencyError
.
# File lib/foreign_currency_exchange/money.rb, line 93 def check_currency(currency) return if base_currency == currency || rates.keys.include?(currency) raise UnknownCurrencyError end
formatted_amount()
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 79 def formatted_amount amount.integer? ? amount.to_s : format(AMOUNT_FORMAT, amount) end
prepare_amount(amount)
click to toggle source
# File lib/foreign_currency_exchange/money.rb, line 75 def prepare_amount(amount) amount.integer? ? amount : amount.round(ROUND_LEVEL) end