normalizy 0.1.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 (40) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +21 -0
  4. data/README.md +341 -0
  5. data/lib/generators/normalizy/install_generator.rb +13 -0
  6. data/lib/generators/normalizy/templates/config/initializers/normalizy.rb +7 -0
  7. data/lib/normalizy/config.rb +35 -0
  8. data/lib/normalizy/extensions.rb +105 -0
  9. data/lib/normalizy/filters/number.rb +17 -0
  10. data/lib/normalizy/filters/strip.rb +19 -0
  11. data/lib/normalizy/filters.rb +8 -0
  12. data/lib/normalizy/rspec/matcher.rb +115 -0
  13. data/lib/normalizy/version.rb +5 -0
  14. data/lib/normalizy.rb +20 -0
  15. data/spec/normalizy/config/add_spec.rb +17 -0
  16. data/spec/normalizy/config/alias_spec.rb +63 -0
  17. data/spec/normalizy/config/default_filters_spec.rb +19 -0
  18. data/spec/normalizy/config/initialize_spec.rb +12 -0
  19. data/spec/normalizy/config/normalizy_aliases_spec.rb +9 -0
  20. data/spec/normalizy/config/normalizy_raws_spec.rb +9 -0
  21. data/spec/normalizy/extensions/apply_normalizations_spec.rb +163 -0
  22. data/spec/normalizy/extensions/normalizy_rules_spec.rb +244 -0
  23. data/spec/normalizy/extensions/normalizy_spec.rb +11 -0
  24. data/spec/normalizy/filters/number_spec.rb +15 -0
  25. data/spec/normalizy/filters/strip_spec.rb +13 -0
  26. data/spec/normalizy/normalizy/configure_spec.rb +11 -0
  27. data/spec/normalizy/rspec/matcher/description_spec.rb +26 -0
  28. data/spec/normalizy/rspec/matcher/failure_message_spec.rb +70 -0
  29. data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +32 -0
  30. data/spec/normalizy/rspec/matcher/from_spec.rb +18 -0
  31. data/spec/normalizy/rspec/matcher/matchers_spec.rb +97 -0
  32. data/spec/normalizy/rspec/matcher/to_spec.rb +18 -0
  33. data/spec/normalizy/rspec/normalizy_spec.rb +8 -0
  34. data/spec/rails_helper.rb +9 -0
  35. data/spec/support/common.rb +11 -0
  36. data/spec/support/db/schema.rb +13 -0
  37. data/spec/support/filters/blacklist_filter.rb +15 -0
  38. data/spec/support/models/clean.rb +4 -0
  39. data/spec/support/models/user.rb +9 -0
  40. metadata +210 -0
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Normalizy::RSpec::Matcher, '.failure_message' do
6
+ let!(:matcher) { described_class.new :name }
7
+ let!(:model) { User }
8
+
9
+ before do
10
+ model.normalizy_rules = {}
11
+
12
+ matcher.from :from
13
+ matcher.to :to
14
+ matcher.matches? model.new
15
+ end
16
+
17
+ context 'with no :with expectation' do
18
+ specify do
19
+ expect(matcher.failure_message).to eq %(expected: "to"\n got: "from")
20
+ end
21
+ end
22
+
23
+ context 'when :with is expectated' do
24
+ before { matcher.with :trim }
25
+
26
+ context 'and attribute has no :with rules' do
27
+ specify do
28
+ expect(matcher.failure_message).to eq %(expected: trim\n got: nil)
29
+ end
30
+ end
31
+
32
+ context 'and attribute has a symbol as :with rule' do
33
+ before do
34
+ model.normalizy_rules = {
35
+ name: [{ block: nil, options: {}, rules: :blank }]
36
+ }
37
+ end
38
+
39
+ specify do
40
+ expect(matcher.failure_message).to eq %(expected: trim\n got: blank)
41
+ end
42
+ end
43
+
44
+ context 'and attribute has an array as :with rule' do
45
+ before do
46
+ model.normalizy_rules = {
47
+ name: [{ block: nil, options: {}, rules: [:blank] }]
48
+ }
49
+ end
50
+
51
+ specify do
52
+ expect(matcher.failure_message).to eq %(expected: trim\n got: blank)
53
+ end
54
+ end
55
+
56
+ context 'and attribute has a hash as :with rule' do
57
+ before do
58
+ model.normalizy_rules = {
59
+ name: [
60
+ { block: nil, options: {}, rules: { trim: { side: :left } } }
61
+ ]
62
+ }
63
+ end
64
+
65
+ specify do
66
+ expect(matcher.failure_message).to eq %(expected: trim\n got: {:trim=>{:side=>:left}})
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Normalizy::RSpec::Matcher, '.failure_message_when_negated' do
6
+ let!(:matcher) { described_class.new :name }
7
+ let!(:model) { User }
8
+
9
+ before do
10
+ matcher.from :from
11
+ matcher.to :to
12
+ matcher.matches? model.new
13
+ end
14
+
15
+ context 'with no :with expectation' do
16
+ specify do
17
+ expect(matcher.failure_message_when_negated).to eq %(expected: value != "to"\n got: "from")
18
+ end
19
+ end
20
+
21
+ context 'with :with expectation' do
22
+ before do
23
+ model.normalizy_rules = {}
24
+
25
+ matcher.with :blank
26
+ end
27
+
28
+ it 'will be nil since script does not initialized it with memo hash' do
29
+ expect(matcher.failure_message_when_negated).to eq %(expected: value != blank\n got: nil)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Normalizy::RSpec::Matcher, '.from' do
6
+ let!(:matcher) { described_class.new :name }
7
+ let!(:model) { User }
8
+
9
+ it 'caches the value' do
10
+ matcher.from :from
11
+
12
+ expect(matcher.instance_variable_get(:@from)).to eq :from
13
+ end
14
+
15
+ it 'returns it self' do
16
+ expect(matcher.from(:from)).to be matcher
17
+ end
18
+ end
@@ -0,0 +1,97 @@
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
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ RSpec.describe Normalizy::RSpec::Matcher, '.to' do
6
+ let!(:matcher) { described_class.new :name }
7
+ let!(:model) { User }
8
+
9
+ it 'caches the value' do
10
+ matcher.to :to
11
+
12
+ expect(matcher.instance_variable_get(:@to)).to eq :to
13
+ end
14
+
15
+ it 'returns it self' do
16
+ expect(matcher.to(:to)).to be matcher
17
+ end
18
+ end
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ ENV['RAILS_ENV'] ||= 'test'
4
+
5
+ require 'active_record/railtie'
6
+ require 'normalizy'
7
+ require 'pry-byebug'
8
+
9
+ Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file }
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/rails'
4
+
5
+ RSpec.configure do |config|
6
+ config.filter_run_when_matching :focus
7
+
8
+ config.disable_monkey_patching!
9
+
10
+ config.order = :random
11
+ end
@@ -0,0 +1,13 @@
1
+ ActiveRecord::Base.establish_connection adapter: :sqlite3, database: ':memory:'
2
+
3
+ ActiveRecord::Schema.define(version: 0) do
4
+ create_table :cleans do |t|
5
+ t.string :name
6
+ end
7
+
8
+ create_table :users do |t|
9
+ t.integer :age
10
+ t.integer :amount
11
+ t.string :name
12
+ end
13
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Normalizy
4
+ module Filters
5
+ module Blacklist
6
+ def self.call(input)
7
+ value = input.gsub('Fuck', 'filtered')
8
+
9
+ value = yield(value) if block_given?
10
+
11
+ value
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Clean < ActiveRecord::Base
4
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class User < ActiveRecord::Base
4
+ normalizy :name
5
+
6
+ def custom_reverse(input, options = {})
7
+ "#{input.reverse}.#{options}.custom"
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,210 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: normalizy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Washington Botelho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.1'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.1'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: guard-rspec
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-rails
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop-rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rubocop
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: sqlite3
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: pry-byebug
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ description: Attribute normalizer for ActiveRecord.
118
+ email: wbotelhos@gmail.com
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - CHANGELOG.md
124
+ - LICENSE
125
+ - README.md
126
+ - lib/generators/normalizy/install_generator.rb
127
+ - lib/generators/normalizy/templates/config/initializers/normalizy.rb
128
+ - lib/normalizy.rb
129
+ - lib/normalizy/config.rb
130
+ - lib/normalizy/extensions.rb
131
+ - lib/normalizy/filters.rb
132
+ - lib/normalizy/filters/number.rb
133
+ - lib/normalizy/filters/strip.rb
134
+ - lib/normalizy/rspec/matcher.rb
135
+ - lib/normalizy/version.rb
136
+ - spec/normalizy/config/add_spec.rb
137
+ - spec/normalizy/config/alias_spec.rb
138
+ - spec/normalizy/config/default_filters_spec.rb
139
+ - spec/normalizy/config/initialize_spec.rb
140
+ - spec/normalizy/config/normalizy_aliases_spec.rb
141
+ - spec/normalizy/config/normalizy_raws_spec.rb
142
+ - spec/normalizy/extensions/apply_normalizations_spec.rb
143
+ - spec/normalizy/extensions/normalizy_rules_spec.rb
144
+ - spec/normalizy/extensions/normalizy_spec.rb
145
+ - spec/normalizy/filters/number_spec.rb
146
+ - spec/normalizy/filters/strip_spec.rb
147
+ - spec/normalizy/normalizy/configure_spec.rb
148
+ - spec/normalizy/rspec/matcher/description_spec.rb
149
+ - spec/normalizy/rspec/matcher/failure_message_spec.rb
150
+ - spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb
151
+ - spec/normalizy/rspec/matcher/from_spec.rb
152
+ - spec/normalizy/rspec/matcher/matchers_spec.rb
153
+ - spec/normalizy/rspec/matcher/to_spec.rb
154
+ - spec/normalizy/rspec/normalizy_spec.rb
155
+ - spec/rails_helper.rb
156
+ - spec/support/common.rb
157
+ - spec/support/db/schema.rb
158
+ - spec/support/filters/blacklist_filter.rb
159
+ - spec/support/models/clean.rb
160
+ - spec/support/models/user.rb
161
+ homepage: https://github.com/wbotelhos/normalizy
162
+ licenses:
163
+ - MIT
164
+ metadata: {}
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubyforge_project:
181
+ rubygems_version: 2.6.11
182
+ signing_key:
183
+ specification_version: 4
184
+ summary: Attribute normalizer for ActiveRecord.
185
+ test_files:
186
+ - spec/normalizy/config/add_spec.rb
187
+ - spec/normalizy/config/alias_spec.rb
188
+ - spec/normalizy/config/default_filters_spec.rb
189
+ - spec/normalizy/config/initialize_spec.rb
190
+ - spec/normalizy/config/normalizy_aliases_spec.rb
191
+ - spec/normalizy/config/normalizy_raws_spec.rb
192
+ - spec/normalizy/extensions/apply_normalizations_spec.rb
193
+ - spec/normalizy/extensions/normalizy_rules_spec.rb
194
+ - spec/normalizy/extensions/normalizy_spec.rb
195
+ - spec/normalizy/filters/number_spec.rb
196
+ - spec/normalizy/filters/strip_spec.rb
197
+ - spec/normalizy/normalizy/configure_spec.rb
198
+ - spec/normalizy/rspec/matcher/description_spec.rb
199
+ - spec/normalizy/rspec/matcher/failure_message_spec.rb
200
+ - spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb
201
+ - spec/normalizy/rspec/matcher/from_spec.rb
202
+ - spec/normalizy/rspec/matcher/matchers_spec.rb
203
+ - spec/normalizy/rspec/matcher/to_spec.rb
204
+ - spec/normalizy/rspec/normalizy_spec.rb
205
+ - spec/rails_helper.rb
206
+ - spec/support/common.rb
207
+ - spec/support/db/schema.rb
208
+ - spec/support/filters/blacklist_filter.rb
209
+ - spec/support/models/clean.rb
210
+ - spec/support/models/user.rb