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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9f01b405bb0c605fb3d1ee13165c30708a209ac2
4
- data.tar.gz: 585231449522b519400d785946f8d226fee9a1c2
3
+ metadata.gz: c92e29d4c962eab7b59f45d08e1491ab5198071b
4
+ data.tar.gz: b36c1555e4e4680972c64696d85a00a4117ce73b
5
5
  SHA512:
6
- metadata.gz: cfe8b07219b1869ce39d2e9795b98c0bf34af89547274e4e6e22c112ef47df6286be7c2098032584d9c40811280ecfc4ea139bbce4bc53a2a64415f0b8a6cebd
7
- data.tar.gz: f7ca6b8dc3cc39ae349a753190eb809f734c00334bf22c5a676addd5f0485cc2c822f5801be8180f22d5e08272da66368c7a2bf446eef2da67ea09bfb4767205
6
+ metadata.gz: 409bda7a9f3416b22acc330674aafdacba08b0fd1747f2c3f4896081fee9b7e203606d26dc33a556d61e90b8668b04d2224c639c2ff4c485398d772d5760a2b9
7
+ data.tar.gz: 6dc1d908fe0fb69c046120438bbe344d33d7238771a837fa8fec8fe6fbd7f738cec01ea5e29b783ade832447c27b0d18f36f82efea337810b260803b728682af
@@ -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
@@ -1,3 +1,11 @@
1
+ ### 0.2.0 / 2016-03-17
2
+
3
+ * Introduce arrays normalization
4
+
5
+ ### 0.1.1 / 2015-11-12
6
+
7
+ * New `parameterize` normalizer
8
+
1
9
  ### 0.1.0 / 2014-08-30
2
10
 
3
11
  * Add Mongoid integration
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  ## Normalizr
2
2
 
3
- [![Travic CI](http://img.shields.io/travis/dimko/normalizr.svg)](https://travis-ci.org/dimko/normalizr) [![Code Climate](http://img.shields.io/codeclimate/github/dimko/normalizr.svg)](https://codeclimate.com/github/dimko/normalizr) [![Coverage](http://img.shields.io/codeclimate/coverage/github/dimko/normalizr.svg)](https://codeclimate.com/github/dimko/normalizr)
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
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
@@ -27,13 +27,23 @@ module Normalizr
27
27
  end
28
28
  end
29
29
 
30
- def normalize(value, *normalizers)
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
- value = find(name).call(value, options)
44
+ obj = process(obj, name, options)
35
45
  end
36
- value
46
+ obj
37
47
  end
38
48
  end
39
49
 
@@ -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.pre)
15
- value = Normalizr.normalize(value, *options.post) if options.post.any?
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
@@ -8,11 +8,11 @@ module Normalizr
8
8
  @block = block
9
9
  end
10
10
 
11
- def pre
11
+ def before
12
12
  options_at(:with, :before) { block }
13
13
  end
14
14
 
15
- def post
15
+ def after
16
16
  options_at(:after)
17
17
  end
18
18
 
@@ -1,3 +1,3 @@
1
1
  module Normalizr
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -17,7 +17,7 @@ describe Normalizr do
17
17
  context 'not existed normalizer' do
18
18
  let(:normalizer) { :not_existed }
19
19
 
20
- it 'raise exception with normalizer in message' do
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
- context 'normalizers is empty' do
82
+
83
+ context 'normalizers are empty' do
39
84
  let(:normalizers) { [] }
40
- let(:value) { ' text ' }
41
85
 
42
- it 'use default normalizers' do
43
- expect(subject).to eq('text')
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 is not empty' do
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
- it 'normalize value' do
52
- expect(subject).to eq('Lorem.')
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
@@ -1,12 +1,15 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Book do
4
- it { should normalize_attribute(:author).from(' Michael Deering ').to('Michael Deering') }
5
- it { should normalize_attribute(:us_price).from('$3.50').to(3.50) }
6
- it { should normalize_attribute(:cnd_price).from('$3,450.98').to(3450.98) }
7
- it { should normalize_attribute(:summary).from(' Here is my summary that is a little to long ').to('Here is m...') }
8
-
9
- it { should normalize_attribute(:title).from('pick up chicks with magic tricks').to('Pick Up Chicks With Magic Tricks') }
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
@@ -5,4 +5,7 @@ class Book < ActiveRecord::Base
5
5
  normalize_attributes :title do |value|
6
6
  String === value ? value.titleize.strip : value
7
7
  end
8
+
9
+ serialize :tags, Array
10
+ normalize :tags
8
11
  end
@@ -22,6 +22,7 @@ ActiveRecord::Schema.define do
22
22
  t.decimal :us_price
23
23
  t.string :summary
24
24
  t.string :title
25
+ t.text :tags
25
26
  end
26
27
 
27
28
  create_table :journals, force: true do |t|
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.1.1
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: 2015-11-13 00:00:00.000000000 Z
11
+ date: 2016-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry