normalizr 0.2.0 → 0.3.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
- SHA1:
3
- metadata.gz: c92e29d4c962eab7b59f45d08e1491ab5198071b
4
- data.tar.gz: b36c1555e4e4680972c64696d85a00a4117ce73b
2
+ SHA256:
3
+ metadata.gz: '068b8f6b2685d273ee884c7a8c0f751da02fd254fe56f0ee6366f304910f81f6'
4
+ data.tar.gz: 9edd590a85219a2ea44651412e32fced140dad016b3035acadcd1ebc43df2408
5
5
  SHA512:
6
- metadata.gz: 409bda7a9f3416b22acc330674aafdacba08b0fd1747f2c3f4896081fee9b7e203606d26dc33a556d61e90b8668b04d2224c639c2ff4c485398d772d5760a2b9
7
- data.tar.gz: 6dc1d908fe0fb69c046120438bbe344d33d7238771a837fa8fec8fe6fbd7f738cec01ea5e29b783ade832447c27b0d18f36f82efea337810b260803b728682af
6
+ metadata.gz: eb22312649da1951b0fe9d0d1214851ebeb62d408217156a0427a55b442814505e68786a513898e8ca8b095701cd47d4880e5c82ba943993b1516cbfd8b04774
7
+ data.tar.gz: 06c555da8af5e30f7527927518e28186577ac1baf2c36b0a0ae9ce3a4596c6cc15eb13c08f9a361e833e84665eb4ed769c9f3ff9d3d4f725db2d8cfa5eb82e37
@@ -1,3 +1,7 @@
1
+ ### 0.3.0 / 2018-03-21
2
+
3
+ * Add a possibility to use if/unless cases for normalizer options (@TheTharin)
4
+
1
5
  ### 0.2.0 / 2016-03-17
2
6
 
3
7
  * Introduce arrays normalization
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
- end
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
@@ -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
- value = Normalizr.normalize(value, *options.before)
15
- value = Normalizr.normalize(value, *options.after) if options.after.any?
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
@@ -16,6 +16,14 @@ module Normalizr
16
16
  options_at(:after)
17
17
  end
18
18
 
19
+ def positive_condition
20
+ options_at(:if)
21
+ end
22
+
23
+ def negative_condition
24
+ options_at(:unless)
25
+ end
26
+
19
27
  private
20
28
 
21
29
  def options_at(*keys)
@@ -1,3 +1,3 @@
1
1
  module Normalizr
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -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
@@ -1,4 +1,4 @@
1
- require 'support/codeclimate'
1
+ require 'simplecov'
2
2
  require 'pry'
3
3
  require 'rspec'
4
4
  require 'rspec/its'
@@ -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
@@ -38,5 +38,7 @@ ActiveRecord::Schema.define do
38
38
  create_table :users, force: true do |t|
39
39
  t.string :firstname
40
40
  t.string :lastname
41
+ t.string :maidenname
42
+ t.string :favouritebook
41
43
  end
42
44
  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.2.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: 2016-03-18 00:00:00.000000000 Z
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.4.8
216
+ rubygems_version: 2.7.6
217
217
  signing_key:
218
218
  specification_version: 4
219
219
  summary: Writer methods parameters normalization