class Converter::Money
Constants
- VERSION
Attributes
base_currency[R]
other_currencies[R]
amount[R]
currency[R]
Public Class Methods
conversion_rates(base_currency, other_currencies)
click to toggle source
# File lib/converter/money.rb, line 17 def self.conversion_rates(base_currency, other_currencies) validate_conversion_rates(base_currency, other_currencies) @base_currency = base_currency @other_currencies = other_currencies end
new(amount, currency)
click to toggle source
# File lib/converter/money.rb, line 23 def initialize(amount, currency) unless base_currency && other_currencies raise ConfigurationError, 'Configuration for conversion_rates is required' end validate(amount, currency) @amount = amount @currency = currency end
Public Instance Methods
convert_to(new_currency)
click to toggle source
# File lib/converter/money.rb, line 37 def convert_to(new_currency) validate_currency(new_currency) currency == new_currency ? self : self.class.new(calculate_amount_for(new_currency), new_currency) end
inspect()
click to toggle source
# File lib/converter/money.rb, line 33 def inspect "#{formatted_amount} #{currency}" end
Private Instance Methods
calculate_amount_for(new_currency)
click to toggle source
# File lib/converter/money.rb, line 48 def calculate_amount_for(new_currency) if new_currency == base_currency amount / other_currencies[currency] else amount * other_currencies[new_currency] end end
formatted_amount()
click to toggle source
# File lib/converter/money.rb, line 44 def formatted_amount "%.2f" % amount.round(2) end