validated_object 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +2 -0
- data/lib/validated_object.rb +16 -7
- data/lib/validated_object/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97d91eb8801c44a0daecab2f6bf5e66c1dc34908
|
4
|
+
data.tar.gz: b53ae1848ed9176217969907ff95d4ceb6f0d06b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7df6a6cacb4a691183fe606828b572a74e08b139d136e8e6826e5fd2ff3f441443b2f4ef451da96a65040075a46ecfee7e123da9f1ed911308c26b586e0482fe
|
7
|
+
data.tar.gz: 86ff719a5c3f23f76f843c049a1071b5ff79e6b20953ca482a3cedf33904adde167553d6efce284e067a0f69bb3841534f1bceb2d29a6bd01ed1dacd77bd8931
|
data/HISTORY.md
ADDED
data/lib/validated_object.rb
CHANGED
@@ -42,6 +42,10 @@ module ValidatedObject
|
|
42
42
|
class Base
|
43
43
|
include ActiveModel::Validations
|
44
44
|
|
45
|
+
# Implements a pseudo-boolean class.
|
46
|
+
class Boolean
|
47
|
+
end
|
48
|
+
|
45
49
|
# Instantiate and validate a new object.
|
46
50
|
#
|
47
51
|
# @yieldparam [ValidatedObject] new_object the yielded new object
|
@@ -63,22 +67,27 @@ module ValidatedObject
|
|
63
67
|
end
|
64
68
|
|
65
69
|
# A custom validator which ensures an object is an instance of a class
|
66
|
-
# or a subclass.
|
67
|
-
#
|
68
|
-
# access by subclasses of {ValidatedObject::Base}.
|
70
|
+
# or a subclass. It supports a pseudo-boolean class for convenient
|
71
|
+
# validation. (Ruby doesn't have a built-in Boolean.)
|
69
72
|
#
|
70
73
|
# @example Ensure that weight is a number
|
71
74
|
# class Dog < ValidatedObject::Base
|
72
|
-
# attr_accessor :weight
|
75
|
+
# attr_accessor :weight, :neutered
|
73
76
|
# validates :weight, type: Numeric
|
77
|
+
# validates :neutered, type: Boolean
|
74
78
|
# end
|
75
79
|
class TypeValidator < ActiveModel::EachValidator
|
76
80
|
# @return [nil]
|
77
81
|
def validate_each(record, attribute, value)
|
78
|
-
|
79
|
-
|
82
|
+
expected_class = options[:with]
|
83
|
+
|
84
|
+
if expected_class == Boolean
|
85
|
+
return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
86
|
+
else
|
87
|
+
return if value.is_a?(expected_class)
|
88
|
+
end
|
80
89
|
|
81
|
-
msg = options[:message] || "is class #{value.class}, not #{
|
90
|
+
msg = options[:message] || "is class #{value.class}, not #{expected_class}"
|
82
91
|
record.errors.add attribute, msg
|
83
92
|
end
|
84
93
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validated_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-04-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- ".rspec"
|
78
78
|
- ".travis.yml"
|
79
79
|
- Gemfile
|
80
|
+
- HISTORY.md
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|
82
83
|
- Rakefile
|
@@ -111,4 +112,3 @@ signing_key:
|
|
111
112
|
specification_version: 4
|
112
113
|
summary: Self-validating plain Ruby objects.
|
113
114
|
test_files: []
|
114
|
-
has_rdoc:
|