Skip to content

Commit 260851b

Browse files
authored
Use instance variable for currency to reduce parameter passing (RubyMoney#198)
1 parent eccd0e1 commit 260851b

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/monetize/parser.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ def initialize(input, fallback_currency = Money.default_currency, options = {})
4343
end
4444

4545
def parse
46-
currency = Money::Currency.wrap(parse_currency)
47-
4846
multiplier_exp, input = extract_multiplier
4947

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

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

56-
major, minor = extract_major_minor(num, currency)
54+
major, minor = extract_major_minor(num)
5755

5856
amount = to_big_decimal([major, minor].join(DEFAULT_DECIMAL_MARK))
5957
amount = apply_multiplier(multiplier_exp, amount)
@@ -72,6 +70,10 @@ def to_big_decimal(value)
7270

7371
attr_reader :input, :fallback_currency, :options
7472

73+
def currency
74+
@currency ||= Money::Currency.wrap(parse_currency)
75+
end
76+
7577
def parse_currency
7678
computed_currency = nil
7779
computed_currency = input[/[A-Z]{2,3}/]
@@ -104,7 +106,7 @@ def compute_currency
104106
CURRENCY_SYMBOLS[match.to_s] if match
105107
end
106108

107-
def extract_major_minor(num, currency)
109+
def extract_major_minor(num)
108110
used_delimiters = num.scan(/[^\d]/).uniq
109111

110112
case used_delimiters.length
@@ -114,20 +116,20 @@ def extract_major_minor(num, currency)
114116
thousands_separator, decimal_mark = used_delimiters
115117
split_major_minor(num.gsub(thousands_separator, ''), decimal_mark)
116118
when 1
117-
extract_major_minor_with_single_delimiter(num, currency, used_delimiters.first)
119+
extract_major_minor_with_single_delimiter(num, used_delimiters.first)
118120
else
119121
fail ParseError, 'Invalid amount'
120122
end
121123
end
122124

123-
def minor_has_correct_dp_for_currency_subunit?(minor, currency)
125+
def minor_has_correct_dp_for_currency_subunit?(minor)
124126
minor.length == currency.subunit_to_unit.to_s.length - 1
125127
end
126128

127-
def extract_major_minor_with_single_delimiter(num, currency, delimiter)
129+
def extract_major_minor_with_single_delimiter(num, delimiter)
128130
if expect_whole_subunits?
129131
_possible_major, possible_minor = split_major_minor(num, delimiter)
130-
if minor_has_correct_dp_for_currency_subunit?(possible_minor, currency)
132+
if minor_has_correct_dp_for_currency_subunit?(possible_minor)
131133
split_major_minor(num, delimiter)
132134
else
133135
extract_major_minor_with_tentative_delimiter(num, delimiter)

0 commit comments

Comments
 (0)