Skip to content

Commit

Permalink
Merge pull request #167 from kahshing96/feature/fix-parse-currency
Browse files Browse the repository at this point in the history
Parse currency correctly if found from existing hash
  • Loading branch information
semmons99 committed Jan 18, 2024
2 parents 0f1825e + 673ea16 commit 643b2f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
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

0 comments on commit 643b2f3

Please sign in to comment.