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

Fix bug introduced by PR#167 not covering all available currencies #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
29 changes: 19 additions & 10 deletions lib/monetize/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,33 @@ def to_big_decimal(value)
attr_reader :input, :fallback_currency, :options

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?

computed_currency = compute_currency
computed_currency ||= compute_currency_assumed_from_symbol if assume_from_symbol?

computed_currency || fallback_currency || Money.default_currency
end

# input[/[A-Z]{2,3}/] can match certain currency symbols with no "special" and uncapitalized characters e.g.
# RM, not just normal ISO codes. Thus, we make sure that if parsed_currency matches
# the mentioned currency symbols, then we return the respective ISO currency code for them.
def compute_currency
parsed_currency = input[/[A-Z]{2,3}/]

return if !parsed_currency
return CURRENCY_SYMBOLS[parsed_currency] if parsed_currency == input.match(currency_symbol_regex).to_s

Money::Currency.all.map(&:iso_code).include?(parsed_currency) ? parsed_currency : nil
end

def assume_from_symbol?
options.fetch(:assume_from_symbol) { Monetize.assume_from_symbol }
end

def compute_currency_assumed_from_symbol
match = input.match(currency_symbol_regex)
CURRENCY_SYMBOLS[match.to_s] if match
end

def expect_whole_subunits?
options.fetch(:expect_whole_subunits) { Monetize.expect_whole_subunits }
end
Expand All @@ -99,11 +113,6 @@ def apply_sign(negative, amount)
negative ? amount * -1 : amount
end

def compute_currency
match = input.match(currency_symbol_regex)
CURRENCY_SYMBOLS[match.to_s] if match
end

def extract_major_minor(num, currency)
used_delimiters = num.scan(/[^\d]/).uniq

Expand Down
6 changes: 5 additions & 1 deletion spec/monetize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@
end

it 'parses formatted inputs without currency detection when overridden' do
expect(Monetize.parse("#{symbol}5.95", nil, assume_from_symbol: false)).to eq Money.new(amount, 'USD')
currency_symbols_with_no_special_and_uncapitalized_chars = ["RM"]

expect(Monetize.parse("#{symbol}5.95", nil, assume_from_symbol: false)).to eq(
Money.new(amount, currency_symbols_with_no_special_and_uncapitalized_chars.include?(symbol) ? iso_code : "USD")
)
end
end
end
Expand Down