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

Parse currency correctly if found from existing hash #167

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion lib/monetize/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def to_big_decimal(value)
def parse_currency
computed_currency = nil
computed_currency = input[/[A-Z]{2,3}/]
computed_currency = nil unless Monetize::Parser::CURRENCY_SYMBOLS.value?(computed_currency)
computed_currency ||= compute_currency if assume_from_symbol?


Expand Down Expand Up @@ -132,7 +133,7 @@ def extract_major_minor_with_single_delimiter(num, currency, delimiter)
extract_major_minor_with_tentative_delimiter(num, delimiter)
end
else
if delimiter == currency.decimal_mark
if delimiter == currency.decimal_mark
split_major_minor(num, delimiter)
elsif Monetize.enforce_currency_delimiters && delimiter == currency.thousands_separator
[num.gsub(delimiter, ''), 0]
Expand Down
26 changes: 23 additions & 3 deletions spec/monetize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,28 @@
expect('20.00 GBP'.to_money).to eq Money.new(20_00, 'GBP')
end

it 'raises an error if currency code is invalid' do
expect { '20.00 OMG'.to_money }.to raise_error Monetize::ParseError
context 'with default currency' do
before do
Money.default_currency = Money::Currency.new('USD')
end

it 'parses currency given using default currency' do
expect('20.00 OMG'.to_money).to eq Money.new(20_00, 'USD')
end
end

context 'without default currency' do
before do
Money.default_currency = nil
end

after do
Money.default_currency = Money::Currency.new('USD')
end

it 'raises an error if currency code is invalid' do
expect { '20.00 OMG'.to_money }.to raise_error
end
end
end
end
Expand Down Expand Up @@ -346,7 +366,7 @@
end

describe "expecting whole subunits" do
before(:all) do
before(:all) do
Monetize.expect_whole_subunits = true
Monetize.assume_from_symbol = true
end
Expand Down