normalizy 0.2.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ModelPercent < ActiveRecord::Base
4
+ normalizy :text , with: :percent
5
+ normalizy :cents_type , with: { percent: { type: :cents } }
6
+ normalizy :cast_to_i , with: { percent: { cast: :to_i } }
7
+ normalizy :cast_to_d , with: { percent: { cast: :to_d } }
8
+ normalizy :cents_type_and_cast_to_f, with: { percent: { cast: :to_f, type: :cents } }
9
+ normalizy :cents_type_and_cast_to_i, with: { percent: { cast: :to_i, type: :cents } }
10
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ModelStrip < ActiveRecord::Base
4
+ normalizy :strip , with: :strip
5
+ normalizy :strip_side_both , with: { strip: { side: :both } }
6
+ normalizy :strip_side_left , with: { strip: { side: :left } }
7
+ normalizy :strip_side_right, with: { strip: { side: :right } }
8
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Clean < ActiveRecord::Base
3
+ class Rule < ActiveRecord::Base
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normalizy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Washington Botelho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-20 00:00:00.000000000 Z
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -141,13 +141,12 @@ files:
141
141
  - spec/normalizy/config/default_filters_spec.rb
142
142
  - spec/normalizy/config/initialize_spec.rb
143
143
  - spec/normalizy/config/normalizy_aliases_spec.rb
144
- - spec/normalizy/config/normalizy_raws_spec.rb
145
- - spec/normalizy/extensions/apply_normalizy_spec.rb
146
144
  - spec/normalizy/extensions/filters/date_spec.rb
147
145
  - spec/normalizy/extensions/filters/money_spec.rb
148
146
  - spec/normalizy/extensions/filters/number_spec.rb
149
147
  - spec/normalizy/extensions/filters/percent_spec.rb
150
148
  - spec/normalizy/extensions/filters/strip_spec.rb
149
+ - spec/normalizy/extensions/model_spec.rb
151
150
  - spec/normalizy/extensions/normalizy_rules_spec.rb
152
151
  - spec/normalizy/filters/date_spec.rb
153
152
  - spec/normalizy/filters/money_spec.rb
@@ -159,17 +158,23 @@ files:
159
158
  - spec/normalizy/rspec/matcher/failure_message_spec.rb
160
159
  - spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb
161
160
  - spec/normalizy/rspec/matcher/from_spec.rb
162
- - spec/normalizy/rspec/matcher/matchers_spec.rb
161
+ - spec/normalizy/rspec/matcher/matches_spec.rb
163
162
  - spec/normalizy/rspec/matcher/to_spec.rb
164
- - spec/normalizy/rspec/normalizy_spec.rb
165
163
  - spec/rails_helper.rb
166
164
  - spec/support/common.rb
167
165
  - spec/support/db/schema.rb
168
- - spec/support/filters/blacklist_1.rb
169
- - spec/support/filters/blacklist_2.rb
170
- - spec/support/filters/blacklist_block.rb
171
- - spec/support/models/clean.rb
172
- - spec/support/models/user.rb
166
+ - spec/support/filters/blacklist.rb
167
+ - spec/support/filters/block.rb
168
+ - spec/support/filters/info.rb
169
+ - spec/support/models/alias.rb
170
+ - spec/support/models/match.rb
171
+ - spec/support/models/model.rb
172
+ - spec/support/models/model_date.rb
173
+ - spec/support/models/model_money.rb
174
+ - spec/support/models/model_number.rb
175
+ - spec/support/models/model_percent.rb
176
+ - spec/support/models/model_strip.rb
177
+ - spec/support/models/rule.rb
173
178
  homepage: https://github.com/wbotelhos/normalizy
174
179
  licenses:
175
180
  - MIT
@@ -200,13 +205,12 @@ test_files:
200
205
  - spec/normalizy/config/default_filters_spec.rb
201
206
  - spec/normalizy/config/initialize_spec.rb
202
207
  - spec/normalizy/config/normalizy_aliases_spec.rb
203
- - spec/normalizy/config/normalizy_raws_spec.rb
204
- - spec/normalizy/extensions/apply_normalizy_spec.rb
205
208
  - spec/normalizy/extensions/filters/date_spec.rb
206
209
  - spec/normalizy/extensions/filters/money_spec.rb
207
210
  - spec/normalizy/extensions/filters/number_spec.rb
208
211
  - spec/normalizy/extensions/filters/percent_spec.rb
209
212
  - spec/normalizy/extensions/filters/strip_spec.rb
213
+ - spec/normalizy/extensions/model_spec.rb
210
214
  - spec/normalizy/extensions/normalizy_rules_spec.rb
211
215
  - spec/normalizy/filters/date_spec.rb
212
216
  - spec/normalizy/filters/money_spec.rb
@@ -218,14 +222,20 @@ test_files:
218
222
  - spec/normalizy/rspec/matcher/failure_message_spec.rb
219
223
  - spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb
220
224
  - spec/normalizy/rspec/matcher/from_spec.rb
221
- - spec/normalizy/rspec/matcher/matchers_spec.rb
225
+ - spec/normalizy/rspec/matcher/matches_spec.rb
222
226
  - spec/normalizy/rspec/matcher/to_spec.rb
223
- - spec/normalizy/rspec/normalizy_spec.rb
224
227
  - spec/rails_helper.rb
225
228
  - spec/support/common.rb
226
229
  - spec/support/db/schema.rb
227
- - spec/support/filters/blacklist_1.rb
228
- - spec/support/filters/blacklist_2.rb
229
- - spec/support/filters/blacklist_block.rb
230
- - spec/support/models/clean.rb
231
- - spec/support/models/user.rb
230
+ - spec/support/filters/blacklist.rb
231
+ - spec/support/filters/block.rb
232
+ - spec/support/filters/info.rb
233
+ - spec/support/models/alias.rb
234
+ - spec/support/models/match.rb
235
+ - spec/support/models/model.rb
236
+ - spec/support/models/model_date.rb
237
+ - spec/support/models/model_money.rb
238
+ - spec/support/models/model_number.rb
239
+ - spec/support/models/model_percent.rb
240
+ - spec/support/models/model_strip.rb
241
+ - spec/support/models/rule.rb
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::Config, 'normalizy_raws' do
6
- it 'has the right defaults' do
7
- expect(subject.normalizy_raws).to eq %i[date money number percent]
8
- end
9
- end
@@ -1,171 +0,0 @@
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
- let!(:object) { Clean.create name: ' washington Botelho ' }
8
-
9
- it 'does not normalize' do
10
- expect(object.name).to eq ' washington Botelho '
11
- end
12
- end
13
-
14
- context 'when object has normalizy' do
15
- let!(:object) { User.new name: ' Washington Fuck Botelho ' }
16
-
17
- before { object.class.normalizy_rules = {} }
18
-
19
- context 'when a filter is not given' do
20
- context 'and no block' do
21
- before do
22
- Normalizy.configure do |config|
23
- config.default_filters = :squish
24
- end
25
-
26
- object.class.normalizy :name
27
- end
28
-
29
- it 'uses the default' do
30
- object.save
31
-
32
- expect(object.name).to eq 'Washington Fuck Botelho'
33
- end
34
- end
35
-
36
- context 'but block is' do
37
- let!(:block) { ->(value) { value.upcase } }
38
-
39
- it 'executes the block' do
40
- object.class.normalizy :name, with: block
41
-
42
- object.save
43
-
44
- expect(object.name).to eq ' WASHINGTON FUCK BOTELHO '
45
- end
46
- end
47
- end
48
-
49
- context 'when a filter is given' do
50
- context 'as a symbol' do
51
- before { object.class.normalizy :name, with: :squish }
52
-
53
- specify do
54
- object.save
55
-
56
- expect(object.name).to eq 'Washington Fuck Botelho'
57
- end
58
- end
59
-
60
- context 'as array of symbol' do
61
- before { object.class.normalizy :name, with: [:squish] }
62
-
63
- specify do
64
- object.save
65
-
66
- expect(object.name).to eq 'Washington Fuck Botelho'
67
- end
68
- end
69
-
70
- context 'as a hash' do
71
- context 'with a filter that does not accepts options' do
72
- before { object.class.normalizy :name, with: { squish: { ignored: true } } }
73
-
74
- it 'executes the filter ignoring the options' do
75
- object.save
76
-
77
- expect(object.name).to eq 'Washington Fuck Botelho'
78
- end
79
- end
80
-
81
- context 'with a filter that accepts options' do
82
- before { object.class.normalizy :name, with: { strip: { side: :left } } }
83
-
84
- it 'executes the filter with given options' do
85
- object.save
86
-
87
- expect(object.name).to eq 'Washington Fuck Botelho '
88
- end
89
- end
90
- end
91
-
92
- context 'as a module' do
93
- context 'with just one arg (input)' do
94
- before { object.class.normalizy :name, with: Normalizy::Filters::Blacklist1 }
95
-
96
- it 'receives the input value' do
97
- object.save
98
-
99
- expect(object.name).to eq ' Washington filtered Botelho '
100
- end
101
- end
102
-
103
- context 'with two args (input, options) with' do
104
- before do
105
- object.class.normalizy :name, with: Normalizy::Filters::Blacklist2
106
- end
107
-
108
- it 'receives the input value with options' do
109
- object.save
110
-
111
- expect(object.name).to eq 'name: Washington Fuck Botelho '
112
- end
113
- end
114
- end
115
-
116
- context 'and block too' do
117
- let!(:block) { ->(value) { value.upcase } }
118
-
119
- before do
120
- Normalizy.configure do |config|
121
- config.add :blacklist, Normalizy::Filters::BlacklistBlock
122
- end
123
- end
124
-
125
- it 'runs after rules' do
126
- object.class.normalizy :name, with: :blacklist, &block
127
-
128
- object.save
129
-
130
- expect(object.name).to eq ' WASHINGTON FUCK BOTELHO '
131
- end
132
- end
133
- end
134
-
135
- context 'when no filter match' do
136
- context 'and object has a method that responds' do
137
- context 'with no options on rule' do
138
- it 'runs the class method with self object on options' do
139
- object.class.normalizy :name, with: :custom_reverse
140
-
141
- object.save
142
-
143
- expect(object.name).to eq "' ohletoB kcuF notgnihsaW ' to ' Washington Fuck Botelho '"
144
- end
145
- end
146
-
147
- context 'with no options on rule' do
148
- it 'runs the class method with given options and self object on it' do
149
- object.class.normalizy :name, with: { custom_reverse: {} }
150
-
151
- object.save
152
-
153
- expect(object.name).to eq "' ohletoB kcuF notgnihsaW ' to ' Washington Fuck Botelho '"
154
- end
155
- end
156
- end
157
-
158
- context 'and attribute has not a method that responds' do
159
- context 'but attribute has a native method like it' do
160
- it 'runs the native method' do
161
- object.class.normalizy :name, with: :split
162
-
163
- object.save
164
-
165
- expect(object.name).to eq %(["Washington", "Fuck", "Botelho"])
166
- end
167
- end
168
- end
169
- end
170
- end
171
- end
@@ -1,97 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec::Matcher, '.matches?' do
6
- let!(:model) { User }
7
- let!(:matcher) { described_class.new :name }
8
- let!(:object) { model.new }
9
-
10
- before { model.normalizy_rules = {} }
11
-
12
- it 'caches the object' do
13
- matcher.matches?(object)
14
-
15
- expect(matcher.instance_variable_get(:@subject)).to eq object
16
- end
17
-
18
- context 'when .with is called' do
19
- before { matcher.with :blank }
20
-
21
- context 'but model do not has that attribute on normalizy' do
22
- specify { expect(matcher.matches?(object)).to eq false }
23
- end
24
-
25
- context 'and model has that attribute on normalizy' do
26
- before { model.normalizy :name }
27
-
28
- context 'with no filter' do
29
- specify { expect(matcher.matches?(object)).to eq false }
30
- end
31
-
32
- context 'with filter' do
33
- context 'different of expected' do
34
- let!(:matcher) { described_class.new :name }
35
-
36
- before { User.normalizy :name, with: :squish }
37
-
38
- specify { expect(matcher.matches?(User.new)).to eq false }
39
- end
40
-
41
- context 'equal of expected' do
42
- let!(:matcher) { described_class.new :name }
43
-
44
- context 'as symbol' do
45
- before do
46
- User.normalizy :name, with: :blank
47
-
48
- matcher.with :blank
49
- end
50
-
51
- specify { expect(matcher.matches?(User.new)).to eq true }
52
- end
53
-
54
- context 'as hash' do
55
- before do
56
- User.normalizy :name, with: { trim: { side: :left } }
57
-
58
- matcher.with(trim: { side: :left })
59
- end
60
-
61
- specify { expect(matcher.matches?(User.new)).to eq true }
62
- end
63
-
64
- context 'as array' do
65
- before do
66
- User.normalizy :name, with: [{ trim: { side: :left } }]
67
-
68
- matcher.with(trim: { side: :left })
69
- end
70
-
71
- specify { expect(matcher.matches?(User.new)).to eq true }
72
- end
73
- end
74
- end
75
- end
76
- end
77
-
78
- context 'when .with is not called' do
79
- context 'and :from output is different of :to' do
80
- before do
81
- matcher.from '1'
82
- matcher.to '2'
83
- end
84
-
85
- specify { expect(matcher.matches?(object)).to eq false }
86
- end
87
-
88
- context 'and :from output is equal of :to' do
89
- before do
90
- matcher.from '1'
91
- matcher.to '1'
92
- end
93
-
94
- specify { expect(matcher.matches?(object)).to eq true }
95
- end
96
- end
97
- end
@@ -1,8 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'rails_helper'
4
-
5
- RSpec.describe Normalizy::RSpec, '.normalizy' do
6
- xit 'initiates with given attribute' do
7
- end
8
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Normalizy
4
- module Filters
5
- module Blacklist2
6
- def self.call(_input, options = {})
7
- attribute = options[:attribute]
8
-
9
- "#{attribute}: #{options[:object].send(attribute)}"
10
- end
11
- end
12
- end
13
- end
@@ -1,9 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class User < ActiveRecord::Base
4
- normalizy :name
5
-
6
- def custom_reverse(input, options = {})
7
- "'#{input.reverse}' to '#{options[:object].name}'"
8
- end
9
- end