Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed SystemStackError #18

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 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,11 @@ def update_expired_at
end

def indirect_rate(from, to)
get_rate('RUB', to) / get_rate('RUB', from)
dividend = original_get_rate('RUB', to)
divider = original_get_rate('RUB', from)
return nil if dividend.nil? || divider.nil?

dividend.to_f / divider.to_f

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olhor Can we trim it down to this ?

def indirect_rate(from, to)
  get_rate('RUB', to) / get_rate('RUB', from)
  dividend = original_get_rate('RUB', to)
  divider = original_get_rate('RUB', from)
  
  dividend && divider && dividend.to_f / divider.to_f
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line get_rate('RUB', to) / get_rate('RUB', from) will throw an exception if get_rate returns nil.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@olhor I am sorry. I missed to remove that line -

def indirect_rate(from, to)
  dividend = original_get_rate('RUB', to)
  divider = original_get_rate('RUB', from)
  
  dividend && divider && dividend.to_f / divider.to_f
end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem for me.

end

def local_currencies
Expand Down
13 changes: 13 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,17 @@
end
end
end

describe '#indirect_rate' do
context 'without rates' 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
expect(bank.send(:indirect_rate, 'USD', 'AED')).to be_nil
end
end
end
end