@@ -9,6 +9,36 @@ def abs_raw_value
9
9
def decimal_pieces
10
10
@decimal_pieces ||= abs_raw_value . split ( decimal_mark )
11
11
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
12
42
end
13
43
14
44
def validate_each ( record , attr , _value )
@@ -35,17 +65,16 @@ def validate_each(record, attr, _value)
35
65
# Cache abs_raw_value before normalizing because it's used in
36
66
# many places and relies on the original raw_value.
37
67
details = generate_details ( raw_value , currency )
38
- normalized_raw_value = normalize ( details )
68
+ normalized_raw_value = details . normalize
39
69
40
70
super ( record , attr , normalized_raw_value )
41
71
42
72
return unless stringy
43
73
return if record_already_has_error? ( record , attr , normalized_raw_value )
44
74
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?
49
78
end
50
79
51
80
private
@@ -78,37 +107,6 @@ def add_error!(record, attr, details)
78
107
)
79
108
end
80
109
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
-
112
110
def lookup ( key , currency )
113
111
if locale_backend
114
112
locale_backend . lookup ( key , currency ) || DEFAULTS [ key ]
0 commit comments