normalizy 0.2.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/README.md +25 -55
- data/lib/normalizy/config.rb +3 -7
- data/lib/normalizy/extensions.rb +45 -49
- data/lib/normalizy/rspec/matcher.rb +14 -2
- data/lib/normalizy/version.rb +1 -1
- data/lib/normalizy.rb +1 -1
- data/spec/normalizy/config/alias_spec.rb +14 -42
- data/spec/normalizy/extensions/filters/date_spec.rb +4 -14
- data/spec/normalizy/extensions/filters/money_spec.rb +9 -19
- data/spec/normalizy/extensions/filters/number_spec.rb +2 -6
- data/spec/normalizy/extensions/filters/percent_spec.rb +9 -19
- data/spec/normalizy/extensions/filters/strip_spec.rb +8 -8
- data/spec/normalizy/extensions/model_spec.rb +107 -0
- data/spec/normalizy/extensions/normalizy_rules_spec.rb +132 -180
- data/spec/normalizy/filters/date_spec.rb +9 -9
- data/spec/normalizy/filters/money_spec.rb +42 -37
- data/spec/normalizy/rspec/matcher/description_spec.rb +8 -7
- data/spec/normalizy/rspec/matcher/failure_message_spec.rb +25 -47
- data/spec/normalizy/rspec/matcher/failure_message_when_negated_spec.rb +13 -15
- data/spec/normalizy/rspec/matcher/from_spec.rb +4 -3
- data/spec/normalizy/rspec/matcher/matches_spec.rb +117 -0
- data/spec/normalizy/rspec/matcher/to_spec.rb +4 -3
- data/spec/support/db/schema.rb +67 -9
- data/spec/support/filters/{blacklist_1.rb → blacklist.rb} +1 -1
- data/spec/support/filters/{blacklist_block.rb → block.rb} +1 -1
- data/spec/support/filters/info.rb +11 -0
- data/spec/support/models/alias.rb +7 -0
- data/spec/support/models/match.rb +9 -0
- data/spec/support/models/model.rb +28 -0
- data/spec/support/models/model_date.rb +7 -0
- data/spec/support/models/model_money.rb +10 -0
- data/spec/support/models/model_number.rb +5 -0
- data/spec/support/models/model_percent.rb +10 -0
- data/spec/support/models/model_strip.rb +8 -0
- data/spec/support/models/{clean.rb → rule.rb} +1 -1
- metadata +30 -20
- data/spec/normalizy/config/normalizy_raws_spec.rb +0 -9
- data/spec/normalizy/extensions/apply_normalizy_spec.rb +0 -171
- data/spec/normalizy/rspec/matcher/matchers_spec.rb +0 -97
- data/spec/normalizy/rspec/normalizy_spec.rb +0 -8
- data/spec/support/filters/blacklist_2.rb +0 -13
- data/spec/support/models/user.rb +0 -9
@@ -18,11 +18,12 @@ RSpec.describe Normalizy::Filters::Money do
|
|
18
18
|
it { expect(subject.call('1030.70')).to eq '1030.70' }
|
19
19
|
it { expect(subject.call('10300.70')).to eq '10300.70' }
|
20
20
|
|
21
|
-
it { expect(subject.call('
|
22
|
-
it { expect(subject.call('
|
23
|
-
it { expect(subject.call('
|
24
|
-
it { expect(subject.call('
|
25
|
-
it { expect(subject.call('
|
21
|
+
it { expect(subject.call('$ 0.01')).to eq '0.01' }
|
22
|
+
it { expect(subject.call('$ 1')).to eq '1.00' }
|
23
|
+
it { expect(subject.call('$ 1.70')).to eq '1.70' }
|
24
|
+
it { expect(subject.call('$ 103.70')).to eq '103.70' }
|
25
|
+
it { expect(subject.call('$ 1030.70')).to eq '1030.70' }
|
26
|
+
it { expect(subject.call('$ 10300.70')).to eq '10300.70' }
|
26
27
|
end
|
27
28
|
|
28
29
|
context 'with :cast' do
|
@@ -32,11 +33,11 @@ RSpec.describe Normalizy::Filters::Money do
|
|
32
33
|
it { expect(subject.call('1030.70' , cast: :to_f)).to be 1030.7 }
|
33
34
|
it { expect(subject.call('10300.70', cast: :to_f)).to be 10_300.7 }
|
34
35
|
|
35
|
-
it { expect(subject.call('
|
36
|
-
it { expect(subject.call('
|
37
|
-
it { expect(subject.call('
|
38
|
-
it { expect(subject.call('
|
39
|
-
it { expect(subject.call('
|
36
|
+
it { expect(subject.call('$ 1' , cast: :to_f)).to be 1.0 }
|
37
|
+
it { expect(subject.call('$ 1.70' , cast: :to_f)).to be 1.7 }
|
38
|
+
it { expect(subject.call('$ 103.70' , cast: :to_f)).to be 103.7 }
|
39
|
+
it { expect(subject.call('$ 1030.70' , cast: :to_f)).to be 1030.7 }
|
40
|
+
it { expect(subject.call('$ 10300.70', cast: :to_f)).to be 10_300.7 }
|
40
41
|
end
|
41
42
|
|
42
43
|
context 'when value has one decimal precision' do
|
@@ -45,10 +46,10 @@ RSpec.describe Normalizy::Filters::Money do
|
|
45
46
|
it { expect(subject.call('1030.7' , cast: :to_f)).to eq 1030.7 }
|
46
47
|
it { expect(subject.call('10300.7', cast: :to_f)).to eq 10_300.7 }
|
47
48
|
|
48
|
-
it { expect(subject.call('
|
49
|
-
it { expect(subject.call('
|
50
|
-
it { expect(subject.call('
|
51
|
-
it { expect(subject.call('
|
49
|
+
it { expect(subject.call('$ 1.7' , cast: :to_f)).to eq 1.7 }
|
50
|
+
it { expect(subject.call('$ 103.7' , cast: :to_f)).to eq 103.7 }
|
51
|
+
it { expect(subject.call('$ 1030.7' , cast: :to_f)).to eq 1030.7 }
|
52
|
+
it { expect(subject.call('$ 10300.7', cast: :to_f)).to eq 10_300.7 }
|
52
53
|
|
53
54
|
context 'and calls cast' do
|
54
55
|
it { expect(subject.call('1.7' , cast: :to_f)).to be 1.7 }
|
@@ -56,10 +57,10 @@ RSpec.describe Normalizy::Filters::Money do
|
|
56
57
|
it { expect(subject.call('1030.7' , cast: :to_f)).to be 1030.7 }
|
57
58
|
it { expect(subject.call('10300.7', cast: :to_f)).to be 10_300.7 }
|
58
59
|
|
59
|
-
it { expect(subject.call('
|
60
|
-
it { expect(subject.call('
|
61
|
-
it { expect(subject.call('
|
62
|
-
it { expect(subject.call('
|
60
|
+
it { expect(subject.call('$ 1.7' , cast: :to_f)).to be 1.7 }
|
61
|
+
it { expect(subject.call('$ 103.7' , cast: :to_f)).to be 103.7 }
|
62
|
+
it { expect(subject.call('$ 1030.7' , cast: :to_f)).to be 1030.7 }
|
63
|
+
it { expect(subject.call('$ 10300.7', cast: :to_f)).to be 10_300.7 }
|
63
64
|
end
|
64
65
|
end
|
65
66
|
|
@@ -78,17 +79,21 @@ RSpec.describe Normalizy::Filters::Money do
|
|
78
79
|
it { expect(subject.call(1030.70 , type: :cents)).to be 1030.70 }
|
79
80
|
it { expect(subject.call(10_300.70, type: :cents)).to be 10_300.70 }
|
80
81
|
|
82
|
+
context 'with different separator' do
|
83
|
+
it { expect(subject.call('1,7', separator: ',', type: :cents)).to eq '170' }
|
84
|
+
end
|
85
|
+
|
81
86
|
context 'with two decimal precision' do
|
82
87
|
it { expect(subject.call('1.70' , type: :cents)).to eq '170' }
|
83
88
|
it { expect(subject.call('103.70' , type: :cents)).to eq '10370' }
|
84
89
|
it { expect(subject.call('1030.70' , type: :cents)).to eq '103070' }
|
85
90
|
it { expect(subject.call('10300.70', type: :cents)).to eq '1030070' }
|
86
91
|
|
87
|
-
it { expect(subject.call('
|
88
|
-
it { expect(subject.call('
|
89
|
-
it { expect(subject.call('
|
90
|
-
it { expect(subject.call('
|
91
|
-
it { expect(subject.call('
|
92
|
+
it { expect(subject.call('$ 1' , type: :cents)).to eq '100' }
|
93
|
+
it { expect(subject.call('$ 1.70' , type: :cents)).to eq '170' }
|
94
|
+
it { expect(subject.call('$ 103.70' , type: :cents)).to eq '10370' }
|
95
|
+
it { expect(subject.call('$ 1030.70' , type: :cents)).to eq '103070' }
|
96
|
+
it { expect(subject.call('$ 10300.70', type: :cents)).to eq '1030070' }
|
92
97
|
|
93
98
|
context 'with :cast' do
|
94
99
|
it { expect(subject.call('1.70' , cast: :to_f, type: :cents)).to be 170.0 }
|
@@ -96,11 +101,11 @@ RSpec.describe Normalizy::Filters::Money do
|
|
96
101
|
it { expect(subject.call('1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
|
97
102
|
it { expect(subject.call('10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
|
98
103
|
|
99
|
-
it { expect(subject.call('
|
100
|
-
it { expect(subject.call('
|
101
|
-
it { expect(subject.call('
|
102
|
-
it { expect(subject.call('
|
103
|
-
it { expect(subject.call('
|
104
|
+
it { expect(subject.call('$ 1' , cast: :to_f, type: :cents)).to be 100.0 }
|
105
|
+
it { expect(subject.call('$ 1.70' , cast: :to_f, type: :cents)).to be 170.0 }
|
106
|
+
it { expect(subject.call('$ 103.70' , cast: :to_f, type: :cents)).to be 10_370.0 }
|
107
|
+
it { expect(subject.call('$ 1030.70' , cast: :to_f, type: :cents)).to be 103_070.0 }
|
108
|
+
it { expect(subject.call('$ 10300.70', cast: :to_f, type: :cents)).to be 1_030_070.0 }
|
104
109
|
end
|
105
110
|
end
|
106
111
|
|
@@ -110,10 +115,10 @@ RSpec.describe Normalizy::Filters::Money do
|
|
110
115
|
it { expect(subject.call('1030.7' , type: :cents)).to eq '103070' }
|
111
116
|
it { expect(subject.call('10300.7', type: :cents)).to eq '1030070' }
|
112
117
|
|
113
|
-
it { expect(subject.call('
|
114
|
-
it { expect(subject.call('
|
115
|
-
it { expect(subject.call('
|
116
|
-
it { expect(subject.call('
|
118
|
+
it { expect(subject.call('$ 1.7' , type: :cents)).to eq '170' }
|
119
|
+
it { expect(subject.call('$ 103.7' , type: :cents)).to eq '10370' }
|
120
|
+
it { expect(subject.call('$ 1030.7' , type: :cents)).to eq '103070' }
|
121
|
+
it { expect(subject.call('$ 10300.7', type: :cents)).to eq '1030070' }
|
117
122
|
|
118
123
|
context 'with :cast' do
|
119
124
|
it { expect(subject.call('1.7' , cast: :to_f, type: :cents)).to be 170.0 }
|
@@ -121,10 +126,10 @@ RSpec.describe Normalizy::Filters::Money do
|
|
121
126
|
it { expect(subject.call('1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
|
122
127
|
it { expect(subject.call('10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
|
123
128
|
|
124
|
-
it { expect(subject.call('
|
125
|
-
it { expect(subject.call('
|
126
|
-
it { expect(subject.call('
|
127
|
-
it { expect(subject.call('
|
129
|
+
it { expect(subject.call('$ 1.7' , cast: :to_f, type: :cents)).to be 170.0 }
|
130
|
+
it { expect(subject.call('$ 103.7' , cast: :to_f, type: :cents)).to be 10_370.0 }
|
131
|
+
it { expect(subject.call('$ 1030.7' , cast: :to_f, type: :cents)).to be 103_070.0 }
|
132
|
+
it { expect(subject.call('$ 10300.7', cast: :to_f, type: :cents)).to be 1_030_070.0 }
|
128
133
|
end
|
129
134
|
end
|
130
135
|
|
@@ -136,7 +141,7 @@ RSpec.describe Normalizy::Filters::Money do
|
|
136
141
|
|
137
142
|
context 'with :separator options' do
|
138
143
|
context 'provided via options rule' do
|
139
|
-
it { expect(subject.call('
|
144
|
+
it { expect(subject.call('R$ 0,01', separator: ',')).to eq '0.01' }
|
140
145
|
end
|
141
146
|
|
142
147
|
context 'provided I18n' do
|
@@ -5,21 +5,22 @@ require 'rails_helper'
|
|
5
5
|
RSpec.describe Normalizy::RSpec::Matcher, '.description' do
|
6
6
|
let!(:matcher) { described_class.new :name }
|
7
7
|
|
8
|
-
before do
|
9
|
-
matcher.from :from
|
10
|
-
matcher.to :to
|
11
|
-
end
|
12
|
-
|
13
8
|
context 'with no :with expectation' do
|
14
9
|
specify do
|
10
|
+
matcher.from :from
|
11
|
+
matcher.to :to
|
12
|
+
|
15
13
|
expect(matcher.description).to eq 'normalizy name from "from" to "to"'
|
16
14
|
end
|
17
15
|
end
|
18
16
|
|
19
17
|
context 'with :with expectation' do
|
20
|
-
before { matcher.with :blank }
|
21
|
-
|
22
18
|
specify do
|
19
|
+
matcher.with :blank
|
20
|
+
|
21
|
+
matcher.from :from
|
22
|
+
matcher.to :to
|
23
|
+
|
23
24
|
expect(matcher.description).to eq 'normalizy name with blank'
|
24
25
|
end
|
25
26
|
end
|
@@ -3,68 +3,46 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Normalizy::RSpec::Matcher, '.failure_message' do
|
6
|
-
let!(:
|
7
|
-
let!(:model) { User }
|
8
|
-
|
9
|
-
before do
|
10
|
-
model.normalizy_rules = {}
|
11
|
-
|
12
|
-
matcher.from :from
|
13
|
-
matcher.to :to
|
14
|
-
matcher.matches? model.new
|
15
|
-
end
|
6
|
+
let!(:model) { Match }
|
16
7
|
|
17
8
|
context 'with no :with expectation' do
|
18
9
|
specify do
|
10
|
+
matcher = described_class.new(:downcase_field)
|
11
|
+
|
12
|
+
matcher.from :from
|
13
|
+
matcher.to :to
|
14
|
+
matcher.matches? model.new
|
15
|
+
|
19
16
|
expect(matcher.failure_message).to eq %(expected: "to"\n got: "from")
|
20
17
|
end
|
21
18
|
end
|
22
19
|
|
23
|
-
context '
|
24
|
-
|
25
|
-
|
26
|
-
context 'and attribute has no :with rules' do
|
27
|
-
specify do
|
28
|
-
expect(matcher.failure_message).to eq %(expected: trim\n got: nil)
|
29
|
-
end
|
30
|
-
end
|
20
|
+
context 'with :with expectation' do
|
21
|
+
specify do
|
22
|
+
matcher = described_class.new(:alone)
|
31
23
|
|
32
|
-
|
33
|
-
|
34
|
-
model.normalizy_rules = {
|
35
|
-
name: [{ block: nil, options: {}, rules: :blank }]
|
36
|
-
}
|
37
|
-
end
|
24
|
+
matcher.with :missing
|
25
|
+
matcher.matches? model.new
|
38
26
|
|
39
|
-
|
40
|
-
expect(matcher.failure_message).to eq %(expected: trim\n got: blank)
|
41
|
-
end
|
27
|
+
expect(matcher.failure_message).to eq %(expected: missing\n got: nil)
|
42
28
|
end
|
43
29
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
end
|
30
|
+
specify do
|
31
|
+
matcher = described_class.new(:downcase_field_array)
|
32
|
+
|
33
|
+
matcher.with :missing
|
34
|
+
matcher.matches? model.new
|
50
35
|
|
51
|
-
|
52
|
-
expect(matcher.failure_message).to eq %(expected: trim\n got: blank)
|
53
|
-
end
|
36
|
+
expect(matcher.failure_message).to eq %(expected: missing\n got: downcase)
|
54
37
|
end
|
55
38
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
]
|
62
|
-
}
|
63
|
-
end
|
39
|
+
specify do
|
40
|
+
matcher = described_class.new(:trim_side_left)
|
41
|
+
|
42
|
+
matcher.with :missing
|
43
|
+
matcher.matches? model.new
|
64
44
|
|
65
|
-
|
66
|
-
expect(matcher.failure_message).to eq %(expected: trim\n got: {:trim=>{:side=>:left}})
|
67
|
-
end
|
45
|
+
expect(matcher.failure_message).to eq %(expected: missing\n got: {:trim=>{:side=>:left}})
|
68
46
|
end
|
69
47
|
end
|
70
48
|
end
|
@@ -3,30 +3,28 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Normalizy::RSpec::Matcher, '.failure_message_when_negated' do
|
6
|
-
let!(:
|
7
|
-
let!(:model) { User }
|
8
|
-
|
9
|
-
before do
|
10
|
-
matcher.from :from
|
11
|
-
matcher.to :to
|
12
|
-
matcher.matches? model.new
|
13
|
-
end
|
6
|
+
let!(:model) { Match }
|
14
7
|
|
15
8
|
context 'with no :with expectation' do
|
16
9
|
specify do
|
17
|
-
|
10
|
+
matcher = described_class.new(:downcase_field)
|
11
|
+
|
12
|
+
matcher.from 'from'
|
13
|
+
matcher.to 'from'
|
14
|
+
matcher.matches? model.new
|
15
|
+
|
16
|
+
expect(matcher.failure_message_when_negated).to eq %(expected: value != "from"\n got: "from")
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
20
|
context 'with :with expectation' do
|
22
|
-
|
23
|
-
|
21
|
+
specify do
|
22
|
+
matcher = described_class.new(:downcase_field)
|
24
23
|
|
25
|
-
matcher.with :
|
26
|
-
|
24
|
+
matcher.with :downcase
|
25
|
+
matcher.matches? model.new
|
27
26
|
|
28
|
-
|
29
|
-
expect(matcher.failure_message_when_negated).to eq %(expected: value != blank\n got: nil)
|
27
|
+
expect(matcher.failure_message_when_negated).to eq %(expected: value != downcase\n got: downcase)
|
30
28
|
end
|
31
29
|
end
|
32
30
|
end
|
@@ -3,16 +3,17 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Normalizy::RSpec::Matcher, '.from' do
|
6
|
-
let!(:matcher) { described_class.new :name }
|
7
|
-
let!(:model) { User }
|
8
|
-
|
9
6
|
it 'caches the value' do
|
7
|
+
matcher = described_class.new(:downcase_field)
|
8
|
+
|
10
9
|
matcher.from :from
|
11
10
|
|
12
11
|
expect(matcher.instance_variable_get(:@from)).to eq :from
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'returns it self' do
|
15
|
+
matcher = described_class.new(:downcase_field)
|
16
|
+
|
16
17
|
expect(matcher.from(:from)).to be matcher
|
17
18
|
end
|
18
19
|
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_helper'
|
4
|
+
|
5
|
+
RSpec.describe Normalizy::RSpec::Matcher, '.matches?' do
|
6
|
+
let!(:object) { Match.new }
|
7
|
+
|
8
|
+
specify do
|
9
|
+
matcher = described_class.new(:alone)
|
10
|
+
|
11
|
+
matcher.matches?(object)
|
12
|
+
|
13
|
+
expect(matcher.instance_variable_get(:@subject)).to eq object
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'when .with is called' do
|
17
|
+
specify do
|
18
|
+
matcher = described_class.new(:alone)
|
19
|
+
|
20
|
+
matcher.with :missing
|
21
|
+
|
22
|
+
expect(matcher.matches?(object)).to eq false
|
23
|
+
end
|
24
|
+
|
25
|
+
specify do
|
26
|
+
matcher = described_class.new(:downcase_field)
|
27
|
+
|
28
|
+
matcher.with :downcase
|
29
|
+
|
30
|
+
expect(matcher.matches?(object)).to eq true
|
31
|
+
end
|
32
|
+
|
33
|
+
specify do
|
34
|
+
matcher = described_class.new(:trim_side_left)
|
35
|
+
|
36
|
+
matcher.with trim: { side: :left }
|
37
|
+
|
38
|
+
expect(matcher.matches?(object)).to eq true
|
39
|
+
end
|
40
|
+
|
41
|
+
specify do
|
42
|
+
matcher = described_class.new(:trim_side_left_array)
|
43
|
+
|
44
|
+
matcher.with trim: { side: :left }
|
45
|
+
|
46
|
+
expect(matcher.matches?(object)).to eq true
|
47
|
+
end
|
48
|
+
|
49
|
+
specify do
|
50
|
+
Normalizy.configure do |config|
|
51
|
+
config.default_filters = :squish
|
52
|
+
end
|
53
|
+
|
54
|
+
matcher = described_class.new(:alone)
|
55
|
+
|
56
|
+
matcher.with :squish
|
57
|
+
|
58
|
+
expect(matcher.matches?(object)).to eq true
|
59
|
+
end
|
60
|
+
|
61
|
+
specify do
|
62
|
+
Normalizy.configure do |config|
|
63
|
+
config.default_filters = [:squish]
|
64
|
+
end
|
65
|
+
|
66
|
+
matcher = described_class.new(:alone)
|
67
|
+
|
68
|
+
matcher.with :squish
|
69
|
+
|
70
|
+
expect(matcher.matches?(object)).to eq true
|
71
|
+
end
|
72
|
+
|
73
|
+
specify do
|
74
|
+
Normalizy.configure do |config|
|
75
|
+
config.default_filters = [{ strip: { side: :left } }]
|
76
|
+
end
|
77
|
+
|
78
|
+
matcher = described_class.new(:alone)
|
79
|
+
|
80
|
+
matcher.with(strip: { side: :left })
|
81
|
+
|
82
|
+
expect(matcher.matches?(object)).to eq true
|
83
|
+
end
|
84
|
+
|
85
|
+
specify do
|
86
|
+
Normalizy.configure do |config|
|
87
|
+
config.default_filters = :squish
|
88
|
+
end
|
89
|
+
|
90
|
+
matcher = described_class.new(:downcase_field)
|
91
|
+
|
92
|
+
matcher.with :squish
|
93
|
+
|
94
|
+
expect(matcher.matches?(object)).to eq false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'when .with is not called' do
|
99
|
+
specify do
|
100
|
+
matcher = described_class.new(:alone)
|
101
|
+
|
102
|
+
matcher.from '1'
|
103
|
+
matcher.to '2'
|
104
|
+
|
105
|
+
expect(matcher.matches?(object)).to eq false
|
106
|
+
end
|
107
|
+
|
108
|
+
specify do
|
109
|
+
matcher = described_class.new(:downcase_field)
|
110
|
+
|
111
|
+
matcher.from 'BOTELHO'
|
112
|
+
matcher.to 'botelho'
|
113
|
+
|
114
|
+
expect(matcher.matches?(object)).to eq true
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -3,16 +3,17 @@
|
|
3
3
|
require 'rails_helper'
|
4
4
|
|
5
5
|
RSpec.describe Normalizy::RSpec::Matcher, '.to' do
|
6
|
-
let!(:matcher) { described_class.new :name }
|
7
|
-
let!(:model) { User }
|
8
|
-
|
9
6
|
it 'caches the value' do
|
7
|
+
matcher = described_class.new(:downcase)
|
8
|
+
|
10
9
|
matcher.to :to
|
11
10
|
|
12
11
|
expect(matcher.instance_variable_get(:@to)).to eq :to
|
13
12
|
end
|
14
13
|
|
15
14
|
it 'returns it self' do
|
15
|
+
matcher = described_class.new(:downcase)
|
16
|
+
|
16
17
|
expect(matcher.to(:to)).to be matcher
|
17
18
|
end
|
18
19
|
end
|
data/spec/support/db/schema.rb
CHANGED
@@ -3,16 +3,74 @@
|
|
3
3
|
ActiveRecord::Base.establish_connection adapter: :sqlite3, database: ':memory:'
|
4
4
|
|
5
5
|
ActiveRecord::Schema.define(version: 0) do
|
6
|
-
create_table :
|
7
|
-
t.string :
|
6
|
+
create_table :aliases do |t|
|
7
|
+
t.string :email
|
8
|
+
t.string :with_arg_field
|
9
|
+
t.string :with_inline_arg_field
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :matches do |t|
|
13
|
+
t.string :alone
|
14
|
+
t.string :downcase_field
|
15
|
+
t.string :trim_side_left
|
16
|
+
t.string :downcase_field_array
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :models do |t|
|
20
|
+
t.string :none
|
21
|
+
t.string :default
|
22
|
+
t.string :block
|
23
|
+
t.string :symbol
|
24
|
+
t.string :array_symbol
|
25
|
+
t.string :array_symbols
|
26
|
+
t.string :hash_no_args
|
27
|
+
t.string :hash_with_args
|
28
|
+
t.string :module_one_arg
|
29
|
+
t.string :module_two_args
|
30
|
+
t.string :module_and_block
|
31
|
+
t.string :module_and_block
|
32
|
+
t.string :method_with_no_options_field
|
33
|
+
t.string :method_with_options_field
|
34
|
+
t.string :native
|
35
|
+
t.string :multiple
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table :model_dates do |t|
|
39
|
+
t.datetime :date
|
40
|
+
t.datetime :date_format
|
41
|
+
t.datetime :date_time_zone
|
42
|
+
end
|
43
|
+
|
44
|
+
create_table :model_moneys do |t|
|
45
|
+
t.string :text
|
46
|
+
t.string :cents_type
|
47
|
+
t.integer :cast_to_i
|
48
|
+
t.decimal :cast_to_d
|
49
|
+
t.float :cents_type_and_cast_to_f
|
50
|
+
t.integer :cents_type_and_cast_to_i
|
51
|
+
end
|
52
|
+
|
53
|
+
create_table :model_numbers do |t|
|
54
|
+
t.string :number
|
55
|
+
end
|
56
|
+
|
57
|
+
create_table :model_percents do |t|
|
58
|
+
t.string :text
|
59
|
+
t.string :cents_type
|
60
|
+
t.integer :cast_to_i
|
61
|
+
t.decimal :cast_to_d
|
62
|
+
t.float :cents_type_and_cast_to_f
|
63
|
+
t.integer :cents_type_and_cast_to_i
|
64
|
+
end
|
65
|
+
|
66
|
+
create_table :model_strips do |t|
|
67
|
+
t.string :strip
|
68
|
+
t.string :strip_side_both
|
69
|
+
t.string :strip_side_left
|
70
|
+
t.string :strip_side_right
|
8
71
|
end
|
9
72
|
|
10
|
-
create_table :
|
11
|
-
t.
|
12
|
-
t.decimal :amount, precision: 16, scale: 10
|
13
|
-
t.integer :age
|
14
|
-
t.integer :amount_cents
|
15
|
-
t.string :amount_text
|
16
|
-
t.string :name
|
73
|
+
create_table :rules do |t|
|
74
|
+
t.string :name
|
17
75
|
end
|
18
76
|
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Match < ActiveRecord::Base
|
4
|
+
normalizy :alone
|
5
|
+
normalizy :downcase_field , with: :downcase
|
6
|
+
normalizy :trim_side_left , with: { trim: { side: :left } }
|
7
|
+
normalizy :trim_side_left_array, with: [{ trim: { side: :left } }]
|
8
|
+
normalizy :downcase_field_array, with: [:downcase]
|
9
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Model < ActiveRecord::Base
|
4
|
+
normalizy :default
|
5
|
+
|
6
|
+
normalizy :block , with: ->(value) { value.upcase }
|
7
|
+
normalizy :symbol , with: :squish
|
8
|
+
normalizy :array_symbol , with: [:squish]
|
9
|
+
normalizy :array_symbols , with: %i[downcase squish]
|
10
|
+
normalizy :hash_no_args , with: { squish: { ignored: true } }
|
11
|
+
normalizy :hash_with_args , with: { strip: { side: :left } }
|
12
|
+
normalizy :module_one_arg , with: Normalizy::Filters::Blacklist
|
13
|
+
normalizy :module_two_args , with: Normalizy::Filters::Info
|
14
|
+
normalizy :module_and_block , with: :blacklist, &->(value) { value.upcase }
|
15
|
+
normalizy :method_with_no_options_field, with: :method_with_no_options
|
16
|
+
normalizy :method_with_options_field , with: { method_with_options: { key: :value } }
|
17
|
+
normalizy :native , with: :split
|
18
|
+
normalizy :multiple , with: :downcase
|
19
|
+
normalizy :multiple , with: :titleize
|
20
|
+
|
21
|
+
def method_with_options(input, options = {})
|
22
|
+
[input, options].join ', '
|
23
|
+
end
|
24
|
+
|
25
|
+
def method_with_no_options(input)
|
26
|
+
input
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ModelMoney < ActiveRecord::Base
|
4
|
+
normalizy :text , with: :money
|
5
|
+
normalizy :cents_type , with: { money: { type: :cents } }
|
6
|
+
normalizy :cast_to_i , with: { money: { cast: :to_i } }
|
7
|
+
normalizy :cast_to_d , with: { money: { cast: :to_d } }
|
8
|
+
normalizy :cents_type_and_cast_to_f, with: { money: { cast: :to_f, type: :cents } }
|
9
|
+
normalizy :cents_type_and_cast_to_i, with: { money: { cast: :to_i, type: :cents } }
|
10
|
+
end
|