active_model_validators_ex 0.3.2 → 1.0.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
2
  SHA1:
3
- metadata.gz: a05737b152952959d082cacc7e93ddf422da9a9f
4
- data.tar.gz: ffb49f4614ca57efbd248df84c55dae753280efc
3
+ metadata.gz: e29da0db036abf2741a6ef1980deb0e38f45485c
4
+ data.tar.gz: 20e6022a990c499f061f682ab645f1dffb424534
5
5
  SHA512:
6
- metadata.gz: e66f36b5998c3b43cf40487fd130c67d8cd98e79295d8335b42ecb728559e9701f8a226dda075b07817ea2c1d09ab4f5a19d0c1a0c138db0f00f74e0e9fc711d
7
- data.tar.gz: 6dd4a3d772f9d09438f10305f019bb619b75f6a6e45d8fd6221c2e7bc3933ccf2fabe6597470a14dbd85225741afce36e8395b1dd13126c067b4e7987b22cf6a
6
+ metadata.gz: 108c4986eef5a72652d4386d3196b7099726594784476e6c00d148efca01d8336a743cc376481792e082838840d7bfc5e29bab24241b1a0645df552f56d3cf8d
7
+ data.tar.gz: 17cd7e8793b02a6e32291675531fb70c0bfad74c459e6eb707fff0de6d1b1a87d232c8c2ee7c4304a4e442af21bb2d6d2d578e5643b99608f5ffcdb608126d51
data/README.md CHANGED
@@ -52,9 +52,9 @@ validations:
52
52
  # defaults to false
53
53
  allow_nil: true,
54
54
 
55
- # can be either true or false, indicates if it accepts empty arrays
55
+ # can be either true or false, indicates if it accepts blank value
56
56
  # defaults to false
57
- allow_empty: true
57
+ allow_blank: true
58
58
  }
59
59
  end
60
60
 
@@ -67,7 +67,7 @@ validations:
67
67
  # returns true
68
68
  ExampleModel.new(array: []).valid?
69
69
 
70
- Used translation keys: array, empty, array_inclusion
70
+ Used translation keys: array, blank, array_inclusion
71
71
 
72
72
  * ArrayFormatValidator
73
73
 
@@ -84,9 +84,9 @@ validations:
84
84
  # defaults to false
85
85
  allow_nil: true,
86
86
 
87
- # can be either true or false, indicates if it accepts empty arrays
87
+ # can be either true or false, indicates if it accepts blank value
88
88
  # defaults to false
89
- allow_empty: true
89
+ allow_blank: true
90
90
  }
91
91
  end
92
92
 
@@ -96,7 +96,7 @@ validations:
96
96
  # return false
97
97
  ExampleModel.new(array: ['not', 'matching']).valid?
98
98
 
99
- Used translation keys: array, empty, array_format
99
+ Used translation keys: array, blank, array_format
100
100
 
101
101
  * TimeFormatValidator
102
102
 
@@ -1,21 +1,28 @@
1
1
  class ArrayValidatorBase < ActiveModel::EachValidator
2
2
  def initialize(options)
3
3
  options[:allow_nil] ||= false
4
- options[:allow_empty] ||= false
4
+ options[:allow_blank] ||= false
5
5
 
6
6
  super(options)
7
7
  end
8
8
 
9
9
  def validate_each(record, attribute, value)
10
- return if options[:allow_nil] && value.nil?
10
+ # TODO: extract this to a more generic class instad of ArrayValidatorBase
11
+ return if (options[:allow_blank] && value.blank?) ||
12
+ (options[:allow_nil] && value.nil?)
11
13
 
12
- unless value.is_a? Array
13
- record.errors.add(attribute, :array, options)
14
+ if !options[:allow_blank] && value.blank?
15
+ record.errors.add(attribute, :blank, options)
14
16
  return
15
17
  end
16
18
 
17
- if !options[:allow_empty] and value.empty?
18
- record.errors.add(attribute, :empty, options)
19
+ if !options[:allow_nil] && value.nil?
20
+ record.errors.add(attribute, :nil, options)
21
+ return
22
+ end
23
+
24
+ unless value.is_a? Array
25
+ record.errors.add(attribute, :array, options)
19
26
  return
20
27
  end
21
28
 
@@ -1,3 +1,3 @@
1
1
  module ActiveModelValidatorsEx
2
- VERSION = "0.3.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -20,7 +20,7 @@ describe ArrayValidatorBase do
20
20
  end
21
21
 
22
22
  shared_examples_for :common_behavior do
23
- context 'and value is a non nil, non array value' do
23
+ context 'value is a non nil, non array value' do
24
24
  let(:value) { :symbol }
25
25
 
26
26
  it 'sets error message in record' do
@@ -30,7 +30,7 @@ describe ArrayValidatorBase do
30
30
  end
31
31
 
32
32
  shared_examples_for :allow_nil_true do
33
- context 'and value is nil' do
33
+ context 'value is nil' do
34
34
  let(:value) { nil }
35
35
 
36
36
  it 'does not set error messages in record' do
@@ -40,7 +40,7 @@ describe ArrayValidatorBase do
40
40
  end
41
41
 
42
42
  shared_examples_for :allow_nil_false do
43
- context 'and value is nil' do
43
+ context 'value is nil' do
44
44
  let(:value) { nil }
45
45
 
46
46
  it 'sets error message in record' do
@@ -49,8 +49,8 @@ describe ArrayValidatorBase do
49
49
  end
50
50
  end
51
51
 
52
- shared_examples_for :allow_empty_true do
53
- context 'and value is an empty Array' do
52
+ shared_examples_for :allow_blank_true do
53
+ context 'value is an blank Array' do
54
54
  let(:value) { [] }
55
55
 
56
56
  it 'does not set error messages in record' do
@@ -59,8 +59,8 @@ describe ArrayValidatorBase do
59
59
  end
60
60
  end
61
61
 
62
- shared_examples_for :allow_empty_false do
63
- context 'and value is an empty Array' do
62
+ shared_examples_for :allow_blank_false do
63
+ context 'value is an blank Array' do
64
64
  let(:value) { [] }
65
65
 
66
66
  it 'set error messager in record' do
@@ -74,23 +74,22 @@ describe ArrayValidatorBase do
74
74
 
75
75
  it_behaves_like :common_behavior
76
76
  it_behaves_like :allow_nil_false
77
- it_behaves_like :allow_empty_false
77
+ it_behaves_like :allow_blank_false
78
78
  end
79
79
 
80
- context 'for instance initialized with allow_empty true' do
81
- let(:options) { { attributes: attribute, allow_empty: true } }
80
+ context 'for instance initialized with allow_blank true' do
81
+ let(:options) { { attributes: attribute, allow_blank: true } }
82
82
 
83
83
  it_behaves_like :common_behavior
84
- it_behaves_like :allow_nil_false
85
- it_behaves_like :allow_empty_true
84
+ it_behaves_like :allow_blank_true
86
85
  end
87
86
 
88
- context 'for instance initialized with allow_empty false' do
89
- let(:options) { { attributes: attribute, allow_empty: false } }
87
+ context 'for instance initialized with allow_blank false' do
88
+ let(:options) { { attributes: attribute, allow_blank: false } }
90
89
 
91
90
  it_behaves_like :common_behavior
91
+ it_behaves_like :allow_blank_false
92
92
  it_behaves_like :allow_nil_false
93
- it_behaves_like :allow_empty_false
94
93
  end
95
94
 
96
95
  context 'for instance initialized with allow_nil true' do
@@ -98,26 +97,26 @@ describe ArrayValidatorBase do
98
97
 
99
98
  it_behaves_like :common_behavior
100
99
  it_behaves_like :allow_nil_true
101
- it_behaves_like :allow_empty_false
100
+ it_behaves_like :allow_blank_false
102
101
 
103
- context 'and allow_empty true' do
102
+ context 'and allow_blank true' do
104
103
  let(:options) do
105
- { attributes: attribute, allow_nil: true, allow_empty: true }
104
+ { attributes: attribute, allow_nil: true, allow_blank: true }
106
105
  end
107
106
 
108
107
  it_behaves_like :common_behavior
109
108
  it_behaves_like :allow_nil_true
110
- it_behaves_like :allow_empty_true
109
+ it_behaves_like :allow_blank_true
111
110
  end
112
111
 
113
- context 'and allow_empty false' do
112
+ context 'and allow_blank false' do
114
113
  let(:options) do
115
- { attributes: attribute, allow_nil: true, allow_empty: false }
114
+ { attributes: attribute, allow_nil: true, allow_blank: false }
116
115
  end
117
116
 
118
117
  it_behaves_like :common_behavior
119
118
  it_behaves_like :allow_nil_true
120
- it_behaves_like :allow_empty_false
119
+ it_behaves_like :allow_blank_false
121
120
  end
122
121
  end
123
122
 
@@ -126,26 +125,25 @@ describe ArrayValidatorBase do
126
125
 
127
126
  it_behaves_like :common_behavior
128
127
  it_behaves_like :allow_nil_false
129
- it_behaves_like :allow_empty_false
128
+ it_behaves_like :allow_blank_false
130
129
 
131
- context 'and allow_empty true' do
130
+ context 'and allow_blank true' do
132
131
  let(:options) do
133
- { attributes: attribute, allow_nil: false, allow_empty: true }
132
+ { attributes: attribute, allow_nil: false, allow_blank: true }
134
133
  end
135
134
 
136
135
  it_behaves_like :common_behavior
137
- it_behaves_like :allow_nil_false
138
- it_behaves_like :allow_empty_true
136
+ it_behaves_like :allow_blank_true
139
137
  end
140
138
 
141
- context 'and allow_empty false' do
139
+ context 'and allow_blank false' do
142
140
  let(:options) do
143
- { attributes: attribute, allow_nil: false, allow_empty: false }
141
+ { attributes: attribute, allow_nil: false, allow_blank: false }
144
142
  end
145
143
 
146
144
  it_behaves_like :common_behavior
147
145
  it_behaves_like :allow_nil_false
148
- it_behaves_like :allow_empty_false
146
+ it_behaves_like :allow_blank_false
149
147
  end
150
148
  end
151
149
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_validators_ex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - junhanamaki
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-01 00:00:00.000000000 Z
11
+ date: 2014-11-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler