normalizy 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +12 -0
  3. data/README.md +25 -55
  4. data/lib/normalizy/config.rb +3 -7
  5. data/lib/normalizy/extensions.rb +45 -49
  6. data/lib/normalizy/rspec/matcher.rb +14 -2
  7. data/lib/normalizy/version.rb +1 -1
  8. data/lib/normalizy.rb +1 -1
  9. data/spec/normalizy/config/alias_spec.rb +14 -42
  10. data/spec/normalizy/extensions/filters/date_spec.rb +4 -14
  11. data/spec/normalizy/extensions/filters/money_spec.rb +9 -19
  12. data/spec/normalizy/extensions/filters/number_spec.rb +2 -6
  13. data/spec/normalizy/extensions/filters/percent_spec.rb +9 -19
  14. data/spec/normalizy/extensions/filters/strip_spec.rb +8 -8
  15. data/spec/normalizy/extensions/model_spec.rb +107 -0
  16. data/spec/normalizy/extensions/normalizy_rules_spec.rb +132 -180
  17. data/spec/normalizy/filters/date_spec.rb +9 -9
  18. data/spec/normalizy/filters/money_spec.rb +42 -37
  19. data/spec/normalizy/rspec/matcher/description_spec.rb +8 -7
  20. data/spec/normalizy/rspec/matcher/failure_message_spec.rb +25 -47
  21. data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +13 -15
  22. data/spec/normalizy/rspec/matcher/from_spec.rb +4 -3
  23. data/spec/normalizy/rspec/matcher/matches_spec.rb +117 -0
  24. data/spec/normalizy/rspec/matcher/to_spec.rb +4 -3
  25. data/spec/support/db/schema.rb +67 -9
  26. data/spec/support/filters/{blacklist_1.rb → blacklist.rb} +1 -1
  27. data/spec/support/filters/{blacklist_block.rb → block.rb} +1 -1
  28. data/spec/support/filters/info.rb +11 -0
  29. data/spec/support/models/alias.rb +7 -0
  30. data/spec/support/models/match.rb +9 -0
  31. data/spec/support/models/model.rb +28 -0
  32. data/spec/support/models/model_date.rb +7 -0
  33. data/spec/support/models/model_money.rb +10 -0
  34. data/spec/support/models/model_number.rb +5 -0
  35. data/spec/support/models/model_percent.rb +10 -0
  36. data/spec/support/models/model_strip.rb +8 -0
  37. data/spec/support/models/{clean.rb → rule.rb} +1 -1
  38. metadata +30 -20
  39. data/spec/normalizy/config/normalizy_raws_spec.rb +0 -9
  40. data/spec/normalizy/extensions/apply_normalizy_spec.rb +0 -171
  41. data/spec/normalizy/rspec/matcher/matchers_spec.rb +0 -97
  42. data/spec/normalizy/rspec/normalizy_spec.rb +0 -8
  43. data/spec/support/filters/blacklist_2.rb +0 -13
  44. data/spec/support/models/user.rb +0 -9
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe '#apply_normalizy' do
6
+ context 'when object has no normalizy' do
7
+ specify do
8
+ expect(Model.create(none: ' Botelho ').none).to eq ' Botelho '
9
+ end
10
+ end
11
+
12
+ context 'when object has normalizy' do
13
+ specify do
14
+ Normalizy.configure do |config|
15
+ config.default_filters = [:squish]
16
+ end
17
+
18
+ expect(Model.create(default: ' Botelho ').default).to eq 'Botelho'
19
+ end
20
+
21
+ specify do
22
+ expect(Model.create(block: 'Botelho').block).to eq 'BOTELHO'
23
+ end
24
+ end
25
+
26
+ context 'when a filter is given' do
27
+ specify do
28
+ expect(Model.create(symbol: ' Bote lho ').symbol).to eq 'Bote lho'
29
+ end
30
+
31
+ specify do
32
+ expect(Model.create(array_symbol: ' Bote lho ').array_symbol).to eq 'Bote lho'
33
+ end
34
+
35
+ specify do
36
+ expect(Model.create(array_symbols: ' Bote lho ').array_symbols).to eq 'bote lho'
37
+ end
38
+
39
+ specify do
40
+ expect(Model.create(hash_no_args: ' Bote lho ').hash_no_args).to eq 'Bote lho'
41
+ end
42
+
43
+ specify do
44
+ expect(Model.create(hash_with_args: ' Botelho ').hash_with_args).to eq 'Botelho '
45
+ end
46
+
47
+ specify do
48
+ expect(Model.create(module_one_arg: 'Fuck').module_one_arg).to eq 'filtered'
49
+ end
50
+
51
+ specify do
52
+ expect(Model.create(module_two_args: 'Botelho').module_two_args).to eq 'module_two_args, Botelho, Model'
53
+ end
54
+
55
+ specify do
56
+ Normalizy.configure do |config|
57
+ config.add :blacklist, Normalizy::Filters::Block
58
+ end
59
+
60
+ expect(Model.create(module_and_block: 'Botelho').module_and_block).to eq 'BOTELHO'
61
+ end
62
+
63
+ specify do
64
+ expect(Model.create(method_with_no_options_field: 'Botelho').method_with_no_options_field).to eq 'Botelho'
65
+ end
66
+
67
+ specify do
68
+ expect(Model.create(method_with_options_field: 'Botelho').method_with_options_field).to eq [
69
+ 'Botelho',
70
+ {
71
+ key: :value,
72
+ attribute: :method_with_options_field,
73
+ object: Model.new
74
+ }
75
+ ].join ', '
76
+ end
77
+
78
+ specify do
79
+ expect(Model.create(native: 'Botelho').native).to eq '["Botelho"]'
80
+ end
81
+
82
+ specify do
83
+ expect(Model.create(multiple: 'BoteLho').multiple).to eq 'bote lho'
84
+ end
85
+ end
86
+
87
+ context 'when assign is made via set' do
88
+ context 'with no save' do
89
+ specify do
90
+ object = Model.new
91
+ object.symbol = ' Bote lho '
92
+
93
+ expect(object.symbol).to eq 'Bote lho'
94
+ end
95
+ end
96
+
97
+ context 'with save' do
98
+ specify do
99
+ object = Model.new
100
+ object.symbol = ' Bote lho '
101
+ object.save
102
+
103
+ expect(object.symbol).to eq 'Bote lho'
104
+ end
105
+ end
106
+ end
107
+ end
@@ -3,7 +3,7 @@
3
3
  require 'rails_helper'
4
4
 
5
5
  RSpec.describe Normalizy::Extension, ':normalizy_rules' do
6
- let!(:model) { User }
6
+ let!(:model) { Rule }
7
7
 
8
8
  before { model.normalizy_rules = {} }
9
9
 
@@ -14,111 +14,87 @@ RSpec.describe Normalizy::Extension, ':normalizy_rules' do
14
14
  end
15
15
  end
16
16
 
17
- context 'with one field' do
18
- context 'with no rules' do
19
- before { model.normalizy :name }
17
+ specify do
18
+ model.normalizy :name
20
19
 
21
- it 'appends default' do
22
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :squish }])
23
- end
24
- end
20
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
21
+ end
25
22
 
26
- context 'with one rule' do
27
- before { model.normalizy :name, with: :upcase }
23
+ specify do
24
+ model.normalizy :name, with: :upcase
28
25
 
29
- specify do
30
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
31
- end
32
- end
26
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
27
+ end
33
28
 
34
- context 'with multiple rules' do
35
- before { model.normalizy :name, with: [:upcase, 'blank', { trim: { side: :left } }] }
29
+ specify do
30
+ model.normalizy :name, with: [:upcase, 'blank', { trim: { side: :left } }]
36
31
 
37
- specify do
38
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: [:upcase, 'blank', { trim: { side: :left } }] }])
39
- end
40
- end
32
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: [:upcase, 'blank', { trim: { side: :left } }] }])
33
+ end
41
34
 
42
- context 'with multiple normalizes including repeated rules' do
43
- before do
44
- model.normalizy :name, with: [:upcase, { trim: { side: :left } }]
45
- model.normalizy :name, with: :squish
46
- model.normalizy :name, with: [:upcase, { trim: { side: :right } }]
47
- model.normalizy :name, with: :squish
48
- end
49
-
50
- it 'includes all' do
51
- expect(model.normalizy_rules).to eq(
52
- name: [
53
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
54
- { block: nil, options: {}, rules: :squish },
55
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
56
- { block: nil, options: {}, rules: :squish }
57
- ]
58
- )
59
- end
60
- end
35
+ specify do
36
+ model.normalizy :name, with: [:upcase, { trim: { side: :left } }]
37
+ model.normalizy :name, with: :squish
38
+ model.normalizy :name, with: [:upcase, { trim: { side: :right } }]
39
+ model.normalizy :name, with: :squish
40
+
41
+ expect(model.normalizy_rules).to eq(
42
+ name: [
43
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
44
+ { block: nil, options: {}, rules: :squish },
45
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
46
+ { block: nil, options: {}, rules: :squish }
47
+ ]
48
+ )
61
49
  end
62
50
 
63
- context 'with multiple fields' do
64
- context 'with no rule' do
65
- before { model.normalizy :email, :name }
51
+ specify do
52
+ model.normalizy :email, :name
66
53
 
67
- it 'appends default' do
68
- expect(model.normalizy_rules).to eq(
69
- email: [{ block: nil, options: {}, rules: :squish }],
70
- name: [{ block: nil, options: {}, rules: :squish }]
71
- )
72
- end
73
- end
54
+ expect(model.normalizy_rules).to eq(
55
+ email: [{ block: nil, options: {}, rules: nil }],
56
+ name: [{ block: nil, options: {}, rules: nil }]
57
+ )
58
+ end
74
59
 
75
- context 'with one rule' do
76
- before { model.normalizy :email, :name, with: :upcase }
60
+ specify do
61
+ model.normalizy :email, :name, with: :upcase
77
62
 
78
- specify do
79
- expect(model.normalizy_rules).to eq(
80
- name: [{ block: nil, options: {}, rules: :upcase }],
81
- email: [{ block: nil, options: {}, rules: :upcase }]
82
- )
83
- end
84
- end
63
+ expect(model.normalizy_rules).to eq(
64
+ name: [{ block: nil, options: {}, rules: :upcase }],
65
+ email: [{ block: nil, options: {}, rules: :upcase }]
66
+ )
67
+ end
85
68
 
86
- context 'with multiple rules' do
87
- before { model.normalizy :email, :name, with: [:upcase, :blank, { trim: { side: :left } }] }
69
+ specify do
70
+ model.normalizy :email, :name, with: [:upcase, :blank, { trim: { side: :left } }]
88
71
 
89
- specify do
90
- expect(model.normalizy_rules).to eq(
91
- email: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }],
92
- name: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }]
93
- )
94
- end
95
- end
72
+ expect(model.normalizy_rules).to eq(
73
+ email: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }],
74
+ name: [{ block: nil, options: {}, rules: [:upcase, :blank, { trim: { side: :left } }] }]
75
+ )
76
+ end
96
77
 
97
- context 'with multiple normalizes including repeated rules' do
98
- before do
99
- model.normalizy :email, :name, with: [:upcase, { trim: { side: :left } }]
100
- model.normalizy :email, :name, with: :squish
101
- model.normalizy :email, :name, with: [:upcase, { trim: { side: :right } }]
102
- model.normalizy :email, :name, with: :squish
103
- end
104
-
105
- it 'merges all as unique with the last one having priority in deep levels too' do
106
- expect(model.normalizy_rules).to eq(
107
- email: [
108
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
109
- { block: nil, options: {}, rules: :squish },
110
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
111
- { block: nil, options: {}, rules: :squish }
112
- ],
113
- name: [
114
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
115
- { block: nil, options: {}, rules: :squish },
116
- { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
117
- { block: nil, options: {}, rules: :squish }
118
- ]
119
- )
120
- end
121
- end
78
+ specify do
79
+ model.normalizy :email, :name, with: [:upcase, { trim: { side: :left } }]
80
+ model.normalizy :email, :name, with: :squish
81
+ model.normalizy :email, :name, with: [:upcase, { trim: { side: :right } }]
82
+ model.normalizy :email, :name, with: :squish
83
+
84
+ expect(model.normalizy_rules).to eq(
85
+ email: [
86
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
87
+ { block: nil, options: {}, rules: :squish },
88
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
89
+ { block: nil, options: {}, rules: :squish }
90
+ ],
91
+ name: [
92
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :left } }] },
93
+ { block: nil, options: {}, rules: :squish },
94
+ { block: nil, options: {}, rules: [:upcase, { trim: { side: :right } }] },
95
+ { block: nil, options: {}, rules: :squish }
96
+ ]
97
+ )
122
98
  end
123
99
  end
124
100
 
@@ -129,115 +105,91 @@ RSpec.describe Normalizy::Extension, ':normalizy_rules' do
129
105
  end
130
106
  end
131
107
 
132
- context 'with one field' do
133
- context 'with no rules' do
134
- before { model.normalizy :name }
108
+ specify do
109
+ model.normalizy :name
135
110
 
136
- specify do
137
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: [] }])
138
- end
139
- end
111
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: nil }])
112
+ end
140
113
 
141
- context 'with one rule' do
142
- before { model.normalizy :name, with: :upcase }
114
+ specify do
115
+ model.normalizy :name, with: :upcase
143
116
 
144
- specify do
145
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
146
- end
147
- end
117
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: :upcase }])
118
+ end
148
119
 
149
- context 'with multiple rules' do
150
- before { model.normalizy :name, with: %i[upcase blank] }
120
+ specify do
121
+ model.normalizy :name, with: %i[upcase blank]
151
122
 
152
- specify do
153
- expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: %i[upcase blank] }])
154
- end
155
- end
123
+ expect(model.normalizy_rules).to eq(name: [{ block: nil, options: {}, rules: %i[upcase blank] }])
124
+ end
156
125
 
157
- context 'with multiple normalizes including repeated rules' do
158
- before do
159
- model.normalizy :name, with: :upcase
160
- model.normalizy :name, with: :squish
161
- model.normalizy :name, with: :upcase
162
- end
163
-
164
- it 'merges all as unique' do
165
- expect(model.normalizy_rules).to eq(
166
- name: [
167
- { block: nil, options: {}, rules: :upcase },
168
- { block: nil, options: {}, rules: :squish },
169
- { block: nil, options: {}, rules: :upcase }
170
- ]
171
- )
172
- end
173
- end
126
+ specify do
127
+ model.normalizy :name, with: :upcase
128
+ model.normalizy :name, with: :squish
129
+ model.normalizy :name, with: :upcase
130
+
131
+ expect(model.normalizy_rules).to eq(
132
+ name: [
133
+ { block: nil, options: {}, rules: :upcase },
134
+ { block: nil, options: {}, rules: :squish },
135
+ { block: nil, options: {}, rules: :upcase }
136
+ ]
137
+ )
174
138
  end
175
139
 
176
- context 'with multiple fields' do
177
- context 'with no rule' do
178
- before { model.normalizy :email, :name }
140
+ specify do
141
+ model.normalizy :email, :name
179
142
 
180
- specify do
181
- expect(model.normalizy_rules).to eq(
182
- email: [{ block: nil, options: {}, rules: [] }],
183
- name: [{ block: nil, options: {}, rules: [] }]
184
- )
185
- end
186
- end
143
+ expect(model.normalizy_rules).to eq(
144
+ email: [{ block: nil, options: {}, rules: nil }],
145
+ name: [{ block: nil, options: {}, rules: nil }]
146
+ )
147
+ end
187
148
 
188
- context 'with one rule' do
189
- before { model.normalizy :email, :name, with: :upcase }
149
+ specify do
150
+ model.normalizy :email, :name, with: :upcase
190
151
 
191
- specify do
192
- expect(model.normalizy_rules).to eq(
193
- email: [{ block: nil, options: {}, rules: :upcase }],
194
- name: [{ block: nil, options: {}, rules: :upcase }]
195
- )
196
- end
197
- end
152
+ expect(model.normalizy_rules).to eq(
153
+ email: [{ block: nil, options: {}, rules: :upcase }],
154
+ name: [{ block: nil, options: {}, rules: :upcase }]
155
+ )
156
+ end
198
157
 
199
- context 'with multiple rules' do
200
- before { model.normalizy :email, :name, with: %i[upcase blank] }
158
+ specify do
159
+ model.normalizy :email, :name, with: %i[upcase blank]
201
160
 
202
- specify do
203
- expect(model.normalizy_rules).to eq(
204
- email: [{ block: nil, options: {}, rules: %i[upcase blank] }],
205
- name: [{ block: nil, options: {}, rules: %i[upcase blank] }]
206
- )
207
- end
208
- end
161
+ expect(model.normalizy_rules).to eq(
162
+ email: [{ block: nil, options: {}, rules: %i[upcase blank] }],
163
+ name: [{ block: nil, options: {}, rules: %i[upcase blank] }]
164
+ )
165
+ end
209
166
 
210
- context 'with multiple normalizes including repeated rules' do
211
- before do
212
- model.normalizy :email, :name, with: :upcase
213
- model.normalizy :email, :name, with: :squish
214
- model.normalizy :email, :name, with: :upcase
215
- end
216
-
217
- it 'merges all as unique' do
218
- expect(model.normalizy_rules).to eq(
219
- email: [
220
- { block: nil, options: {}, rules: :upcase },
221
- { block: nil, options: {}, rules: :squish },
222
- { block: nil, options: {}, rules: :upcase }
223
- ],
224
- name: [
225
- { block: nil, options: {}, rules: :upcase },
226
- { block: nil, options: {}, rules: :squish },
227
- { block: nil, options: {}, rules: :upcase }
228
- ]
229
- )
230
- end
231
- end
167
+ specify do
168
+ model.normalizy :email, :name, with: :upcase
169
+ model.normalizy :email, :name, with: :squish
170
+ model.normalizy :email, :name, with: :upcase
171
+
172
+ expect(model.normalizy_rules).to eq(
173
+ email: [
174
+ { block: nil, options: {}, rules: :upcase },
175
+ { block: nil, options: {}, rules: :squish },
176
+ { block: nil, options: {}, rules: :upcase }
177
+ ],
178
+ name: [
179
+ { block: nil, options: {}, rules: :upcase },
180
+ { block: nil, options: {}, rules: :squish },
181
+ { block: nil, options: {}, rules: :upcase }
182
+ ]
183
+ )
232
184
  end
233
185
  end
234
186
 
235
187
  context 'when block is given' do
236
188
  let!(:block) { ->(value) { value.downcase } }
237
189
 
238
- before { model.normalizy(:name, &block) }
239
-
240
190
  specify do
191
+ model.normalizy :name, &block
192
+
241
193
  expect(model.normalizy_rules[:name][0][:block]).to eq block
242
194
  end
243
195
  end
@@ -21,15 +21,15 @@ RSpec.describe Normalizy::Filters::Date do
21
21
  end
22
22
 
23
23
  context 'with invalid date' do
24
- let!(:object) { User.new }
25
- let!(:options) { { attribute: :birthday, object: object } }
24
+ let!(:object) { ModelDate.new }
25
+ let!(:options) { { attribute: :date, object: object } }
26
26
 
27
27
  context 'with i18n present' do
28
28
  before do
29
- allow(I18n).to receive(:t).with(:birthday,
30
- scope: ['normalizy.errors.date', 'user'],
29
+ allow(I18n).to receive(:t).with(:date,
30
+ scope: ['normalizy.errors.date', 'model_date'],
31
31
  value: '1984-10-00',
32
- default: '%{value} is an invalid date.') { 'birthday.error' }
32
+ default: '%{value} is an invalid date.') { 'date.error' }
33
33
  end
34
34
 
35
35
  it 'writes an error on object and does not set the values' do
@@ -37,8 +37,8 @@ RSpec.describe Normalizy::Filters::Date do
37
37
 
38
38
  subject.call '1984-10-00', options
39
39
 
40
- expect(object.errors[:birthday]).to eq ['birthday.error']
41
- expect(object.birthday).to eq nil
40
+ expect(object.errors[:date]).to eq ['date.error']
41
+ expect(object.date).to eq nil
42
42
  end
43
43
  end
44
44
 
@@ -48,8 +48,8 @@ RSpec.describe Normalizy::Filters::Date do
48
48
 
49
49
  subject.call '1984-10-00', options
50
50
 
51
- expect(object.errors[:birthday]).to eq ['1984-10-00 is an invalid date.']
52
- expect(object.birthday).to eq nil
51
+ expect(object.errors[:date]).to eq ['1984-10-00 is an invalid date.']
52
+ expect(object.date).to eq nil
53
53
  end
54
54
  end
55
55
  end