Class: Money::RatesStore::StoreWithDateSupport

Inherits:
Memory
  • Object
show all
Defined in:
lib/money/rates_store/store_with_date_support.rb

Overview

Class for thread-safe storage of exchange rates per date.

Constant Summary collapse

INDEX_DATE_SEPARATOR =
'_ON_'

Instance Method Summary collapse

Instance Method Details

#add_rate(currency_iso_from, currency_iso_to, rate, date = nil) ⇒ Object



11
12
13
14
15
# File 'lib/money/rates_store/store_with_date_support.rb', line 11

def add_rate(currency_iso_from, currency_iso_to, rate, date = nil)
  guard.synchronize do
    rates[rate_key_for(currency_iso_from, currency_iso_to, date)] = rate
  end
end

#each_rate {|iso_from, iso_to, rate, date| ... } ⇒ Enumerator

Iterate over exchange rate tuples

Examples:

store.each_rate do |iso_from, iso_to, rate, date|
  puts [iso_from, iso_to, rate, date].join
end

Yield Parameters:

  • iso_from (String)

    Currency ISO string.

  • iso_to (String)

    Currency ISO string.

  • rate (String)

    Exchange rate.

  • date (Date)

    Date of the exchange rate. Nil for current rate.

Returns:

  • (Enumerator)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/money/rates_store/store_with_date_support.rb', line 35

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)
      iso_to, date = iso_to.split(INDEX_DATE_SEPARATOR)
      date = Date.parse(date) if date
      yield iso_from, iso_to, rate, date
    end
  end
end

#get_rate(currency_iso_from, currency_iso_to, date = nil) ⇒ Object



17
18
19
20
21
# File 'lib/money/rates_store/store_with_date_support.rb', line 17

def get_rate(currency_iso_from, currency_iso_to, date = nil)
  guard.synchronize do
    rates[rate_key_for(currency_iso_from, currency_iso_to, date)]
  end
end