class Money::RatesStore::Memory
store.each_rate {|iso_from, iso_to, rate| puts “#{from} -> #{to}: #{rate}” }
# iterates rates
store.get_rate ‘USD’, ‘CAD’ # => 0.98
store.add_rate ‘USD’, ‘CAD’, 0.98
store = Money::RatesStore::Memory.new
@example
Used by instances of Money::Bank::VariableExchange.
Class for thread-safe storage of exchange rate pairs.
def add_rate(currency_iso_from, currency_iso_to, rate)
-
(Numeric)-
Parameters:
-
rate(Numeric) -- Rate to use when exchanging currencies. -
currency_iso_to(String) -- Currency to exchange to. -
currency_iso_from(String) -- Currency to exchange from.
def add_rate(currency_iso_from, currency_iso_to, rate) guard.synchronize do rates[rate_key_for(currency_iso_from, currency_iso_to)] = rate end end
def each_rate(&block)
-
(Enumerator)-
Other tags:
- Yieldparam: rate - Exchange rate.
Yieldparam: iso_to - Currency ISO string.
Yieldparam: iso_from - Currency ISO string.
def each_rate(&block) return to_enum(:each_rate) unless block_given? guard.synchronize do rates.each do |key, rate| iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR) yield iso_from, iso_to, rate end end end
def get_rate(currency_iso_from, currency_iso_to)
-
(Numeric)-
Parameters:
-
currency_iso_to(String) -- Currency to exchange to. -
currency_iso_from(String) -- Currency to exchange from.
def get_rate(currency_iso_from, currency_iso_to) guard.synchronize do rates[rate_key_for(currency_iso_from, currency_iso_to)] end end
def initialize(opts = {}, rates = {})
-
rates(Hash) -- Optional initial exchange rate data. -
opts(Hash) -- Optional store options.
Options Hash:
(**opts)-
:without_mutex(Boolean) -- disables the usage of a mutex
def initialize(opts = {}, rates = {}) @rates = rates @options = opts @guard = Monitor.new end
def marshal_dump
def marshal_dump guard.synchronize do return [self.class, options, rates.dup] end end
def rate_key_for(currency_iso_from, currency_iso_to)
-
(String)-
Parameters:
-
currency_iso_to(String) -- The currency to exchange to. -
currency_iso_from(String) -- The currency to exchange from.
def rate_key_for(currency_iso_from, currency_iso_to) [currency_iso_from, currency_iso_to].join(INDEX_KEY_SEPARATOR).upcase end
def transaction(&block)
def transaction(&block) guard.synchronize do yield end end