Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
79 changes: 44 additions & 35 deletions lib/monetize/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ def parse

negative, num = extract_sign(num)

num.chop! if num =~ /[\.|,]$/

major, minor = extract_major_minor(num)

amount = to_big_decimal([major, minor].join(DEFAULT_DECIMAL_MARK))
amount = to_big_decimal(normalize_number(num))
amount = apply_multiplier(multiplier_exp, amount)
amount = apply_sign(negative, amount)

Expand All @@ -64,6 +60,10 @@ def parse

private

def normalize_number(num)
extract_major_minor(num.sub(/[\.|,]$/, "")).join(DEFAULT_DECIMAL_MARK)
Comment thread
yukideluxe marked this conversation as resolved.
Outdated
end

def to_big_decimal(value)
BigDecimal(value)
rescue ::ArgumentError => err
Expand Down Expand Up @@ -108,64 +108,73 @@ def compute_currency
CURRENCY_SYMBOLS[match.to_s] if match
end

def remove_separator(num, separator)
num.gsub(separator, "")
end

def extract_with_currency_delimiters(num)
Comment thread
yukideluxe marked this conversation as resolved.
Outdated
num = remove_separator(num, currency.thousands_separator)

split_major_minor(num, currency.decimal_mark)
end

def extract_major_minor(num)
if Monetize.enforce_currency_delimiters
Comment thread
sunny marked this conversation as resolved.
Outdated
return extract_with_currency_delimiters(num)
end

used_delimiters = num.scan(/[^\d]/).uniq

case used_delimiters.length
when 0
[num, DEFAULT_MINOR]
when 2
Comment thread
yukideluxe marked this conversation as resolved.
thousands_separator, decimal_mark = used_delimiters
split_major_minor(num.gsub(thousands_separator, ''), decimal_mark)
num = remove_separator(num, thousands_separator)

split_major_minor(num, decimal_mark)
when 1
extract_major_minor_with_single_delimiter(num, used_delimiters.first)
else
fail ParseError, 'Invalid amount'
end
end

def minor_has_correct_dp_for_currency_subunit?(minor)
minor.length == currency.subunit_to_unit.to_s.length - 1
def minor_has_correct_decimal_places_for_currency?(minor)
Comment thread
yukideluxe marked this conversation as resolved.
minor.length == currency.decimal_places
end

def extract_major_minor_with_single_delimiter(num, delimiter)
if expect_whole_subunits?
_possible_major, possible_minor = split_major_minor(num, delimiter)
if minor_has_correct_dp_for_currency_subunit?(possible_minor)
split_major_minor(num, delimiter)
else
extract_major_minor_with_tentative_delimiter(num, delimiter)
end
else
if delimiter == currency.decimal_mark
split_major_minor(num, delimiter)
elsif Monetize.enforce_currency_delimiters && delimiter == currency.thousands_separator
[num.gsub(delimiter, ''), DEFAULT_MINOR]
else
extract_major_minor_with_tentative_delimiter(num, delimiter)
possible_major, possible_minor = split_major_minor(num, delimiter)

if minor_has_correct_decimal_places_for_currency?(possible_minor)
return [possible_major, possible_minor]
Comment thread
yukideluxe marked this conversation as resolved.
end
elsif delimiter == currency.decimal_mark
return split_major_minor(num, delimiter)
end

extract_major_minor_with_tentative_delimiter(num, delimiter)
end

def extract_major_minor_with_tentative_delimiter(num, delimiter)
if num.scan(delimiter).length > 1
# Multiple matches; treat as thousands separator
[num.gsub(delimiter, ''), DEFAULT_MINOR]
else
possible_major, possible_minor = split_major_minor(num, delimiter)
return [remove_separator(num, delimiter), DEFAULT_MINOR]
end

# Doesn't look like thousands separator
is_decimal_mark = possible_minor.length != 3 ||
possible_major.length > 3 ||
possible_major.to_i == 0 ||
(!expect_whole_subunits? && delimiter == '.')
possible_major, possible_minor = split_major_minor(num, delimiter)

if is_decimal_mark
[possible_major, possible_minor]
else
["#{possible_major}#{possible_minor}", DEFAULT_MINOR]
end
end
# Doesn't look like thousands separator
is_decimal_mark = possible_minor.length != 3 ||
possible_major.length > 3 ||
possible_major.to_i == 0 ||
(!expect_whole_subunits? && delimiter == ".")

return [possible_major, possible_minor] if is_decimal_mark

["#{possible_major}#{possible_minor}", DEFAULT_MINOR]
end

def extract_multiplier
Expand Down
1 change: 1 addition & 0 deletions spec/monetize_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(653, 'EUR')

Monetize.enforce_currency_delimiters = true
expect(Monetize.parse('6,534', 'EUR')).to eq Money.new(653, 'EUR')
expect(Monetize.parse('6.534', 'EUR')).to eq Money.new(6_534_00, 'EUR')
Monetize.enforce_currency_delimiters = false
end
Expand Down