normalizr 0.2.0 → 0.3.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 +5 -5
- data/CHANGELOG.md +4 -0
- data/README.md +16 -1
- data/lib/normalizr/concern.rb +8 -2
- data/lib/normalizr/options_parser.rb +8 -0
- data/lib/normalizr/version.rb +1 -1
- data/spec/models/user_spec.rb +38 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/models/user.rb +16 -0
- data/spec/support/schema.rb +2 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: '068b8f6b2685d273ee884c7a8c0f751da02fd254fe56f0ee6366f304910f81f6'
|
4
|
+
data.tar.gz: 9edd590a85219a2ea44651412e32fced140dad016b3035acadcd1ebc43df2408
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb22312649da1951b0fe9d0d1214851ebeb62d408217156a0427a55b442814505e68786a513898e8ca8b095701cd47d4880e5c82ba943993b1516cbfd8b04774
|
7
|
+
data.tar.gz: 06c555da8af5e30f7527927518e28186577ac1baf2c36b0a0ae9ce3a4596c6cc15eb13c08f9a361e833e84665eb4ed769c9f3ff9d3d4f725db2d8cfa5eb82e37
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -74,7 +74,7 @@ Normalizr.configure do
|
|
74
74
|
add :indent do |value, amount = 2|
|
75
75
|
if String === value
|
76
76
|
value.indent(amount)
|
77
|
-
|
77
|
+
else
|
78
78
|
value
|
79
79
|
end
|
80
80
|
end
|
@@ -130,6 +130,21 @@ sms.message
|
|
130
130
|
#=> "It works"
|
131
131
|
```
|
132
132
|
|
133
|
+
You can also use if/unless options (they accept a symbol (method name) or proc):
|
134
|
+
|
135
|
+
```ruby
|
136
|
+
class Book
|
137
|
+
include Normalizr::Concerns
|
138
|
+
|
139
|
+
attr_accessor :author, :description, :date
|
140
|
+
|
141
|
+
normalize :author, if: :author_should_be_normalized?
|
142
|
+
normalize :description, unless: :description_should_not_be_normalized?
|
143
|
+
|
144
|
+
normalize :author, if: proc { date.today? }
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
133
148
|
Normalize values outside of class:
|
134
149
|
|
135
150
|
```ruby
|
data/lib/normalizr/concern.rb
CHANGED
@@ -11,8 +11,14 @@ module Normalizr
|
|
11
11
|
prepend Module.new {
|
12
12
|
options.attributes.each do |method|
|
13
13
|
define_method :"#{method}=" do |value|
|
14
|
-
|
15
|
-
|
14
|
+
positive = options.positive_condition.all? { |condition| instance_eval(&condition) }
|
15
|
+
negative = options.negative_condition.none? { |condition| instance_eval(&condition) }
|
16
|
+
|
17
|
+
if positive && negative
|
18
|
+
value = Normalizr.normalize(value, *options.before)
|
19
|
+
value = Normalizr.normalize(value, *options.after) if options.after.any?
|
20
|
+
end
|
21
|
+
|
16
22
|
super(value)
|
17
23
|
end
|
18
24
|
end
|
data/lib/normalizr/version.rb
CHANGED
data/spec/models/user_spec.rb
CHANGED
@@ -4,4 +4,42 @@ describe User do
|
|
4
4
|
context 'on default attribute with the default normalizer changed' do
|
5
5
|
it { should normalize_attribute(:firstname).from(' here ').to('here') }
|
6
6
|
end
|
7
|
+
|
8
|
+
context 'last name has to be normalized' do
|
9
|
+
before do
|
10
|
+
allow(subject).to receive(:should_normalize_lastname?).and_return(true)
|
11
|
+
end
|
12
|
+
|
13
|
+
it { is_expected.to normalize_attribute(:lastname).from(' here ').to('here') }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'last name cannot be normalized' do
|
17
|
+
before do
|
18
|
+
allow(subject).to receive(:should_normalize_lastname?).and_return(false)
|
19
|
+
end
|
20
|
+
|
21
|
+
it { is_expected.not_to normalize_attribute(:lastname).from(' here ').to('here') }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'maidenname has to be normalized' do
|
25
|
+
before do
|
26
|
+
allow(subject).to receive(:should_normalize_maidenname_positive?).and_return(true)
|
27
|
+
allow(subject).to receive(:should_normalize_maidenname_negative?).and_return(false)
|
28
|
+
end
|
29
|
+
|
30
|
+
it { is_expected.to normalize_attribute(:maidenname).from(' boba ').to('boba') }
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'maidenname cannot to be normalized' do
|
34
|
+
before do
|
35
|
+
allow(subject).to receive(:should_normalize_maidenname_positive?).and_return(true)
|
36
|
+
allow(subject).to receive(:should_normalize_maidenname_negative?).and_return(true)
|
37
|
+
end
|
38
|
+
|
39
|
+
it { is_expected.not_to normalize_attribute(:maidenname).from(' boba ').to('boba') }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'favouritebook has to be normalized with proc' do
|
43
|
+
it { is_expected.to normalize_attribute(:favouritebook).from(' Евангелие от Матфея ').to('Евангелие от Матфея') }
|
44
|
+
end
|
7
45
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/support/models/user.rb
CHANGED
@@ -1,3 +1,19 @@
|
|
1
1
|
class User < Person
|
2
2
|
normalize_attribute :firstname
|
3
|
+
|
4
|
+
normalize :lastname, if: :should_normalize_lastname?
|
5
|
+
normalize :maidenname, if: :should_normalize_maidenname_positive?, unless: :should_normalize_maidenname_negative?
|
6
|
+
normalize :favouritebook, if: proc { true }
|
7
|
+
|
8
|
+
def should_normalize_lastname?
|
9
|
+
true
|
10
|
+
end
|
11
|
+
|
12
|
+
def should_normalize_maidenname_positive?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def should_normalize_maidenname_negative?
|
17
|
+
false
|
18
|
+
end
|
3
19
|
end
|
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.3.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: 2018-03-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -213,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
213
213
|
version: '0'
|
214
214
|
requirements: []
|
215
215
|
rubyforge_project:
|
216
|
-
rubygems_version: 2.
|
216
|
+
rubygems_version: 2.7.6
|
217
217
|
signing_key:
|
218
218
|
specification_version: 4
|
219
219
|
summary: Writer methods parameters normalization
|