simple_range_validator 0.2.0 → 0.2.1
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.
- data/README.md +14 -0
- data/VERSION +1 -1
- data/lib/array_validator.rb +5 -1
- data/lib/simple_range_validator.rb +6 -1
- data/lib/timespan_validator.rb +29 -0
- data/simple_range_validator.gemspec +3 -2
- metadata +4 -3
data/README.md
CHANGED
@@ -70,6 +70,20 @@ class Search
|
|
70
70
|
end
|
71
71
|
```
|
72
72
|
|
73
|
+
## TimeSpan Validator
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class Search
|
77
|
+
include Mongoid::Document
|
78
|
+
|
79
|
+
field :period, type: Timespan
|
80
|
+
|
81
|
+
# you can also use in: as the option
|
82
|
+
validates :period, timespan: {from: 1.day.ago, to: 14.days.from_now }
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
|
73
87
|
Simple as pie ;)
|
74
88
|
|
75
89
|
## Contributing to range_validator
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.1
|
data/lib/array_validator.rb
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
require 'array_types_validator'
|
2
2
|
|
3
3
|
class ArrayValidator < ActiveModel::EachValidator
|
4
|
+
attr_reader :attrib
|
5
|
+
|
4
6
|
def validate_each(record, attribute, value)
|
7
|
+
@attrib = attribute
|
5
8
|
record.errors[attribute] << "must be in: #{show_list}" unless contains? value
|
6
9
|
end
|
7
10
|
|
8
11
|
protected
|
9
12
|
|
10
13
|
def contains? value
|
11
|
-
|
14
|
+
return true if value.nil?
|
15
|
+
raise ArgumentError, "#{attrib} must be an Array, was: #{value}" unless value.kind_of?(Array)
|
12
16
|
(value - list).empty?
|
13
17
|
end
|
14
18
|
|
@@ -1,14 +1,19 @@
|
|
1
1
|
require 'array_validator'
|
2
|
+
require 'timespan_validator'
|
2
3
|
|
3
4
|
class RangeValidator < ActiveModel::EachValidator
|
5
|
+
attr_reader :attrib
|
6
|
+
|
4
7
|
def validate_each(record, attribute, value)
|
8
|
+
@attrib = attribute
|
5
9
|
record.errors[attribute] << "must be within: #{show_range}" unless within? value
|
6
10
|
end
|
7
11
|
|
8
12
|
protected
|
9
13
|
|
10
14
|
def within? value
|
11
|
-
|
15
|
+
return true if value.nil?
|
16
|
+
raise ArgumentError, "#{attrib} must be a Range, was: #{value}" unless value.kind_of?(Range)
|
12
17
|
range.cover?(value.min) && range.cover?(value.max)
|
13
18
|
end
|
14
19
|
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class TimespanValidator < ActiveModel::EachValidator
|
2
|
+
attr_reader :attrib
|
3
|
+
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
@attrib = attribute
|
6
|
+
record.errors[attribute] << "must be within: #{show_range}" unless within? value
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
def within? value
|
12
|
+
return true if value.nil?
|
13
|
+
raise ArgumentError, "#{attrib} must be a Timespan, was: #{value}" unless value.kind_of?(Timespan)
|
14
|
+
# puts "value: #{value} between? #{from} and #{to}"
|
15
|
+
value.between?(from, to)
|
16
|
+
end
|
17
|
+
|
18
|
+
def from
|
19
|
+
options[:from]
|
20
|
+
end
|
21
|
+
|
22
|
+
def to
|
23
|
+
options[:to]
|
24
|
+
end
|
25
|
+
|
26
|
+
def show_range
|
27
|
+
"#{from} - #{to}"
|
28
|
+
end
|
29
|
+
end
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "simple_range_validator"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date = "2012-11-
|
12
|
+
s.date = "2012-11-21"
|
13
13
|
s.description = "Range validator for any Range model fields"
|
14
14
|
s.email = "kmandrup@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
|
|
28
28
|
"lib/array_types_validator.rb",
|
29
29
|
"lib/array_validator.rb",
|
30
30
|
"lib/simple_range_validator.rb",
|
31
|
+
"lib/timespan_validator.rb",
|
31
32
|
"simple_range_validator.gemspec",
|
32
33
|
"spec/array_types_validator_spec.rb",
|
33
34
|
"spec/array_validator_spec.rb",
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: simple_range_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
12
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -126,6 +126,7 @@ files:
|
|
126
126
|
- lib/array_types_validator.rb
|
127
127
|
- lib/array_validator.rb
|
128
128
|
- lib/simple_range_validator.rb
|
129
|
+
- lib/timespan_validator.rb
|
129
130
|
- simple_range_validator.gemspec
|
130
131
|
- spec/array_types_validator_spec.rb
|
131
132
|
- spec/array_validator_spec.rb
|
@@ -146,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
146
147
|
version: '0'
|
147
148
|
segments:
|
148
149
|
- 0
|
149
|
-
hash:
|
150
|
+
hash: 3665912120419951902
|
150
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
152
|
none: false
|
152
153
|
requirements:
|