class SimpleExchangeRate::Conversion
Attributes
base_currency[R]
conversion_currency[R]
date[R]
Public Class Methods
new(database)
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 53 def initialize(database) @database = database end
Public Instance Methods
base_currency=(value)
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 63 def base_currency=(value) raise InvalidCurrency unless valid_currency?(value) @base_currency = value end
conversion_currency=(value)
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 69 def conversion_currency=(value) raise InvalidCurrency unless valid_currency?(value) @conversion_currency = value end
date=(value)
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 57 def date=(value) @date = Date.iso8601(value.to_s) rescue ArgumentError raise InvalidDate end
rate()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 75 def rate return BASE_RATE if @base_currency == @conversion_currency raise OutOfRangeDate if rates_for_date.nil? if @base_currency == INITIAL_BASE_CURRENCY standard_rate else cross_rate end rescue KeyError raise DatabaseError end
rates_for_date()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 89 def rates_for_date @rates_for_date ||= @database.all.fetch(date.to_s, nil) end
Private Instance Methods
calculate_cross_rate_through_mutual_rate()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 111 def calculate_cross_rate_through_mutual_rate base_to_currency = rates_for_date.fetch(@conversion_currency).to_f base_to_new_base = rates_for_date.fetch(@base_currency).to_f (base_to_currency / base_to_new_base).round(PRECISION_NUMBER) end
converting_to_base_currency()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 103 def converting_to_base_currency INITIAL_BASE_CURRENCY == @conversion_currency end
cross_rate()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 118 def cross_rate if converting_to_base_currency inverse_of_base_currency_rate else calculate_cross_rate_through_mutual_rate end end
inverse_of_base_currency_rate()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 107 def inverse_of_base_currency_rate (1 / rates_for_date.fetch(@base_currency).to_f).round(PRECISION_NUMBER) end
standard_rate()
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 99 def standard_rate rates_for_date.fetch(@conversion_currency, nil).to_f end
valid_currency?(value)
click to toggle source
# File lib/simple_exchange_rate/conversion.rb, line 95 def valid_currency?(value) return true if SUPPORTED_CURRENCIES.include?(value.to_s.upcase) end