Skip to content

Commit 3a46b2e

Browse files
committed
Move details validation methos and normalize method to Details class
1 parent 3b03fde commit 3a46b2e

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

Diff for: lib/money-rails/active_model/validator.rb

+34-36
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,36 @@ def abs_raw_value
99
def decimal_pieces
1010
@decimal_pieces ||= abs_raw_value.split(decimal_mark)
1111
end
12+
13+
def has_too_many_decimal_points?
14+
decimal_pieces.length > 2
15+
end
16+
17+
def thousand_separator_after_decimal_mark?
18+
return false unless thousands_separator.present?
19+
20+
decimal_pieces.length == 2 && decimal_pieces[1].include?(thousands_separator)
21+
end
22+
23+
def invalid_thousands_separation?
24+
pieces_array = decimal_pieces[0].split(thousands_separator.presence)
25+
26+
return false if pieces_array.length <= 1
27+
return true if pieces_array[0].length > 3
28+
29+
pieces_array[1..-1].any? do |thousands_group|
30+
thousands_group.length != 3
31+
end
32+
end
33+
34+
# Remove thousands separators, normalize decimal mark,
35+
# remove whitespaces and _ (E.g. 99 999 999 or 12_300_200.20)
36+
def normalize
37+
raw_value.to_s
38+
.gsub(thousands_separator, '')
39+
.gsub(decimal_mark, '.')
40+
.gsub(/[\s_]/, '')
41+
end
1242
end
1343

1444
def validate_each(record, attr, _value)
@@ -35,17 +65,16 @@ def validate_each(record, attr, _value)
3565
# Cache abs_raw_value before normalizing because it's used in
3666
# many places and relies on the original raw_value.
3767
details = generate_details(raw_value, currency)
38-
normalized_raw_value = normalize(details)
68+
normalized_raw_value = details.normalize
3969

4070
super(record, attr, normalized_raw_value)
4171

4272
return unless stringy
4373
return if record_already_has_error?(record, attr, normalized_raw_value)
4474

45-
add_error!(record, attr, details) if
46-
value_has_too_many_decimal_points(details) ||
47-
thousand_separator_after_decimal_mark(details) ||
48-
invalid_thousands_separation(details)
75+
add_error!(record, attr, details) if details.has_too_many_decimal_points? ||
76+
details.thousand_separator_after_decimal_mark? ||
77+
details.invalid_thousands_separation?
4978
end
5079

5180
private
@@ -78,37 +107,6 @@ def add_error!(record, attr, details)
78107
)
79108
end
80109

81-
def value_has_too_many_decimal_points(details)
82-
![1, 2].include?(details.decimal_pieces.length)
83-
end
84-
85-
def thousand_separator_after_decimal_mark(details)
86-
details.thousands_separator.present? &&
87-
details.decimal_pieces.length == 2 &&
88-
details.decimal_pieces[1].include?(details.thousands_separator)
89-
end
90-
91-
def invalid_thousands_separation(details)
92-
pieces_array = details.decimal_pieces[0].split(details.thousands_separator.presence)
93-
94-
return false if pieces_array.length <= 1
95-
return true if pieces_array[0].length > 3
96-
97-
pieces_array[1..-1].any? do |thousands_group|
98-
thousands_group.length != 3
99-
end
100-
end
101-
102-
# Remove thousands separators, normalize decimal mark,
103-
# remove whitespaces and _ (E.g. 99 999 999 or 12_300_200.20)
104-
def normalize(details)
105-
details.raw_value
106-
.to_s
107-
.gsub(details.thousands_separator, '')
108-
.gsub(details.decimal_mark, '.')
109-
.gsub(/[\s_]/, '')
110-
end
111-
112110
def lookup(key, currency)
113111
if locale_backend
114112
locale_backend.lookup(key, currency) || DEFAULTS[key]

0 commit comments

Comments
 (0)