Class: Money::RatesStore::Memory

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

Overview

Class for thread-safe storage of exchange rate pairs. Used by instances of Money::Bank::VariableExchange.

Examples:

store = Money::RatesStore::Memory.new
store.add_rate 'USD', 'CAD', 0.98
store.get_rate 'USD', 'CAD' # => 0.98
# iterates rates
store.each_rate {|iso_from, iso_to, rate| puts "#{from} -> #{to}: #{rate}" }

Constant Summary collapse

INDEX_KEY_SEPARATOR =
'_TO_'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, rates = {}) ⇒ Memory

Initializes a new Money::RatesStore::Memory object.

Parameters:

  • opts (Hash) (defaults to: {})

    Optional store options.

  • rates (Hash) (defaults to: {})

    Optional initial exchange rate data.

Options Hash (opts):

  • :without_mutex (Boolean)

    disables the usage of a mutex



23
24
25
26
27
# File 'lib/money/rates_store/memory.rb', line 23

def initialize(opts = {}, rates = {})
  @rates = rates
  @options = opts
  @guard = Monitor.new
end

Instance Method Details

#add_rate(currency_iso_from, currency_iso_to, rate) ⇒ Numeric

Registers a conversion rate and returns it. Uses Mutex to synchronize data access.

Examples:

store = Money::RatesStore::Memory.new
store.add_rate("USD", "CAD", 1.24515)
store.add_rate("CAD", "USD", 0.803115)

Parameters:

  • currency_iso_from (String)

    Currency to exchange from.

  • currency_iso_to (String)

    Currency to exchange to.

  • rate (Numeric)

    Rate to use when exchanging currencies.

Returns:

  • (Numeric)


41
42
43
44
45
# File 'lib/money/rates_store/memory.rb', line 41

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

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

Iterate over rate tuples (iso_from, iso_to, rate)

Examples:

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

Yield Parameters:

  • iso_from (String)

    Currency ISO string.

  • iso_to (String)

    Currency ISO string.

  • rate (Numeric)

    Exchange rate.

Returns:

  • (Enumerator)


91
92
93
94
95
96
97
98
99
100
# File 'lib/money/rates_store/memory.rb', line 91

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

#get_rate(currency_iso_from, currency_iso_to) ⇒ Numeric

Retrieve the rate for the given currencies. Uses Mutex to synchronize data access. Delegates to Money::RatesStore::Memory

Examples:

store = Money::RatesStore::Memory.new
store.add_rate("USD", "CAD", 1.24515)

store.get_rate("USD", "CAD") #=> 1.24515

Parameters:

  • currency_iso_from (String)

    Currency to exchange from.

  • currency_iso_to (String)

    Currency to exchange to.

Returns:

  • (Numeric)


60
61
62
63
64
# File 'lib/money/rates_store/memory.rb', line 60

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

#marshal_dumpObject



66
67
68
69
70
# File 'lib/money/rates_store/memory.rb', line 66

def marshal_dump
  guard.synchronize do
    return [self.class, options, rates.dup]
  end
end

#transaction(&block) ⇒ Object

Wraps block execution in a thread-safe transaction



73
74
75
76
77
# File 'lib/money/rates_store/memory.rb', line 73

def transaction(&block)
  guard.synchronize do
    yield
  end
end