Skip to content
Merged
Changes from 1 commit
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
17 changes: 8 additions & 9 deletions lib/monetize/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ def initialize(input, fallback_currency = Money.default_currency, options = {})
@input = input.to_s.strip
@fallback_currency = fallback_currency
@options = options
@currency = Money::Currency.wrap(parse_currency)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making that into a private method with memoisation instead of an attr_reader? E.g.:

def currency
  @currency ||= Money::Currency.wrap(parse_currency)
end

It doesn’t make a big difference but in the rare case where we don’t use that method, the parsing won’t be called.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really have no preference, I also like that approach and we just leave initialize for instantiating the arguments 🙆🏻‍♀️

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes total sense for me, I’ll update the code.

I have a question, I actually have a few similar changes that are chained to this branch. Should I open them as separate PRs even if they depend on this one?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be lovely would be chaining them by making PRs where the base is the previous PR, this way the diff is only focused on one change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

end

def parse
currency = Money::Currency.wrap(parse_currency)

multiplier_exp, input = extract_multiplier

num = input.gsub(/(?:^#{currency.symbol}|[^\d.,'-]+)/, '')
Expand All @@ -53,7 +52,7 @@ def parse

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

major, minor = extract_major_minor(num, currency)
major, minor = extract_major_minor(num)

amount = to_big_decimal([major, minor].join(DEFAULT_DECIMAL_MARK))
amount = apply_multiplier(multiplier_exp, amount)
Expand All @@ -70,7 +69,7 @@ def to_big_decimal(value)
fail ParseError, err.message
end

attr_reader :input, :fallback_currency, :options
attr_reader :input, :fallback_currency, :options, :currency

def parse_currency
computed_currency = nil
Expand Down Expand Up @@ -104,7 +103,7 @@ def compute_currency
CURRENCY_SYMBOLS[match.to_s] if match
end

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

case used_delimiters.length
Expand All @@ -114,20 +113,20 @@ def extract_major_minor(num, currency)
thousands_separator, decimal_mark = used_delimiters
split_major_minor(num.gsub(thousands_separator, ''), decimal_mark)
when 1
extract_major_minor_with_single_delimiter(num, currency, used_delimiters.first)
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, currency)
def minor_has_correct_dp_for_currency_subunit?(minor)
minor.length == currency.subunit_to_unit.to_s.length - 1
end

def extract_major_minor_with_single_delimiter(num, currency, delimiter)
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, currency)
if minor_has_correct_dp_for_currency_subunit?(possible_minor)
split_major_minor(num, delimiter)
else
extract_major_minor_with_tentative_delimiter(num, delimiter)
Expand Down