normalizr 0.3.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '068b8f6b2685d273ee884c7a8c0f751da02fd254fe56f0ee6366f304910f81f6'
4
- data.tar.gz: 9edd590a85219a2ea44651412e32fced140dad016b3035acadcd1ebc43df2408
3
+ metadata.gz: 4ad46e009a59073eae3f65cfd60449c60fbc1570c5dde582de56f04a449416fa
4
+ data.tar.gz: 4b83cf27a15fcf5b1d2087dbada0ecad50a4d807be26e2fb6a2c91c6705adef6
5
5
  SHA512:
6
- metadata.gz: eb22312649da1951b0fe9d0d1214851ebeb62d408217156a0427a55b442814505e68786a513898e8ca8b095701cd47d4880e5c82ba943993b1516cbfd8b04774
7
- data.tar.gz: 06c555da8af5e30f7527927518e28186577ac1baf2c36b0a0ae9ce3a4596c6cc15eb13c08f9a361e833e84665eb4ed769c9f3ff9d3d4f725db2d8cfa5eb82e37
6
+ metadata.gz: 76b3b482de58da7f8af729c3d8392f0ef074ae9a7a05bbea55e3cd19d02c4297c6fa1bf01e924ec14aaa9666ad8689c1d91076e5588647b77341a39554e106d9
7
+ data.tar.gz: 1e8a7051bf44e55fbc1a81459670c4ec6b4d463adaa04b5a5179a40727cc17a3365c222c0bd8a657562d0f3d133ab442464a95787d0dc49ef1b877d02d6365d2
data/.travis.yml CHANGED
@@ -1,18 +1,16 @@
1
1
  language: ruby
2
2
  cache: bundler
3
3
  rvm:
4
- - 2.0.0
5
- - 2.1.8
6
- - 2.2.4
7
- - 2.3.0
4
+ - 2.5.7
5
+ - 2.6.5
6
+ - 2.7.0
8
7
  gemfile:
9
- - gemfiles/activerecord-3.2.x.gemfile
10
- - gemfiles/activerecord-4.0.x.gemfile
11
- - gemfiles/activerecord-4.1.x.gemfile
12
- - gemfiles/activerecord-4.2.x.gemfile
8
+ - gemfiles/activerecord-5.2.x.gemfile
9
+ - gemfiles/activerecord-6.0.x.gemfile
13
10
  addons:
14
11
  code_climate:
15
- repo_token: 1cac54041089115a5bf218169b62c76c8b87a5896f5aa973f89637a8553c0376
12
+ repo_token:
13
+ secure: Dn+nXUOFHTRUhYqYM1NYSxYSyoDhFaB2EHvlhBJ/FK3QxCy4a0beSLhs+YnDl8kI1DOrZQMl7HDrh9AHlfYvu5fX4zADD5g7JkTA2mIx1Rk3yazAUuy1j1ftX/UaowzoUAR5t0qwYm344Nnqoh+O1FujvWbiGVuZvWa6vfjFbXA=
16
14
  matrix:
17
15
  fast_finish: true
18
16
  before_install:
data/CHANGELOG.md CHANGED
@@ -1,3 +1,20 @@
1
+ ### 0.6.0 / 2021-07-29
2
+
3
+ * Use on_load hook for integration with ActiveRecord (@sikachu)
4
+ * Use update instead of update_attributes (@sikachu)
5
+
6
+ ### 0.5.0 / 2020-07-27
7
+
8
+ * Add ability to use default and custom normalizers together (@subbbotin)
9
+
10
+ ### 0.4.0 / 2020-01-20
11
+
12
+ * Support Ruby 2.7 (@badlamer)
13
+
14
+ ### 0.3.1 / 2018-03-22
15
+
16
+ * Fix conditional normalization
17
+
1
18
  ### 0.3.0 / 2018-03-21
2
19
 
3
20
  * Add a possibility to use if/unless cases for normalizer options (@TheTharin)
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  ## Normalizr
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/normalizr.svg)](https://badge.fury.io/rb/normalizr)
4
- [![Build Status](https://travis-ci.org/dimko/normalizr.svg?branch=master)](https://travis-ci.org/dimko/normalizr)
5
- [![Code Climate](https://codeclimate.com/github/dimko/normalizr/badges/gpa.svg)](https://codeclimate.com/github/dimko/normalizr)
6
- [![Test Coverage](https://codeclimate.com/github/dimko/normalizr/badges/coverage.svg)](https://codeclimate.com/github/dimko/normalizr/coverage)
4
+ [![Build Status](https://travis-ci.org/dmeremyanin/normalizr.svg?branch=master)](https://travis-ci.org/dmeremyanin/normalizr)
5
+ [![Code Climate](https://codeclimate.com/github/dmeremyanin/normalizr/badges/gpa.svg)](https://codeclimate.com/github/dmeremyanin/normalizr)
6
+ [![Test Coverage](https://codeclimate.com/github/dmeremyanin/normalizr/badges/coverage.svg)](https://codeclimate.com/github/dmeremyanin/normalizr/coverage)
7
7
 
8
8
  The [attribute_normalizer](https://github.com/mdeering/attribute_normalizer) replacement.
9
9
 
@@ -88,6 +88,9 @@ class User < ActiveRecord::Base
88
88
  normalize :first_name, :last_name, :about # with default normalizers
89
89
  normalize :email, with: :downcase
90
90
 
91
+ # you can use default and custom normalizers together
92
+ normalize :middle_name, with: [:default, :titleize]
93
+
91
94
  # supports `normalize_attribute` and `normalize_attributes` as well
92
95
  normalize_attribute :skype
93
96
 
@@ -95,13 +98,15 @@ class User < ActiveRecord::Base
95
98
  normalize :skills
96
99
  end
97
100
 
98
- user = User.new(first_name: '', last_name: '', skills: [nil, '', ' ruby'])
101
+ user = User.new(first_name: '', last_name: '', middle_name: 'elizabeth ', skills: [nil, '', ' ruby'])
99
102
  user.email = "ADDRESS@example.com"
100
103
 
101
104
  user.first_name
102
105
  #=> nil
103
106
  user.last_name
104
107
  #=> nil
108
+ user.middle_name
109
+ #=> "Elizabeth"
105
110
  user.email
106
111
  #=> "address@example.com"
107
112
  user.skills
@@ -140,8 +145,8 @@ class Book
140
145
 
141
146
  normalize :author, if: :author_should_be_normalized?
142
147
  normalize :description, unless: :description_should_not_be_normalized?
143
-
144
- normalize :author, if: proc { date.today? }
148
+
149
+ normalize :author, if: -> { date.today? }
145
150
  end
146
151
  ```
147
152
 
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'activerecord', '~> 3.2.0'
4
- gem 'mongoid', '~> 3.0'
3
+ gem 'activerecord', '~> 5.2.0'
4
+ gem 'mongoid', '~> 6.0'
5
5
  gem 'codeclimate-test-reporter', require: false
6
6
 
7
7
  gemspec path: '../'
@@ -1,7 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'activerecord', '~> 4.0.0'
4
- gem 'mongoid', '~> 4.0'
3
+ gem 'activerecord', '~> 6.0.0'
4
+ gem 'mongoid', '~> 7.0'
5
5
  gem 'codeclimate-test-reporter', require: false
6
6
 
7
7
  gemspec path: '../'
data/lib/normalizr.rb CHANGED
@@ -15,8 +15,12 @@ module Normalizr
15
15
  @configuration ||= Configuration.new
16
16
  end
17
17
 
18
- def configure
19
- configuration.instance_eval &Proc.new
18
+ def configure(&block)
19
+ unless block_given?
20
+ raise ArgumentError, '.configure must be called with a block'
21
+ end
22
+
23
+ configuration.instance_eval(&block)
20
24
  end
21
25
 
22
26
  def find(name)
@@ -38,12 +42,21 @@ module Normalizr
38
42
  end
39
43
 
40
44
  def normalize(obj, *normalizers)
41
- normalizers = configuration.default_normalizers if normalizers.empty?
42
- normalizers.each do |name|
45
+ normalizers_with_default(normalizers).reduce(obj) do |memo, name|
43
46
  name, options = name.first if Hash === name
44
- obj = process(obj, name, options)
47
+
48
+ process(memo, name, options)
45
49
  end
46
- obj
50
+ end
51
+
52
+ private
53
+
54
+ def normalizers_with_default(normalizers)
55
+ default_index = normalizers.index(:default)
56
+
57
+ normalizers[default_index.to_i] = configuration.default_normalizers if normalizers.empty? || default_index
58
+ normalizers.flatten!
59
+ normalizers
47
60
  end
48
61
  end
49
62
 
@@ -11,8 +11,10 @@ module Normalizr
11
11
  prepend Module.new {
12
12
  options.attributes.each do |method|
13
13
  define_method :"#{method}=" do |value|
14
- positive = options.positive_condition.all? { |condition| instance_eval(&condition) }
15
- negative = options.negative_condition.none? { |condition| instance_eval(&condition) }
14
+ condition_lambda = -> condition { Proc === condition ? instance_exec(&condition) : send(condition) }
15
+
16
+ positive = options.positive_condition.all?(&condition_lambda)
17
+ negative = options.negative_condition.none?(&condition_lambda)
16
18
 
17
19
  if positive && negative
18
20
  value = Normalizr.normalize(value, *options.before)
@@ -14,8 +14,12 @@ module Normalizr
14
14
  self.default_normalizers = normalizers
15
15
  end
16
16
 
17
- def add(name)
18
- normalizers[name] = Proc.new
17
+ def add(name, &block)
18
+ unless block_given?
19
+ raise ArgumentError, '#add must be called with a block'
20
+ end
21
+
22
+ normalizers[name] = block
19
23
  end
20
24
  end
21
25
  end
@@ -1 +1,3 @@
1
- ActiveRecord::Base.send(:include, Normalizr::Concern)
1
+ ActiveSupport.on_load(:active_record) do
2
+ include Normalizr::Concern
3
+ end
@@ -1,3 +1,3 @@
1
1
  module Normalizr
2
- VERSION = '0.3.0'
2
+ VERSION = '0.6.0'
3
3
  end
data/normalizr.gemspec CHANGED
@@ -5,11 +5,11 @@ require 'normalizr/version'
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'normalizr'
7
7
  spec.version = Normalizr::VERSION
8
- spec.authors = ['Dimko']
8
+ spec.authors = ['Dmitriy Meremyanin']
9
9
  spec.email = ['deemox@gmail.com']
10
10
  spec.description = 'Writer methods parameters normalization'
11
11
  spec.summary = 'Writer methods parameters normalization'
12
- spec.homepage = 'https://github.com/dimko/normalizr'
12
+ spec.homepage = 'https://github.com/dmeremyanin/normalizr'
13
13
  spec.license = 'MIT'
14
14
 
15
15
  spec.files = `git ls-files`.split($/)
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.required_ruby_version = '>= 2.0'
20
20
 
21
21
  spec.add_development_dependency 'pry'
22
- spec.add_development_dependency 'bundler', '~> 1.5'
22
+ spec.add_development_dependency 'bundler'
23
23
  spec.add_development_dependency 'rake'
24
24
  spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  spec.add_development_dependency 'rspec-its'
@@ -5,17 +5,27 @@ describe Normalizr::Configuration do
5
5
  subject { configuration }
6
6
 
7
7
  describe '#default' do
8
- before { configuration.default(:blank) }
9
- its(:default_normalizers) { should == [:blank] }
8
+ it 'accpets default normalizers' do
9
+ subject.default(:blank)
10
+ expect(subject.default_normalizers).to eq([:blank])
11
+ end
10
12
  end
11
13
 
12
14
  describe '#normalizer_names' do
13
- before { allow(configuration).to receive(:normalizers) {{ blank: proc {}}}}
14
- its(:normalizer_names) { should == [:blank] }
15
+ it 'returns normalized names' do
16
+ allow(subject).to receive(:normalizers) {{ blank: proc {}}}
17
+ expect(subject.normalizer_names).to eq([:blank])
18
+ end
15
19
  end
16
20
 
17
21
  describe '#add' do
18
- before { configuration.add(:blank) {}}
19
- its(:normalizer_names) { should include(:blank) }
22
+ it 'requires block' do
23
+ expect { subject.add(:blank) }.to raise_error(ArgumentError)
24
+ end
25
+
26
+ it 'accepts block' do
27
+ subject.add(:blank) {}
28
+ expect(subject.normalizer_names).to include(:blank)
29
+ end
20
30
  end
21
31
  end
@@ -119,5 +119,26 @@ describe Normalizr do
119
119
  end
120
120
  end
121
121
  end
122
+
123
+ context 'normalizers are default and custom' do
124
+ let(:normalizers) { [:default, { truncate: { length: 8, omission: '...' }}, proc { |v| v.first(6) }] }
125
+ let(:value) { ' Lorem ipsum dolor sit amet' }
126
+
127
+ context 'string' do
128
+ let(:value) { ' Lorem ipsum dolor sit amet' }
129
+
130
+ it 'uses the default normalizers, then truncates and slices the value' do
131
+ expect(subject).to eq('Lorem.')
132
+ end
133
+ end
134
+
135
+ context 'array' do
136
+ let(:value) { [' Lorem ipsum dolor sit amet', ' One more sentence', ''] }
137
+
138
+ it 'strips, truncates and then slices the value' do
139
+ expect(subject).to eq(['Lorem.', 'One m.'])
140
+ end
141
+ end
142
+ end
122
143
  end
123
144
  end
@@ -21,7 +21,7 @@ describe Article do
21
21
  before do
22
22
  @article = Article.create!(title: 'Original Title')
23
23
  @article2 = Article.find(@article.id)
24
- @article2.update_attributes(title: 'New Title')
24
+ @article2.update(title: 'New Title')
25
25
  end
26
26
 
27
27
  it "should reflect the change when the record is reloaded" do
@@ -25,7 +25,7 @@ describe Book do
25
25
  before do
26
26
  @book = Book.create!(title: 'Original Title')
27
27
  @book2 = Book.find(@book.id)
28
- @book2.update_attributes(title: 'New Title')
28
+ @book2.update(title: 'New Title')
29
29
  end
30
30
 
31
31
  it "should reflect the change when the record is reloaded" do
@@ -3,7 +3,9 @@ class User < Person
3
3
 
4
4
  normalize :lastname, if: :should_normalize_lastname?
5
5
  normalize :maidenname, if: :should_normalize_maidenname_positive?, unless: :should_normalize_maidenname_negative?
6
- normalize :favouritebook, if: proc { true }
6
+ normalize :favouritebook, if: -> { true }
7
+
8
+ private
7
9
 
8
10
  def should_normalize_lastname?
9
11
  true
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: normalizr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
- - Dimko
8
- autorequire:
7
+ - Dmitriy Meremyanin
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-03-21 00:00:00.000000000 Z
11
+ date: 2021-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.5'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.5'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -151,10 +151,8 @@ files:
151
151
  - LICENSE.txt
152
152
  - README.md
153
153
  - Rakefile
154
- - gemfiles/activerecord-3.2.x.gemfile
155
- - gemfiles/activerecord-4.0.x.gemfile
156
- - gemfiles/activerecord-4.1.x.gemfile
157
- - gemfiles/activerecord-4.2.x.gemfile
154
+ - gemfiles/activerecord-5.2.x.gemfile
155
+ - gemfiles/activerecord-6.0.x.gemfile
158
156
  - lib/normalizr.rb
159
157
  - lib/normalizr/concern.rb
160
158
  - lib/normalizr/configuration.rb
@@ -193,11 +191,11 @@ files:
193
191
  - spec/support/models/publisher.rb
194
192
  - spec/support/models/user.rb
195
193
  - spec/support/schema.rb
196
- homepage: https://github.com/dimko/normalizr
194
+ homepage: https://github.com/dmeremyanin/normalizr
197
195
  licenses:
198
196
  - MIT
199
197
  metadata: {}
200
- post_install_message:
198
+ post_install_message:
201
199
  rdoc_options: []
202
200
  require_paths:
203
201
  - lib
@@ -212,9 +210,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
212
210
  - !ruby/object:Gem::Version
213
211
  version: '0'
214
212
  requirements: []
215
- rubyforge_project:
216
- rubygems_version: 2.7.6
217
- signing_key:
213
+ rubygems_version: 3.1.6
214
+ signing_key:
218
215
  specification_version: 4
219
216
  summary: Writer methods parameters normalization
220
217
  test_files:
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activerecord', '~> 4.1.0'
4
- gem 'mongoid', '~> 4.0'
5
- gem 'codeclimate-test-reporter', require: false
6
-
7
- gemspec path: '../'
@@ -1,7 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- gem 'activerecord', '~> 4.2.0'
4
- gem 'mongoid', '~> 4.0'
5
- gem 'codeclimate-test-reporter', require: false
6
-
7
- gemspec path: '../'