validated_object 2.0.0 → 2.0.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.
- checksums.yaml +4 -4
- data/README.md +10 -4
- data/lib/validated_object.rb +36 -10
- data/lib/validated_object/version.rb +1 -1
- data/script/demo.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ead35b1f631106e09d5ed245b18ced301b0fd22a642136d2198dc65f592feb72
|
4
|
+
data.tar.gz: 119b6594bedc0b63a11256ae7e40dccf67561cab4acccf6a67b7c3a08789642d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 371f7b30919720b846b2d0a8f1b065e011da484632f889cf98b21847f34b41d1b4ed0bf213662f6876bdbbd088faede79e054ed83284cc9d589bdc8dd4fc52a3
|
7
|
+
data.tar.gz: 8c9c3ea80f2924d23bd9297852b557f373a4bdcdb26785ef279730dbec5d3c372740be475f36652ece87884e6ec92bae13a5ea7b488e032ba684fdb29354ee09
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# ValidatedObject
|
4
4
|
|
5
|
-
Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.
|
5
|
+
Plain Old Ruby Objects + Rails Validations = self-checking Ruby objects.
|
6
6
|
|
7
7
|
|
8
8
|
## Goals
|
@@ -19,12 +19,14 @@ This is a small layer around
|
|
19
19
|
|
20
20
|
### Writing a self-validating object
|
21
21
|
|
22
|
-
All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
|
22
|
+
All of the [ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) are available, plus a new one, `TypeValidator`.
|
23
23
|
|
24
24
|
```ruby
|
25
25
|
class Dog < ValidatedObject::Base
|
26
|
+
# Plain old Ruby
|
26
27
|
attr_accessor :name, :birthday # attr_reader is supported as well for read-only attributes
|
27
28
|
|
29
|
+
# Plain old Rails
|
28
30
|
validates :name, presence: true
|
29
31
|
validates :birthday, type: Date, allow_nil: true # Strongly typed but optional
|
30
32
|
end
|
@@ -62,7 +64,7 @@ used to test an instance, plus the custom `check_validations!` convenience metho
|
|
62
64
|
```ruby
|
63
65
|
spot.birthday = '2015-01-23'
|
64
66
|
spot.valid? # => false
|
65
|
-
spot.check_validations! # => ArgumentError: Birthday is
|
67
|
+
spot.check_validations! # => ArgumentError: Birthday is a String, not a Date
|
66
68
|
```
|
67
69
|
|
68
70
|
Note the clear, explicit error message. These are great when reading a log
|
@@ -71,7 +73,7 @@ test it by making another attribute invalid:
|
|
71
73
|
|
72
74
|
```ruby
|
73
75
|
spot.name = nil
|
74
|
-
spot.check_validations! # => ArgumentError: Name can't be blank; Birthday is
|
76
|
+
spot.check_validations! # => ArgumentError: Name can't be blank; Birthday is a String, not a Date
|
75
77
|
```
|
76
78
|
|
77
79
|
|
@@ -95,6 +97,10 @@ The result is that `dogs` is an array of guaranteed valid Dog objects. And the
|
|
95
97
|
error log lists unparseable rows with good info for tracking down problems in
|
96
98
|
the data.
|
97
99
|
|
100
|
+
### Use in code generation
|
101
|
+
|
102
|
+
My [Schema.org microdata generation gem](https://github.com/dogweather/schema-dot-org) uses ValidatedObjects to recursively create well formed HTML / JSON-LD.
|
103
|
+
|
98
104
|
## Installation
|
99
105
|
|
100
106
|
Add this line to your application's Gemfile:
|
data/lib/validated_object.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_model'
|
2
4
|
require 'validated_object/version'
|
3
5
|
|
@@ -55,9 +57,7 @@ module ValidatedObject
|
|
55
57
|
def initialize(attributes=EMPTY_HASH)
|
56
58
|
raise ArgumentError, "#{attributes} is not a Hash" unless attributes.is_a?(Hash)
|
57
59
|
|
58
|
-
|
59
|
-
self.instance_variable_set "@#{key}".to_sym, attributes.fetch(key)
|
60
|
-
end
|
60
|
+
set_instance_variables from_hash: attributes
|
61
61
|
check_validations!
|
62
62
|
self
|
63
63
|
end
|
@@ -84,15 +84,41 @@ module ValidatedObject
|
|
84
84
|
def validate_each(record, attribute, value)
|
85
85
|
expected_class = options[:with]
|
86
86
|
|
87
|
-
if expected_class
|
88
|
-
|
89
|
-
else
|
90
|
-
return if value.is_a?(expected_class)
|
91
|
-
end
|
87
|
+
return if pseudo_boolean?(expected_class, value) ||
|
88
|
+
expected_class?(expected_class, value)
|
92
89
|
|
93
|
-
|
94
|
-
|
90
|
+
save_error(record, attribute, value, options)
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def pseudo_boolean?(expected_class, value)
|
97
|
+
expected_class == Boolean && boolean?(value)
|
98
|
+
end
|
99
|
+
|
100
|
+
def expected_class?(expected_class, value)
|
101
|
+
value.is_a?(expected_class)
|
102
|
+
end
|
103
|
+
|
104
|
+
def boolean?(value)
|
105
|
+
value.is_a?(TrueClass) || value.is_a?(FalseClass)
|
106
|
+
end
|
107
|
+
|
108
|
+
def save_error(record, attribute, value, options)
|
109
|
+
record.errors.add attribute,
|
110
|
+
options[:message] || "is a #{value.class}, not a #{options[:with]}"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
private
|
116
|
+
|
117
|
+
def set_instance_variables(from_hash:)
|
118
|
+
from_hash.each do |variable_name, variable_value|
|
119
|
+
self.instance_variable_set "@#{variable_name}".to_sym, variable_value
|
95
120
|
end
|
96
121
|
end
|
122
|
+
|
97
123
|
end
|
98
124
|
end
|
data/script/demo.rb
CHANGED
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: 2.0.
|
4
|
+
version: 2.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-02-
|
11
|
+
date: 2018-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|