validated_object 2.3.0 → 2.3.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/.github/workflows/ruby.yml +37 -0
- data/README.md +17 -15
- data/lib/validated_object/version.rb +1 -1
- data/validated_object.gemspec +2 -0
- metadata +5 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 03a54694b8208e1e788cd55ece182bacdbfa75f3389db8c0dcc8902df92d9822
|
4
|
+
data.tar.gz: 03c58a85faa371f663201c0deb0e7ed8cb021f35aae8d3568e18f24e7db09c59
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f3bf8d6e81b2359e60f830d493e0a4fed9997bc21fa21c33f838794300e1b97514999ab42fb478a5cd22562ec89aa0dcfddf3a2c127b6b804f9f5f34faaea11
|
7
|
+
data.tar.gz: 5f05239f7150ffc10dc11544717298ec6500804ba4ab764cc8ba1dcb0bf1c6af7dfe59c927c829183902b575b2133ebc5278d2b1b9a3d8cd701dda8fd7a08f4d
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
2
|
+
# They are provided by a third-party and are governed by
|
3
|
+
# separate terms of service, privacy policy, and support
|
4
|
+
# documentation.
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
+
|
8
|
+
name: Ruby
|
9
|
+
|
10
|
+
on:
|
11
|
+
push:
|
12
|
+
branches: [ "master" ]
|
13
|
+
pull_request:
|
14
|
+
branches: [ "master" ]
|
15
|
+
|
16
|
+
permissions:
|
17
|
+
contents: read
|
18
|
+
|
19
|
+
jobs:
|
20
|
+
test:
|
21
|
+
|
22
|
+
runs-on: ubuntu-latest
|
23
|
+
strategy:
|
24
|
+
matrix:
|
25
|
+
ruby-version: ['3.1', '3.2', '3.3', '3.4']
|
26
|
+
|
27
|
+
steps:
|
28
|
+
- uses: actions/checkout@v3
|
29
|
+
|
30
|
+
- name: Set up Ruby
|
31
|
+
uses: ruby/setup-ruby@v1
|
32
|
+
with:
|
33
|
+
ruby-version: ${{ matrix.ruby-version }}
|
34
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
35
|
+
|
36
|
+
- name: Run tests
|
37
|
+
run: bundle exec rake
|
data/README.md
CHANGED
@@ -1,21 +1,22 @@
|
|
1
|
-
[](https://badge.fury.io/rb/validated_object)
|
2
|
-
|
1
|
+
[](https://badge.fury.io/rb/validated_object)
|
3
2
|
# ValidatedObject
|
4
3
|
|
5
|
-
Plain Old Ruby
|
6
|
-
|
7
|
-
|
4
|
+
`Plain Old Ruby` + `Rails Validations` = **self-checking Ruby objects**.
|
5
|
+
|
6
|
+
## Example: A `Person` class that ensures its `name` isn't blank (nil or empty string):
|
8
7
|
|
9
8
|
```ruby
|
10
9
|
class Person < ValidatedObject::Base
|
11
|
-
|
10
|
+
attr_reader :name
|
11
|
+
validates :name, presence: true
|
12
12
|
end
|
13
13
|
|
14
|
-
#
|
15
|
-
me
|
14
|
+
# Instantiating it runs the validations.
|
15
|
+
me = Person.new(name: 'Robb')
|
16
|
+
you = Person.new(name: '') # => ArgumentError: "Name can't be blank"
|
16
17
|
```
|
17
18
|
|
18
|
-
I use classes like these as Data Transfer Objects at my system boundaries.
|
19
|
+
Note how Person's two lines of code are nothing new: `attr_reader` is standard Ruby. [`validates`](https://guides.rubyonrails.org/active_record_validations.html) is standard Rails. I use classes like these as Data Transfer Objects at my system boundaries.
|
19
20
|
|
20
21
|
|
21
22
|
## Goals
|
@@ -24,7 +25,7 @@ I use classes like these as Data Transfer Objects at my system boundaries.
|
|
24
25
|
* Clean, minimal syntax
|
25
26
|
|
26
27
|
This is a small layer around
|
27
|
-
[ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates). (About 25 lines of code.) So if you know how to use Rails Validations, you're good to go. I wrote this to help with CSV data imports and [website
|
28
|
+
[ActiveModel::Validations](http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates). (About 25 lines of code.) So if you know how to use [Rails Validations](https://guides.rubyonrails.org/active_record_validations.html), you're good to go. I wrote this to help with CSV data imports and [website structured data](https://github.com/public-law/schema-dot-org).
|
28
29
|
|
29
30
|
|
30
31
|
## Usage
|
@@ -47,7 +48,7 @@ class Dog < ValidatedObject::Base
|
|
47
48
|
end
|
48
49
|
```
|
49
50
|
|
50
|
-
|
51
|
+
Alternatively, we could make it immutable with Ruby's [attr_reader](https://bootrails.com/blog/ruby-attr-accessor-attr-writer-attr-reader/#2-attr_reader-attr_writer--attr_accessor):
|
51
52
|
|
52
53
|
```ruby
|
53
54
|
class ImmutableDog < ValidatedObject::Base
|
@@ -58,10 +59,11 @@ class ImmutableDog < ValidatedObject::Base
|
|
58
59
|
end
|
59
60
|
```
|
60
61
|
|
61
|
-
|
62
|
-
|
62
|
+
And again, that `ImmutableDog` consists of one line of plain Ruby and two lines of standard Rails validations.
|
63
|
+
|
64
|
+
### `attr_reader` followed by `validates` is such a common pattern that there's a DSL which wraps them up into one call: `validates_attr`.
|
63
65
|
|
64
|
-
Here's the immutable version of `Dog` re-written with the simplified DSL:
|
66
|
+
Here's the immutable version of `Dog` re-written with the new, simplified DSL:
|
65
67
|
|
66
68
|
```ruby
|
67
69
|
class ImmutableDog < ValidatedObject::Base
|
@@ -139,7 +141,7 @@ the data.
|
|
139
141
|
|
140
142
|
### Use in code generation
|
141
143
|
|
142
|
-
My [Schema.org
|
144
|
+
My [Schema.org structured data gem](https://github.com/public-law/schema-dot-org) uses ValidatedObjects to recursively create well formed HTML / JSON-LD.
|
143
145
|
|
144
146
|
## Installation
|
145
147
|
|
data/validated_object.gemspec
CHANGED
@@ -5,6 +5,8 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
5
|
require 'validated_object/version'
|
6
6
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
|
+
spec.required_ruby_version = '>= 3.1'
|
9
|
+
|
8
10
|
spec.name = 'validated_object'
|
9
11
|
spec.version = ValidatedObject::VERSION
|
10
12
|
spec.authors = ['Robb Shecter']
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validated_object
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.3.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robb Shecter
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: rake
|
@@ -87,6 +86,7 @@ executables: []
|
|
87
86
|
extensions: []
|
88
87
|
extra_rdoc_files: []
|
89
88
|
files:
|
89
|
+
- ".github/workflows/ruby.yml"
|
90
90
|
- ".gitignore"
|
91
91
|
- ".rspec"
|
92
92
|
- ".travis.yml"
|
@@ -125,7 +125,6 @@ licenses:
|
|
125
125
|
- MIT
|
126
126
|
metadata:
|
127
127
|
allowed_push_host: https://rubygems.org
|
128
|
-
post_install_message:
|
129
128
|
rdoc_options: []
|
130
129
|
require_paths:
|
131
130
|
- lib
|
@@ -133,15 +132,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
133
132
|
requirements:
|
134
133
|
- - ">="
|
135
134
|
- !ruby/object:Gem::Version
|
136
|
-
version: '
|
135
|
+
version: '3.1'
|
137
136
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
137
|
requirements:
|
139
138
|
- - ">="
|
140
139
|
- !ruby/object:Gem::Version
|
141
140
|
version: '0'
|
142
141
|
requirements: []
|
143
|
-
rubygems_version: 3.
|
144
|
-
signing_key:
|
142
|
+
rubygems_version: 3.6.9
|
145
143
|
specification_version: 4
|
146
144
|
summary: Self-validating plain Ruby objects.
|
147
145
|
test_files: []
|