From fc5b8d6666b252eb8d438be813faa60f56ab32ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Kluczyk?= Date: Thu, 28 Nov 2019 18:08:29 +0100 Subject: [PATCH] Fixed SystemStackError Fix: calling "indirect_rate" method when rates are not defined throws SystemStackError: stack level too deep. --- lib/money/bank/russian_central_bank.rb | 5 ++++- spec/lib/money/bank/russian_central_bank_spec.rb | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/money/bank/russian_central_bank.rb b/lib/money/bank/russian_central_bank.rb index bac2aa6..630e687 100644 --- a/lib/money/bank/russian_central_bank.rb +++ b/lib/money/bank/russian_central_bank.rb @@ -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) @@ -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 diff --git a/spec/lib/money/bank/russian_central_bank_spec.rb b/spec/lib/money/bank/russian_central_bank_spec.rb index 6259e17..bdb139d 100644 --- a/spec/lib/money/bank/russian_central_bank_spec.rb +++ b/spec/lib/money/bank/russian_central_bank_spec.rb @@ -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