normalizr 0.1.1 → 0.2.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 +4 -4
- data/.travis.yml +7 -3
- data/CHANGELOG.md +8 -0
- data/README.md +10 -2
- data/lib/normalizr.rb +13 -3
- data/lib/normalizr/concern.rb +2 -2
- data/lib/normalizr/options_parser.rb +2 -2
- data/lib/normalizr/version.rb +1 -1
- data/spec/lib/normalizr_spec.rb +76 -9
- data/spec/models/book_spec.rb +9 -6
- data/spec/support/models/book.rb +3 -0
- data/spec/support/schema.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c92e29d4c962eab7b59f45d08e1491ab5198071b
|
4
|
+
data.tar.gz: b36c1555e4e4680972c64696d85a00a4117ce73b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 409bda7a9f3416b22acc330674aafdacba08b0fd1747f2c3f4896081fee9b7e203606d26dc33a556d61e90b8668b04d2224c639c2ff4c485398d772d5760a2b9
|
7
|
+
data.tar.gz: 6dc1d908fe0fb69c046120438bbe344d33d7238771a837fa8fec8fe6fbd7f738cec01ea5e29b783ade832447c27b0d18f36f82efea337810b260803b728682af
|
data/.travis.yml
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
language: ruby
|
2
2
|
cache: bundler
|
3
3
|
rvm:
|
4
|
-
- 2.0
|
5
|
-
- 2.1
|
6
|
-
- 2.2
|
4
|
+
- 2.0.0
|
5
|
+
- 2.1.8
|
6
|
+
- 2.2.4
|
7
|
+
- 2.3.0
|
7
8
|
gemfile:
|
8
9
|
- gemfiles/activerecord-3.2.x.gemfile
|
9
10
|
- gemfiles/activerecord-4.0.x.gemfile
|
@@ -14,3 +15,6 @@ addons:
|
|
14
15
|
repo_token: 1cac54041089115a5bf218169b62c76c8b87a5896f5aa973f89637a8553c0376
|
15
16
|
matrix:
|
16
17
|
fast_finish: true
|
18
|
+
before_install:
|
19
|
+
- gem install bundler
|
20
|
+
sudo: false
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
## Normalizr
|
2
2
|
|
3
|
-
[](https://badge.fury.io/rb/normalizr)
|
4
|
+
[](https://travis-ci.org/dimko/normalizr)
|
5
|
+
[](https://codeclimate.com/github/dimko/normalizr)
|
6
|
+
[](https://codeclimate.com/github/dimko/normalizr/coverage)
|
4
7
|
|
5
8
|
The [attribute_normalizer](https://github.com/mdeering/attribute_normalizer) replacement.
|
6
9
|
|
@@ -87,9 +90,12 @@ class User < ActiveRecord::Base
|
|
87
90
|
|
88
91
|
# supports `normalize_attribute` and `normalize_attributes` as well
|
89
92
|
normalize_attribute :skype
|
93
|
+
|
94
|
+
# array normalization is supported too
|
95
|
+
normalize :skills
|
90
96
|
end
|
91
97
|
|
92
|
-
user = User.new(first_name: '', last_name: '')
|
98
|
+
user = User.new(first_name: '', last_name: '', skills: [nil, '', ' ruby'])
|
93
99
|
user.email = "ADDRESS@example.com"
|
94
100
|
|
95
101
|
user.first_name
|
@@ -98,6 +104,8 @@ user.last_name
|
|
98
104
|
#=> nil
|
99
105
|
user.email
|
100
106
|
#=> "address@example.com"
|
107
|
+
user.skills
|
108
|
+
#=> ["ruby"]
|
101
109
|
```
|
102
110
|
|
103
111
|
```ruby
|
data/lib/normalizr.rb
CHANGED
@@ -27,13 +27,23 @@ module Normalizr
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def process(obj, name, options)
|
31
|
+
if Array === obj
|
32
|
+
obj.map { |item| process(item, name, options) }.tap do |ary|
|
33
|
+
ary.compact! if name == :blank
|
34
|
+
end
|
35
|
+
else
|
36
|
+
find(name).call(obj, options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def normalize(obj, *normalizers)
|
31
41
|
normalizers = configuration.default_normalizers if normalizers.empty?
|
32
42
|
normalizers.each do |name|
|
33
43
|
name, options = name.first if Hash === name
|
34
|
-
|
44
|
+
obj = process(obj, name, options)
|
35
45
|
end
|
36
|
-
|
46
|
+
obj
|
37
47
|
end
|
38
48
|
end
|
39
49
|
|
data/lib/normalizr/concern.rb
CHANGED
@@ -11,8 +11,8 @@ module Normalizr
|
|
11
11
|
prepend Module.new {
|
12
12
|
options.attributes.each do |method|
|
13
13
|
define_method :"#{method}=" do |value|
|
14
|
-
value = Normalizr.normalize(value, *options.
|
15
|
-
value = Normalizr.normalize(value, *options.
|
14
|
+
value = Normalizr.normalize(value, *options.before)
|
15
|
+
value = Normalizr.normalize(value, *options.after) if options.after.any?
|
16
16
|
super(value)
|
17
17
|
end
|
18
18
|
end
|
data/lib/normalizr/version.rb
CHANGED
data/spec/lib/normalizr_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe Normalizr do
|
|
17
17
|
context 'not existed normalizer' do
|
18
18
|
let(:normalizer) { :not_existed }
|
19
19
|
|
20
|
-
it '
|
20
|
+
it 'raises the exception with normalizer in message' do
|
21
21
|
expect { subject }.to raise_error(Normalizr::MissingNormalizer, /not_existed/)
|
22
22
|
end
|
23
23
|
end
|
@@ -31,25 +31,92 @@ describe Normalizr do
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
describe '#process' do
|
35
|
+
subject { described_class.process(obj, name, {}) }
|
36
|
+
|
37
|
+
context 'string' do
|
38
|
+
let(:obj) { ' test ' }
|
39
|
+
|
40
|
+
context 'blank normalizer' do
|
41
|
+
let(:name) { :strip }
|
42
|
+
|
43
|
+
it 'strips the value' do
|
44
|
+
should == 'test'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'array ' do
|
50
|
+
let(:obj) { [nil, ' '] }
|
51
|
+
|
52
|
+
context 'strip normalizer' do
|
53
|
+
let(:name) { :strip }
|
54
|
+
|
55
|
+
it 'strips all elements' do
|
56
|
+
expect(subject).to eq([nil, ''])
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'custom normalizer' do
|
61
|
+
let(:name) { proc { |value| value.prepend('N:') if String === value }}
|
62
|
+
|
63
|
+
it 'processes all elements with the custom normalizer' do
|
64
|
+
expect(subject).to eq([nil, 'N: '])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'blank normalizer' do
|
69
|
+
let(:name) { :blank }
|
70
|
+
|
71
|
+
it 'returns empty array' do
|
72
|
+
expect(subject).to eq([])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
34
78
|
describe '#normalize' do
|
35
79
|
before { configuration.default_normalizers = [:strip, :blank] }
|
36
80
|
subject { described_class.normalize(value, *normalizers) }
|
37
81
|
|
38
|
-
|
82
|
+
|
83
|
+
context 'normalizers are empty' do
|
39
84
|
let(:normalizers) { [] }
|
40
|
-
let(:value) { ' text ' }
|
41
85
|
|
42
|
-
|
43
|
-
|
86
|
+
context 'string' do
|
87
|
+
let(:value) { ' text ' }
|
88
|
+
|
89
|
+
it 'uses the default normalizers' do
|
90
|
+
expect(subject).to eq('text')
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'array' do
|
95
|
+
let(:value) { [nil, ' test '] }
|
96
|
+
|
97
|
+
it 'uses the default normalizers' do
|
98
|
+
expect(subject).to eq(['test'])
|
99
|
+
end
|
44
100
|
end
|
45
101
|
end
|
46
102
|
|
47
|
-
context 'normalizers
|
103
|
+
context 'normalizers are not empty' do
|
48
104
|
let(:normalizers) { [:strip, { truncate: { length: 8, omission: '...' }}, proc { |v| v.first(6) }] }
|
49
|
-
let(:value) { ' Lorem ipsum dolor sit amet' }
|
50
105
|
|
51
|
-
|
52
|
-
|
106
|
+
context 'string' do
|
107
|
+
let(:value) { ' Lorem ipsum dolor sit amet' }
|
108
|
+
|
109
|
+
it 'strips, truncates and then slices the value' do
|
110
|
+
expect(subject).to eq('Lorem.')
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context 'array' do
|
115
|
+
let(:value) { [' Lorem ipsum dolor sit amet', ' One more sentence'] }
|
116
|
+
|
117
|
+
it 'strips, truncates and then slices the value' do
|
118
|
+
expect(subject).to eq(['Lorem.', 'One m.'])
|
119
|
+
end
|
53
120
|
end
|
54
121
|
end
|
55
122
|
end
|
data/spec/models/book_spec.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Book do
|
4
|
-
it { should
|
5
|
-
it { should
|
6
|
-
it { should
|
7
|
-
it { should
|
8
|
-
|
9
|
-
|
4
|
+
it { should normalize(:author).from(' Michael Deering ').to('Michael Deering') }
|
5
|
+
it { should normalize(:us_price).from('$3.50').to(3.50) }
|
6
|
+
it { should normalize(:cnd_price).from('$3,450.98').to(3450.98) }
|
7
|
+
it { should normalize(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
|
8
|
+
it { should normalize(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks') }
|
9
|
+
|
10
|
+
it { should normalize(:tags).from(['', nil, ' ', ' test']).to(['test']) }
|
11
|
+
it { should normalize(:tags).from([[''], [nil, ' test ']]).to([[], ['test']]) } # nested arrays
|
12
|
+
it { should normalize(:tags).from([]).to([]) }
|
10
13
|
|
11
14
|
context 'normalization should not interfere with other hooks and aliases on the attribute assignment' do
|
12
15
|
before do
|
data/spec/support/models/book.rb
CHANGED
data/spec/support/schema.rb
CHANGED
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
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dimko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|