validated_object 2.0.2 → 2.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -3
- data/lib/validated_object.rb +11 -3
- data/lib/validated_object/version.rb +1 -1
- data/validated_object.gemspec +6 -6
- metadata +12 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c1aef88260ebc19d28fa8bd90a400ec6d05c45a60fea1632ebb1b81bbe2b456
|
4
|
+
data.tar.gz: 239216afd24adc90bb9ae4641253443a91d303e312f49b7bc6f348c541563f5f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 032c79475b1a99e990f4b04da2cda94cd5cca556cb38e385a783300ef675bfcd2ed51cc055d653b86f77b70270de87518cd7296b1c754a108f11c70d4e989c86
|
7
|
+
data.tar.gz: affdd5eb1d8dd2525c6b0fd7730fb907317ce0ad3bf8f105b42d67c66aba69e3389c65f7eae85dbfc67ed273e242761a34c67210153f4d9cbaaa694a3e6cac57
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/validated_object.svg)](https://badge.fury.io/rb/validated_object) [![Build Status](https://travis-ci.org/
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/validated_object.svg)](https://badge.fury.io/rb/validated_object) [![Build Status](https://travis-ci.org/public-law/validated_object.svg?branch=master)](https://travis-ci.org/public-law/validated_object) [![Code Climate](https://codeclimate.com/github/dogweather/validated_object/badges/gpa.svg)](https://codeclimate.com/github/dogweather/validated_object)
|
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
|
@@ -28,11 +28,13 @@ class Dog < ValidatedObject::Base
|
|
28
28
|
|
29
29
|
# Plain old Rails
|
30
30
|
validates :name, presence: true
|
31
|
+
|
32
|
+
# A new type-validation if you'd like to use it
|
31
33
|
validates :birthday, type: Date, allow_nil: true # Strongly typed but optional
|
32
34
|
end
|
33
35
|
```
|
34
36
|
|
35
|
-
The `TypeValidator` is what enables `type: Date`, above. All classes can be checked, as well as a pseudo-class `Boolean`. E.g.:
|
37
|
+
The included `TypeValidator` is what enables `type: Date`, above. All classes can be checked, as well as a pseudo-class `Boolean`. E.g.:
|
36
38
|
|
37
39
|
```ruby
|
38
40
|
#...
|
data/lib/validated_object.rb
CHANGED
@@ -7,7 +7,7 @@ module ValidatedObject
|
|
7
7
|
# @abstract Subclass and add `attr_accessor` and validations
|
8
8
|
# to create custom validating objects.
|
9
9
|
#
|
10
|
-
# Uses
|
10
|
+
# Uses {http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates ActiveModel::Validations}
|
11
11
|
# to create self-validating Plain Old Ruby objects. This is especially
|
12
12
|
# useful when importing data from one system into another. This class also
|
13
13
|
# creates very readable error messages.
|
@@ -44,7 +44,11 @@ module ValidatedObject
|
|
44
44
|
|
45
45
|
EMPTY_HASH = {}.freeze
|
46
46
|
|
47
|
-
#
|
47
|
+
# A private class definition, not intended to
|
48
|
+
# be used directly. Implements a pseudo-boolean class
|
49
|
+
# enabling validations like this:
|
50
|
+
#
|
51
|
+
# validates :enabled, type: Boolean
|
48
52
|
class Boolean
|
49
53
|
end
|
50
54
|
|
@@ -63,7 +67,9 @@ module ValidatedObject
|
|
63
67
|
end
|
64
68
|
|
65
69
|
# Run any validations and raise an error if invalid.
|
70
|
+
#
|
66
71
|
# @raise [ArgumentError] if any validations fail.
|
72
|
+
# @return [ValidatedObject::Base] the receiver
|
67
73
|
def check_validations!
|
68
74
|
raise ArgumentError, errors.full_messages.join('; ') if invalid?
|
69
75
|
self
|
@@ -73,6 +79,8 @@ module ValidatedObject
|
|
73
79
|
# or a subclass. It supports a pseudo-boolean class for convenient
|
74
80
|
# validation. (Ruby doesn't have a built-in Boolean.)
|
75
81
|
#
|
82
|
+
# Automatically used in a `type` validation:
|
83
|
+
#
|
76
84
|
# @example Ensure that weight is a number
|
77
85
|
# class Dog < ValidatedObject::Base
|
78
86
|
# attr_accessor :weight, :neutered
|
@@ -106,7 +114,7 @@ module ValidatedObject
|
|
106
114
|
end
|
107
115
|
|
108
116
|
def save_error(record, attribute, value, options)
|
109
|
-
record.errors.add attribute,
|
117
|
+
record.errors.add attribute,
|
110
118
|
options[:message] || "is a #{value.class}, not a #{options[:with]}"
|
111
119
|
end
|
112
120
|
end
|
data/validated_object.gemspec
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
#
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
3
4
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
5
|
require 'validated_object/version'
|
5
6
|
|
@@ -7,11 +8,11 @@ Gem::Specification.new do |spec|
|
|
7
8
|
spec.name = 'validated_object'
|
8
9
|
spec.version = ValidatedObject::VERSION
|
9
10
|
spec.authors = ['Robb Shecter']
|
10
|
-
spec.email = ['robb@
|
11
|
+
spec.email = ['robb@public.law']
|
11
12
|
|
12
13
|
spec.summary = 'Self-validating plain Ruby objects.'
|
13
14
|
spec.description = 'A small wrapper around ActiveModel Validations.'
|
14
|
-
spec.homepage = 'https://github.com/
|
15
|
+
spec.homepage = 'https://github.com/public-law/validated_object'
|
15
16
|
spec.license = 'MIT'
|
16
17
|
|
17
18
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
@@ -27,8 +28,7 @@ Gem::Specification.new do |spec|
|
|
27
28
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
29
|
spec.require_paths = ['lib']
|
29
30
|
|
30
|
-
spec.add_development_dependency '
|
31
|
-
spec.add_development_dependency 'rake', '~> 10.0'
|
31
|
+
spec.add_development_dependency 'rake', '>= 12.3.3'
|
32
32
|
spec.add_development_dependency 'rspec'
|
33
33
|
|
34
34
|
spec.add_runtime_dependency 'activemodel', '>= 3.2.21'
|
metadata
CHANGED
@@ -1,43 +1,29 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-08-23 00:00:00.000000000 Z
|
12
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.10'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '1.10'
|
27
13
|
- !ruby/object:Gem::Dependency
|
28
14
|
name: rake
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
|
-
- - "
|
17
|
+
- - ">="
|
32
18
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
19
|
+
version: 12.3.3
|
34
20
|
type: :development
|
35
21
|
prerelease: false
|
36
22
|
version_requirements: !ruby/object:Gem::Requirement
|
37
23
|
requirements:
|
38
|
-
- - "
|
24
|
+
- - ">="
|
39
25
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
26
|
+
version: 12.3.3
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: rspec
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -68,7 +54,7 @@ dependencies:
|
|
68
54
|
version: 3.2.21
|
69
55
|
description: A small wrapper around ActiveModel Validations.
|
70
56
|
email:
|
71
|
-
- robb@
|
57
|
+
- robb@public.law
|
72
58
|
executables: []
|
73
59
|
extensions: []
|
74
60
|
extra_rdoc_files: []
|
@@ -87,12 +73,12 @@ files:
|
|
87
73
|
- lib/validated_object/version.rb
|
88
74
|
- script/demo.rb
|
89
75
|
- validated_object.gemspec
|
90
|
-
homepage: https://github.com/
|
76
|
+
homepage: https://github.com/public-law/validated_object
|
91
77
|
licenses:
|
92
78
|
- MIT
|
93
79
|
metadata:
|
94
80
|
allowed_push_host: https://rubygems.org
|
95
|
-
post_install_message:
|
81
|
+
post_install_message:
|
96
82
|
rdoc_options: []
|
97
83
|
require_paths:
|
98
84
|
- lib
|
@@ -107,9 +93,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
93
|
- !ruby/object:Gem::Version
|
108
94
|
version: '0'
|
109
95
|
requirements: []
|
110
|
-
|
111
|
-
|
112
|
-
signing_key:
|
96
|
+
rubygems_version: 3.1.4
|
97
|
+
signing_key:
|
113
98
|
specification_version: 4
|
114
99
|
summary: Self-validating plain Ruby objects.
|
115
100
|
test_files: []
|