Skip to content

Commit

Permalink
Fix RatesStore::Memory regression (RubyMoney#901)
Browse files Browse the repository at this point in the history
  • Loading branch information
antstorm authored Dec 4, 2019
1 parent 5fe52ec commit fc97b9f
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/money/rates_store/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def initialize(opts = {}, rates = {})
# store.add_rate("USD", "CAD", 1.24515)
# store.add_rate("CAD", "USD", 0.803115)
def add_rate(currency_iso_from, currency_iso_to, rate)
@guard.synchronize do
@rates[rate_key_for(currency_iso_from, currency_iso_to)] = rate
guard.synchronize do
rates[rate_key_for(currency_iso_from, currency_iso_to)] = rate
end
end

Expand All @@ -58,20 +58,20 @@ def add_rate(currency_iso_from, currency_iso_to, rate)
#
# store.get_rate("USD", "CAD") #=> 1.24515
def get_rate(currency_iso_from, currency_iso_to)
@guard.synchronize do
@rates[rate_key_for(currency_iso_from, currency_iso_to)]
guard.synchronize do
rates[rate_key_for(currency_iso_from, currency_iso_to)]
end
end

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

# Wraps block execution in a thread-safe transaction
def transaction(&block)
@guard.synchronize do
guard.synchronize do
yield
end
end
Expand All @@ -91,8 +91,8 @@ def transaction(&block)
def each_rate(&block)
return to_enum(:each_rate) unless block_given?

@guard.synchronize do
@rates.each do |key, rate|
guard.synchronize do
rates.each do |key, rate|
iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR)
yield iso_from, iso_to, rate
end
Expand All @@ -101,6 +101,8 @@ def each_rate(&block)

private

attr_reader :rates, :options, :guard

# Return the rate hashkey for the given currencies.
#
# @param [String] currency_iso_from The currency to exchange from.
Expand Down

0 comments on commit fc97b9f

Please sign in to comment.