normalizr 0.4.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d46a399a42b6686eb841774af8c65fe00a01440b8b987babae87b43c8f3ea557
4
- data.tar.gz: cae7060a66923fe7a5f208d7e7b5649d3d49c1c02780361c88400eef3f451ae7
3
+ metadata.gz: 6df1b6a2804509540eacc96b2df9fd1a35ef129f64e88d672bdad2361dd92513
4
+ data.tar.gz: 46d9326603da3d5741c9174e7a579c1cc994463cdbafaeeabd13fd4bff6002d4
5
5
  SHA512:
6
- metadata.gz: 89c5535023a0c3119f959f782c112eb70c1525ba7dc3e82eb45bad8d9702b393df73683af9a3ca5f1d1a085a4888f80a9c0142eb3def7f3c0c76ea9def169895
7
- data.tar.gz: e6a67050e0f4d1761db25b0fd72cbaf4b4f241489016a3076cfcb5768cf8290cf9f69af2e774aa575d8ee25b07397eecd4ad843fc35e133293508732ab864bd6
6
+ metadata.gz: b01d5e605d9b743a6763d68f00a14e61f70314c7f666ddc628cb1930c49acabf5faf85c2f0cccb8f5652b706f9dc6a158723e027352c792ced274338e7f946cb
7
+ data.tar.gz: 2a2501d6aca9ea1b4afdfd5b1d976b1c16cae4e285bf2f6d8cd2b143a69619d680a7b48e2c3b958810e8f057316718c91b5493d13a2fee89b1958e6202efa5e5
@@ -9,7 +9,8 @@ gemfile:
9
9
  - gemfiles/activerecord-6.0.x.gemfile
10
10
  addons:
11
11
  code_climate:
12
- repo_token: 1cac54041089115a5bf218169b62c76c8b87a5896f5aa973f89637a8553c0376
12
+ repo_token:
13
+ secure: Dn+nXUOFHTRUhYqYM1NYSxYSyoDhFaB2EHvlhBJ/FK3QxCy4a0beSLhs+YnDl8kI1DOrZQMl7HDrh9AHlfYvu5fX4zADD5g7JkTA2mIx1Rk3yazAUuy1j1ftX/UaowzoUAR5t0qwYm344Nnqoh+O1FujvWbiGVuZvWa6vfjFbXA=
13
14
  matrix:
14
15
  fast_finish: true
15
16
  before_install:
@@ -1,3 +1,7 @@
1
+ ### 0.5.0 / 2020-07-27
2
+
3
+ * Add ability to use default and custom normalizers together (@subbbotin)
4
+
1
5
  ### 0.4.0 / 2020-01-20
2
6
 
3
7
  * Support Ruby 2.7 (@badlamer)
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
@@ -42,12 +42,21 @@ module Normalizr
42
42
  end
43
43
 
44
44
  def normalize(obj, *normalizers)
45
- normalizers = configuration.default_normalizers if normalizers.empty?
46
- normalizers.each do |name|
45
+ normalizers_with_default(normalizers).reduce(obj) do |memo, name|
47
46
  name, options = name.first if Hash === name
48
- obj = process(obj, name, options)
47
+
48
+ process(memo, name, options)
49
49
  end
50
- 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
51
60
  end
52
61
  end
53
62
 
@@ -1,3 +1,3 @@
1
1
  module Normalizr
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
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($/)
@@ -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
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-20 00:00:00.000000000 Z
11
+ date: 2020-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -191,7 +191,7 @@ files:
191
191
  - spec/support/models/publisher.rb
192
192
  - spec/support/models/user.rb
193
193
  - spec/support/schema.rb
194
- homepage: https://github.com/dimko/normalizr
194
+ homepage: https://github.com/dmeremyanin/normalizr
195
195
  licenses:
196
196
  - MIT
197
197
  metadata: {}
@@ -210,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
210
210
  - !ruby/object:Gem::Version
211
211
  version: '0'
212
212
  requirements: []
213
- rubygems_version: 3.0.3
213
+ rubygems_version: 3.0.8
214
214
  signing_key:
215
215
  specification_version: 4
216
216
  summary: Writer methods parameters normalization