Class: Money::RatesStore::Memory
- Inherits:
-
Object
- Object
- Money::RatesStore::Memory
- Defined in:
- lib/money/rates_store/memory.rb,
sig/lib/money/rates_store/memory.rbs
Overview
Class for thread-safe storage of exchange rate pairs.
Used by instances of Money::Bank::VariableExchange.
Constant Summary collapse
- INDEX_KEY_SEPARATOR =
"_TO_"
Instance Attribute Summary collapse
-
#guard ⇒ Object
readonly
Returns the value of attribute guard.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Instance Method Summary collapse
-
#add_rate(currency_iso_from, currency_iso_to, rate) ⇒ Numeric
Registers a conversion rate and returns it.
-
#each_rate {|iso_from, iso_to, rate| ... } ⇒ Enumerator
Iterate over rate tuples (iso_from, iso_to, rate).
-
#get_rate(currency_iso_from, currency_iso_to) ⇒ Numeric
Retrieve the rate for the given currencies.
-
#initialize(opts = {}, rates = {}) ⇒ Memory
constructor
Initializes a new
Money::RatesStore::Memoryobject. - #marshal_dump ⇒ Object
-
#rate_key_for(currency_iso_from, currency_iso_to) ⇒ String
Return the rate hashkey for the given currencies.
-
#transaction { ... } ⇒ Object
Wraps block execution in a thread-safe transaction.
Constructor Details
#initialize(opts = {}, rates = {}) ⇒ Memory
Initializes a new Money::RatesStore::Memory object.
24 25 26 27 28 |
# File 'lib/money/rates_store/memory.rb', line 24 def initialize(opts = {}, rates = {}) @rates = rates @options = opts @guard = Monitor.new end |
Instance Attribute Details
#guard ⇒ Object (readonly)
Returns the value of attribute guard.
103 104 105 |
# File 'lib/money/rates_store/memory.rb', line 103 def guard @guard end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
103 104 105 |
# File 'lib/money/rates_store/memory.rb', line 103 def @options end |
#rates ⇒ Object (readonly)
Returns the value of attribute rates.
103 104 105 |
# File 'lib/money/rates_store/memory.rb', line 103 def rates @rates 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.
42 43 44 45 46 |
# File 'lib/money/rates_store/memory.rb', line 42 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)
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/money/rates_store/memory.rb', line 90 def each_rate(&) 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
61 62 63 64 65 |
# File 'lib/money/rates_store/memory.rb', line 61 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_dump ⇒ Object
67 68 69 70 71 |
# File 'lib/money/rates_store/memory.rb', line 67 def marshal_dump guard.synchronize do return [self.class, , rates.dup] end end |
#rate_key_for(currency_iso_from, currency_iso_to) ⇒ String
Return the rate hashkey for the given currencies.
114 115 116 |
# File 'lib/money/rates_store/memory.rb', line 114 def rate_key_for(currency_iso_from, currency_iso_to) [currency_iso_from, currency_iso_to].join(INDEX_KEY_SEPARATOR).upcase end |
#transaction { ... } ⇒ Object
Wraps block execution in a thread-safe transaction
74 75 76 |
# File 'lib/money/rates_store/memory.rb', line 74 def transaction(&) guard.synchronize(&) end |