Skip to content

Commit

Permalink
Fixed SystemStackError
Browse files Browse the repository at this point in the history
Fix: calling "indirect_rate" method when rates are not defined throws SystemStackError: stack level too deep.
  • Loading branch information
Sławomir Kluczyk committed Nov 28, 2019
1 parent 33359e6 commit fc5b8d6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/money/bank/russian_central_bank.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def add_rate(from, to, rate)
super(to, from, 1.0 / rate)
end

alias original_get_rate get_rate

def get_rate(from, to)
update_rates if rates_expired?
super || indirect_rate(from, to)
Expand Down Expand Up @@ -58,7 +60,8 @@ def update_expired_at
end

def indirect_rate(from, to)
get_rate('RUB', to) / get_rate('RUB', from)
rate = original_get_rate('RUB', to).to_f / original_get_rate('RUB', from).to_f
rate.nan? || !!rate.infinite? ? nil : rate
end

def local_currencies
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/money/bank/russian_central_bank_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,14 @@
end
end
end

describe '#indirect_rate' do
before do
bank.flush_rates
end

it 'should return nil when indirect rate cannot be calculated' do
expect(bank.send(:indirect_rate, 'RUB', 'RUB')).to be_nil
end
end
end

0 comments on commit fc5b8d6

Please sign in to comment.