active_model_validators_ex 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +85 -0
- data/Rakefile +1 -0
- data/active_model_validators_ex.gemspec +28 -0
- data/lib/active_model_validators_ex/array_inclusion_validator.rb +28 -0
- data/lib/active_model_validators_ex/time_format_validator.rb +20 -0
- data/lib/active_model_validators_ex/version.rb +3 -0
- data/lib/active_model_validators_ex.rb +6 -0
- data/spec/active_model_validators_ex/array_inclusion_validator_spec.rb +229 -0
- data/spec/active_model_validators_ex/time_format_validator_spec.rb +212 -0
- data/spec/mock_objects/mock_record.rb +7 -0
- data/spec/spec_helper.rb +14 -0
- metadata +153 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 99b398cb38cb4e091c86e9e008d4ae262a2314ee
|
4
|
+
data.tar.gz: 4aeab77db7fbeb63a39fed7f205288d4368ede65
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 461948b5a4330f6ac7c88449c60dc931cc46c860b2239c4350044509640b176eb36c49e3382804c61ebe1bf02f11df5d0ee9a1519b82e84365c9cd9d1273d2e4
|
7
|
+
data.tar.gz: 9174f15424612cbb9cf2fa88ba4cea62a8a9b3546cf265f1259a475d6c8f5171ea0678a4c237447ed90b4135ad199fd9c1e2f601fde5b3c4789c84b91eea4191
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
active_model_validators_ex
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.1
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 junhanamaki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
# ActiveModelValidatorsEx
|
2
|
+
|
3
|
+
[![build](https://travis-ci.org/junhanamaki/active_model_validators_ex.svg?branch=master)](https://travis-ci.org/junhanamaki/active_model_validators_ex)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/junhanamaki/active_model_validators_ex/badges/gpa.svg)](https://codeclimate.com/github/junhanamaki/active_model_validators_ex)
|
5
|
+
[![Test Coverage](https://codeclimate.com/github/junhanamaki/active_model_validators_ex/badges/coverage.svg)](https://codeclimate.com/github/junhanamaki/active_model_validators_ex)
|
6
|
+
[![Dependency Status](https://gemnasium.com/junhanamaki/active_model_validators_ex.svg)](https://gemnasium.com/junhanamaki/active_model_validators_ex)
|
7
|
+
|
8
|
+
A library of validators for ActiveModel.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'active_model_validators_ex'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install active_model_validators_ex
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
After requiring, the following classes will be available for ActiveModel
|
27
|
+
validations:
|
28
|
+
|
29
|
+
* ArrayInclusionValidator
|
30
|
+
|
31
|
+
Allow you to validate if values in an array is included in a specified range
|
32
|
+
(meaning the value itself has to be an Array). As an example:
|
33
|
+
|
34
|
+
class ExampleModel
|
35
|
+
include Mongoid::Document
|
36
|
+
|
37
|
+
field :array
|
38
|
+
|
39
|
+
validates :array, array_inclusion: {
|
40
|
+
# the collection of valid values for array elements
|
41
|
+
in: [0, 1, 2], # it could also be written as a range: 0..2
|
42
|
+
|
43
|
+
# can be either true or false, indicates if nil is accepted
|
44
|
+
# defaults to false
|
45
|
+
allow_nil: true
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
# returns true
|
50
|
+
ExampleModel.new(array: [1, 2, 1]).valid?
|
51
|
+
|
52
|
+
# returns false
|
53
|
+
ExampleModel.new(array: [1, 2, 5]).valid?
|
54
|
+
|
55
|
+
* TimeFormatValidator
|
56
|
+
|
57
|
+
Allow you to check if given value is parsable to Time, example:
|
58
|
+
|
59
|
+
class ExampleModel < ActiveRecord::Base
|
60
|
+
attr_accessible :time
|
61
|
+
|
62
|
+
validates :time, time_format: {
|
63
|
+
# value can be either a lambda or a time, and indicates that
|
64
|
+
# attribute value must be after this value
|
65
|
+
after: lambda { Time.new(2014) },
|
66
|
+
|
67
|
+
# can be either true or false, indicates if nil is accepted
|
68
|
+
# defaults to false
|
69
|
+
allow_nil: true
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
# returns true
|
74
|
+
ExampleModel.new(time: Time.new(2015)).valid?
|
75
|
+
|
76
|
+
# returns false
|
77
|
+
ExampleModel.new(time: Time.new(2013)).valid?
|
78
|
+
|
79
|
+
## Contributing
|
80
|
+
|
81
|
+
1. Fork it ( https://github.com/junhanamaki/active_model_validators_ex/fork )
|
82
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
83
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
84
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
85
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_model_validators_ex/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_model_validators_ex"
|
8
|
+
spec.version = ActiveModelValidatorsEx::VERSION
|
9
|
+
spec.authors = ["junhanamaki"]
|
10
|
+
spec.email = ["jun.hanamaki@gmail.com"]
|
11
|
+
spec.summary = %q{Some custom validators for ActiveModel}
|
12
|
+
spec.description = %q{Extend ActiveModel with more validators for your
|
13
|
+
models.}
|
14
|
+
spec.homepage = "https://github.com/junhanamaki/active_model_validators_ex"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
18
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
19
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
|
+
spec.add_development_dependency 'simplecov', '~> 0.9'
|
25
|
+
spec.add_development_dependency 'codeclimate-test-reporter', '~> 0.4'
|
26
|
+
spec.add_development_dependency 'jazz_hands', '~> 0.5'
|
27
|
+
spec.add_development_dependency 'activemodel', '~> 4.1'
|
28
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class ArrayInclusionValidator < ActiveModel::EachValidator
|
2
|
+
def initialize(options)
|
3
|
+
unless options.key?(:in) &&
|
4
|
+
(options[:in].is_a?(Array) ||
|
5
|
+
options[:in].is_a?(Range))
|
6
|
+
raise 'key :in can not be nil, and value must be either an Array or Range'
|
7
|
+
end
|
8
|
+
|
9
|
+
super(options)
|
10
|
+
end
|
11
|
+
|
12
|
+
def validate_each(record, attribute, value)
|
13
|
+
return if value.nil? and options[:allow_nil]
|
14
|
+
|
15
|
+
unless value.is_a? Array
|
16
|
+
record.errors[attribute] <<
|
17
|
+
"expecting either Array or nil for attribute #{attribute}"
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
unless value.all? { |val| options[:in].include?(val) }
|
22
|
+
record.errors[attribute] <<
|
23
|
+
"attribute #{attribute} has be an array composed of values " \
|
24
|
+
" #{options[:in]}"
|
25
|
+
return
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class TimeFormatValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(record, attribute, value)
|
3
|
+
return if options[:allow_nil] && value.nil?
|
4
|
+
|
5
|
+
parsed_time = value.is_a?(Time) ? value : Time.parse(value.to_s)
|
6
|
+
|
7
|
+
previous_time = \
|
8
|
+
if options[:after].is_a?(Proc)
|
9
|
+
options[:after].call
|
10
|
+
elsif options[:after].is_a?(Time)
|
11
|
+
options[:after]
|
12
|
+
end
|
13
|
+
|
14
|
+
if !previous_time.nil? and parsed_time < previous_time
|
15
|
+
record.errors[attribute] << "invalid value, #{value} must be after #{previous_time}"
|
16
|
+
end
|
17
|
+
rescue StandardError => e
|
18
|
+
record.errors[attribute] << "invalid value, #{value} not valid for #{attribute}"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,229 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ArrayInclusionValidator do
|
4
|
+
describe '.new' do
|
5
|
+
context 'when key :in is not present in argument hash' do
|
6
|
+
let(:options) { { attributes: :something } }
|
7
|
+
|
8
|
+
it 'raises error' do
|
9
|
+
expect do
|
10
|
+
ArrayInclusionValidator.new(options)
|
11
|
+
end.to raise_error
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe '#validate_each' do
|
17
|
+
let(:record) { MockRecord.new(attribute) }
|
18
|
+
let(:attribute) { :array }
|
19
|
+
let(:validator) { ArrayInclusionValidator.new(options) }
|
20
|
+
before { validator.validate_each(record, attribute, value) }
|
21
|
+
|
22
|
+
context 'for instance initialized with options hash ' \
|
23
|
+
'key :in with empty array as value' do
|
24
|
+
let(:options) { { attributes: attribute, in: [] } }
|
25
|
+
|
26
|
+
context 'when passed value is nil' do
|
27
|
+
let(:value) { nil }
|
28
|
+
|
29
|
+
it 'sets error message in record, under passed attribute key' do
|
30
|
+
expect(record.errors[attribute].count).to eq(1)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when passed value is a non nil, non array value' do
|
35
|
+
let(:value) { :symbol }
|
36
|
+
|
37
|
+
it 'sets error message in record' do
|
38
|
+
expect(record.errors[attribute].count).to eq(1)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when passed value is an empty array' do
|
43
|
+
let(:value) { [] }
|
44
|
+
|
45
|
+
it 'does not set error messages in record' do
|
46
|
+
expect(record.errors[attribute].count).to eq(0)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when passed value is an array with values' do
|
51
|
+
let(:value) { [1, 2, 3] }
|
52
|
+
|
53
|
+
it 'sets error message in record, under passed attribute key' do
|
54
|
+
expect(record.errors[attribute].count).to eq(1)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'for instance initialized with options hash ' \
|
60
|
+
'key :in with empty array as value, ' \
|
61
|
+
'allow_nil as false' do
|
62
|
+
let(:options) { { attributes: attribute, in: [], allow_nil: false } }
|
63
|
+
|
64
|
+
context 'when passed value is nil' do
|
65
|
+
let(:value) { nil }
|
66
|
+
|
67
|
+
it 'sets error message in record, under passed attribute key' do
|
68
|
+
expect(record.errors[attribute].count).to eq(1)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when passed value is a non nil, non array value' do
|
73
|
+
let(:value) { :symbol }
|
74
|
+
|
75
|
+
it 'sets error message in record' do
|
76
|
+
expect(record.errors[attribute].count).to eq(1)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'when passed value is an empty array' do
|
81
|
+
let(:value) { [] }
|
82
|
+
|
83
|
+
it 'does not set error messages in record' do
|
84
|
+
expect(record.errors[attribute].count).to eq(0)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when passed value is an array with values' do
|
89
|
+
let(:value) { [1, 2, 3] }
|
90
|
+
|
91
|
+
it 'sets error message in record, under passed attribute key' do
|
92
|
+
expect(record.errors[attribute].count).to eq(1)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'for instance initialized with options hash ' \
|
98
|
+
'key :in with empty array as value, ' \
|
99
|
+
'allow_nil as true' do
|
100
|
+
let(:options) { { attributes: attribute, in: [], allow_nil: true } }
|
101
|
+
|
102
|
+
context 'when passed value is nil' do
|
103
|
+
let(:value) { nil }
|
104
|
+
|
105
|
+
it 'does not set error messages in record' do
|
106
|
+
expect(record.errors[attribute].count).to eq(0)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'when passed value is a non nil, non array value' do
|
111
|
+
let(:value) { :symbol }
|
112
|
+
|
113
|
+
it 'sets error message in record' do
|
114
|
+
expect(record.errors[attribute].count).to eq(1)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'when passed value is an empty array' do
|
119
|
+
let(:value) { [] }
|
120
|
+
|
121
|
+
it 'does not set error messages in record' do
|
122
|
+
expect(record.errors[attribute].count).to eq(0)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context 'when passed value is an array with values' do
|
127
|
+
let(:value) { [1, 2, 3] }
|
128
|
+
|
129
|
+
it 'sets error message in record, under passed attribute key' do
|
130
|
+
expect(record.errors[attribute].count).to eq(1)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'for instance initialized with options hash ' \
|
136
|
+
'key :in with array with values as value, ' \
|
137
|
+
'allow_nil as true' do
|
138
|
+
let(:in_array) { [1, 2, 3] }
|
139
|
+
let(:options) { { attributes: attribute, in: in_array, allow_nil: true } }
|
140
|
+
|
141
|
+
context 'when passed value is nil' do
|
142
|
+
let(:value) { nil }
|
143
|
+
|
144
|
+
it 'does not set error messages in record' do
|
145
|
+
expect(record.errors[attribute].count).to eq(0)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when passed value is a non nil, non array value' do
|
150
|
+
let(:value) { :symbol }
|
151
|
+
|
152
|
+
it 'sets error message in record' do
|
153
|
+
expect(record.errors[attribute].count).to eq(1)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when passed value is an empty array' do
|
158
|
+
let(:value) { [] }
|
159
|
+
|
160
|
+
it 'does not set error messages in record' do
|
161
|
+
expect(record.errors[attribute].count).to eq(0)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'when passed value is an array with values that are in options array' do
|
166
|
+
let(:value) { [1, 2, 3] }
|
167
|
+
|
168
|
+
it 'does not set error messages in record' do
|
169
|
+
expect(record.errors[attribute].count).to eq(0)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context 'when passed value is an array with values not in options array' do
|
174
|
+
let(:value) { [4, 5, 6] }
|
175
|
+
|
176
|
+
it 'sets error message in record' do
|
177
|
+
expect(record.errors[attribute].count).to eq(1)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'for instance initialized with options hash ' \
|
183
|
+
'key :in with range with values as value, ' \
|
184
|
+
'allow_nil as false' do
|
185
|
+
let(:in_range) { 1..3 }
|
186
|
+
let(:options) { { attributes: attribute, in: in_range, allow_nil: false } }
|
187
|
+
|
188
|
+
context 'when passed value is nil' do
|
189
|
+
let(:value) { nil }
|
190
|
+
|
191
|
+
it 'sets error message in record' do
|
192
|
+
expect(record.errors[attribute].count).to eq(1)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
context 'when passed value is a non nil, non array value' do
|
197
|
+
let(:value) { :symbol }
|
198
|
+
|
199
|
+
it 'sets error message in record' do
|
200
|
+
expect(record.errors[attribute].count).to eq(1)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
context 'when passed value is an empty array' do
|
205
|
+
let(:value) { [] }
|
206
|
+
|
207
|
+
it 'does not set error messages in record' do
|
208
|
+
expect(record.errors[attribute].count).to eq(0)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'when passed value is an array with values that are in options range' do
|
213
|
+
let(:value) { [1, 2, 3] }
|
214
|
+
|
215
|
+
it 'does not set error messages in record' do
|
216
|
+
expect(record.errors[attribute].count).to eq(0)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
context 'when passed value is an array with values not in options range' do
|
221
|
+
let(:value) { [4, 5, 6] }
|
222
|
+
|
223
|
+
it 'sets error message in record' do
|
224
|
+
expect(record.errors[attribute].count).to eq(1)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TimeFormatValidator do
|
4
|
+
describe '#validate_each' do
|
5
|
+
let(:record) { MockRecord.new(attribute) }
|
6
|
+
let(:attribute) { :time }
|
7
|
+
let(:validator) { TimeFormatValidator.new(options) }
|
8
|
+
before { validator.validate_each(record, attribute, value) }
|
9
|
+
|
10
|
+
context 'for instance initialized with no options' do
|
11
|
+
let(:options) { { attributes: attribute } }
|
12
|
+
|
13
|
+
context 'when value is nil' do
|
14
|
+
let(:value) { nil }
|
15
|
+
|
16
|
+
it 'sets error message in record' do
|
17
|
+
expect(record.errors[attribute].count).to eq(1)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'when value is non nil, nor Time, nor parsable to Time string' do
|
22
|
+
let(:value) { :symbol }
|
23
|
+
|
24
|
+
it 'sets error message in record' do
|
25
|
+
expect(record.errors[attribute].count).to eq(1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when value is an instance of Time' do
|
30
|
+
let(:value) { Time.now }
|
31
|
+
|
32
|
+
it 'does not set error messages in record' do
|
33
|
+
expect(record.errors[attribute].count).to eq(0)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when value is string parsable to Time' do
|
38
|
+
let(:value) { Time.now.to_s }
|
39
|
+
|
40
|
+
it 'does not set error messages in record' do
|
41
|
+
expect(record.errors[attribute].count).to eq(0)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'for instance initialized with ' \
|
47
|
+
'allow_nil as false' do
|
48
|
+
let(:options) { { attributes: attribute, allow_nil: false } }
|
49
|
+
|
50
|
+
context 'when value is nil' do
|
51
|
+
let(:value) { nil }
|
52
|
+
|
53
|
+
it 'sets error message in record' do
|
54
|
+
expect(record.errors[attribute].count).to eq(1)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'when value is non nil, nor Time, nor parsable to Time string' do
|
59
|
+
let(:value) { :symbol }
|
60
|
+
|
61
|
+
it 'sets error message in record' do
|
62
|
+
expect(record.errors[attribute].count).to eq(1)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when value is an instance of Time' do
|
67
|
+
let(:value) { Time.now }
|
68
|
+
|
69
|
+
it 'does not set error messages in record' do
|
70
|
+
expect(record.errors[attribute].count).to eq(0)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'when value is string parsable to Time' do
|
75
|
+
let(:value) { Time.now.to_s }
|
76
|
+
|
77
|
+
it 'does not set error messages in record' do
|
78
|
+
expect(record.errors[attribute].count).to eq(0)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'and after as Time' do
|
83
|
+
let(:after) { Time.now }
|
84
|
+
let(:options) do
|
85
|
+
{ attributes: attribute, allow_nil: false, after: after }
|
86
|
+
end
|
87
|
+
|
88
|
+
context 'when value is an instance of Time bellow after' do
|
89
|
+
let(:value) { Time.now - 24 * 60 * 60 }
|
90
|
+
|
91
|
+
it 'sets error message in record' do
|
92
|
+
expect(record.errors[attribute].count).to eq(1)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when value is an instance of Time after after' do
|
97
|
+
let(:value) { Time.now + 24 * 60 * 60 }
|
98
|
+
|
99
|
+
it 'does not set error message in record' do
|
100
|
+
expect(record.errors[attribute].count).to eq(0)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'and after is a Proc that returns Time' do
|
106
|
+
let(:after) { lambda { Time.now } }
|
107
|
+
let(:options) do
|
108
|
+
{ attributes: attribute, allow_nil: false, after: after }
|
109
|
+
end
|
110
|
+
|
111
|
+
context 'when value is an instance of Time bellow after' do
|
112
|
+
let(:value) { Time.now - 24 * 60 * 60 }
|
113
|
+
|
114
|
+
it 'sets error message in record' do
|
115
|
+
expect(record.errors[attribute].count).to eq(1)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when value is an instance of Time after after' do
|
120
|
+
let(:value) { Time.now + 24 * 60 * 60 }
|
121
|
+
|
122
|
+
it 'does not set error message in record' do
|
123
|
+
expect(record.errors[attribute].count).to eq(0)
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context 'for instance initialized with ' \
|
130
|
+
'allow_nil as true' do
|
131
|
+
let(:options) { { attributes: attribute, allow_nil: true } }
|
132
|
+
|
133
|
+
context 'when value is nil' do
|
134
|
+
let(:value) { nil }
|
135
|
+
|
136
|
+
it 'does not set error messages in record' do
|
137
|
+
expect(record.errors[attribute].count).to eq(0)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'when value is non nil, nor Time, nor parsable to Time string' do
|
142
|
+
let(:value) { :symbol }
|
143
|
+
|
144
|
+
it 'sets error message in record' do
|
145
|
+
expect(record.errors[attribute].count).to eq(1)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
context 'when value is an instance of Time' do
|
150
|
+
let(:value) { Time.now }
|
151
|
+
|
152
|
+
it 'does not set error messages in record' do
|
153
|
+
expect(record.errors[attribute].count).to eq(0)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
context 'when value is string parsable to Time' do
|
158
|
+
let(:value) { Time.now.to_s }
|
159
|
+
|
160
|
+
it 'does not set error messages in record' do
|
161
|
+
expect(record.errors[attribute].count).to eq(0)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context 'and after as Time' do
|
166
|
+
let(:after) { Time.now }
|
167
|
+
let(:options) do
|
168
|
+
{ attributes: attribute, allow_nil: true, after: after }
|
169
|
+
end
|
170
|
+
|
171
|
+
context 'when value is an instance of Time bellow after' do
|
172
|
+
let(:value) { Time.now - 24 * 60 * 60 }
|
173
|
+
|
174
|
+
it 'sets error message in record' do
|
175
|
+
expect(record.errors[attribute].count).to eq(1)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
context 'when value is an instance of Time after after' do
|
180
|
+
let(:value) { Time.now + 24 * 60 * 60 }
|
181
|
+
|
182
|
+
it 'does not set error message in record' do
|
183
|
+
expect(record.errors[attribute].count).to eq(0)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
context 'and after is a Proc that returns Time' do
|
189
|
+
let(:after) { lambda { Time.now } }
|
190
|
+
let(:options) do
|
191
|
+
{ attributes: attribute, allow_nil: true, after: after }
|
192
|
+
end
|
193
|
+
|
194
|
+
context 'when value is an instance of Time bellow after' do
|
195
|
+
let(:value) { Time.now - 24 * 60 * 60 }
|
196
|
+
|
197
|
+
it 'sets error message in record' do
|
198
|
+
expect(record.errors[attribute].count).to eq(1)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'when value is an instance of Time after after' do
|
203
|
+
let(:value) { Time.now + 24 * 60 * 60 }
|
204
|
+
|
205
|
+
it 'does not set error message in record' do
|
206
|
+
expect(record.errors[attribute].count).to eq(0)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
if ENV['CODECLIMATE_REPO_TOKEN'].nil?
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
coverage_dir 'tmp/coverage'
|
5
|
+
end
|
6
|
+
else
|
7
|
+
require "codeclimate-test-reporter"
|
8
|
+
CodeClimate::TestReporter.start
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'pry'
|
12
|
+
require 'active_model'
|
13
|
+
require 'active_model_validators_ex'
|
14
|
+
require 'mock_objects/mock_record'
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_validators_ex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- junhanamaki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: simplecov
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: codeclimate-test-reporter
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: jazz_hands
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activemodel
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '4.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '4.1'
|
97
|
+
description: |-
|
98
|
+
Extend ActiveModel with more validators for your
|
99
|
+
models.
|
100
|
+
email:
|
101
|
+
- jun.hanamaki@gmail.com
|
102
|
+
executables: []
|
103
|
+
extensions: []
|
104
|
+
extra_rdoc_files: []
|
105
|
+
files:
|
106
|
+
- ".gitignore"
|
107
|
+
- ".rspec"
|
108
|
+
- ".ruby-gemset"
|
109
|
+
- ".ruby-version"
|
110
|
+
- ".travis.yml"
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- active_model_validators_ex.gemspec
|
116
|
+
- lib/active_model_validators_ex.rb
|
117
|
+
- lib/active_model_validators_ex/array_inclusion_validator.rb
|
118
|
+
- lib/active_model_validators_ex/time_format_validator.rb
|
119
|
+
- lib/active_model_validators_ex/version.rb
|
120
|
+
- spec/active_model_validators_ex/array_inclusion_validator_spec.rb
|
121
|
+
- spec/active_model_validators_ex/time_format_validator_spec.rb
|
122
|
+
- spec/mock_objects/mock_record.rb
|
123
|
+
- spec/spec_helper.rb
|
124
|
+
homepage: https://github.com/junhanamaki/active_model_validators_ex
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.2.2
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Some custom validators for ActiveModel
|
148
|
+
test_files:
|
149
|
+
- spec/active_model_validators_ex/array_inclusion_validator_spec.rb
|
150
|
+
- spec/active_model_validators_ex/time_format_validator_spec.rb
|
151
|
+
- spec/mock_objects/mock_record.rb
|
152
|
+
- spec/spec_helper.rb
|
153
|
+
has_rdoc:
|