Skip to content

Commit 643b2f3

Browse files
authored
Merge pull request #167 from kahshing96/feature/fix-parse-currency
Parse currency correctly if found from existing hash
2 parents 0f1825e + 673ea16 commit 643b2f3

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/monetize/parser.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def to_big_decimal(value)
7676
def parse_currency
7777
computed_currency = nil
7878
computed_currency = input[/[A-Z]{2,3}/]
79+
computed_currency = nil unless Monetize::Parser::CURRENCY_SYMBOLS.value?(computed_currency)
7980
computed_currency ||= compute_currency if assume_from_symbol?
8081

8182

@@ -132,7 +133,7 @@ def extract_major_minor_with_single_delimiter(num, currency, delimiter)
132133
extract_major_minor_with_tentative_delimiter(num, delimiter)
133134
end
134135
else
135-
if delimiter == currency.decimal_mark
136+
if delimiter == currency.decimal_mark
136137
split_major_minor(num, delimiter)
137138
elsif Monetize.enforce_currency_delimiters && delimiter == currency.thousands_separator
138139
[num.gsub(delimiter, ''), 0]

spec/monetize_spec.rb

+23-3
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,28 @@
172172
expect('20.00 GBP'.to_money).to eq Money.new(20_00, 'GBP')
173173
end
174174

175-
it 'raises an error if currency code is invalid' do
176-
expect { '20.00 OMG'.to_money }.to raise_error Monetize::ParseError
175+
context 'with default currency' do
176+
before do
177+
Money.default_currency = Money::Currency.new('USD')
178+
end
179+
180+
it 'parses currency given using default currency' do
181+
expect('20.00 OMG'.to_money).to eq Money.new(20_00, 'USD')
182+
end
183+
end
184+
185+
context 'without default currency' do
186+
before do
187+
Money.default_currency = nil
188+
end
189+
190+
after do
191+
Money.default_currency = Money::Currency.new('USD')
192+
end
193+
194+
it 'raises an error if currency code is invalid' do
195+
expect { '20.00 OMG'.to_money }.to raise_error
196+
end
177197
end
178198
end
179199
end
@@ -346,7 +366,7 @@
346366
end
347367

348368
describe "expecting whole subunits" do
349-
before(:all) do
369+
before(:all) do
350370
Monetize.expect_whole_subunits = true
351371
Monetize.assume_from_symbol = true
352372
end

0 commit comments

Comments
 (0)