active_model_validations_reflection 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +1 -0
- data/.rubocop.yml +11 -0
- data/.travis.yml +11 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +80 -0
- data/LICENSE +13 -0
- data/README.md +90 -0
- data/Rakefile +11 -0
- data/active_model_validations_reflection.gemspec +34 -0
- data/lib/active_model/validations/reflection.rb +71 -0
- data/lib/active_model_validations_reflection.rb +7 -0
- data/lib/active_model_validations_reflection/version.rb +5 -0
- metadata +169 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 03df2721bd43802064e8365a8409e6a3e9f4016c17cfc3cc03ec544269a4b560
|
4
|
+
data.tar.gz: 03163f128e2fcf350c481e9dc4023a7c2c8409ee4a50308d5c4548228314cb8e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e301c7c80c86cd343f8e077488cee4d6cd4cc160485a54379cead37ad3aed8f9e8b4f22f6e91353c1fff89bd2da84afcab37ba2525b52cf6868ced1cbb96f7f1
|
7
|
+
data.tar.gz: 36ba3ec7d564406e0c70e721330a1f726a9883b022248353c7da368752d705545a6bffa06264412434ab39d0da19d2c9b2a5fb4c671685a99abe9c59160ac5e2
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_model_validations_reflection (0.1.0)
|
5
|
+
activemodel (>= 3.2)
|
6
|
+
activesupport (>= 3.2)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (6.1.3.2)
|
12
|
+
activesupport (= 6.1.3.2)
|
13
|
+
activesupport (6.1.3.2)
|
14
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
15
|
+
i18n (>= 1.6, < 2)
|
16
|
+
minitest (>= 5.1)
|
17
|
+
tzinfo (~> 2.0)
|
18
|
+
zeitwerk (~> 2.3)
|
19
|
+
ast (2.4.2)
|
20
|
+
concurrent-ruby (1.1.8)
|
21
|
+
diff-lcs (1.4.4)
|
22
|
+
i18n (1.8.10)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
minitest (5.14.4)
|
25
|
+
parallel (1.20.1)
|
26
|
+
parser (3.0.1.1)
|
27
|
+
ast (~> 2.4.1)
|
28
|
+
rainbow (3.0.0)
|
29
|
+
rake (13.0.3)
|
30
|
+
regexp_parser (2.1.1)
|
31
|
+
rexml (3.2.5)
|
32
|
+
rspec (3.10.0)
|
33
|
+
rspec-core (~> 3.10.0)
|
34
|
+
rspec-expectations (~> 3.10.0)
|
35
|
+
rspec-mocks (~> 3.10.0)
|
36
|
+
rspec-core (3.10.1)
|
37
|
+
rspec-support (~> 3.10.0)
|
38
|
+
rspec-expectations (3.10.1)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.10.0)
|
41
|
+
rspec-mocks (3.10.2)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.10.0)
|
44
|
+
rspec-support (3.10.2)
|
45
|
+
rubocop (1.14.0)
|
46
|
+
parallel (~> 1.10)
|
47
|
+
parser (>= 3.0.0.0)
|
48
|
+
rainbow (>= 2.2.2, < 4.0)
|
49
|
+
regexp_parser (>= 1.8, < 3.0)
|
50
|
+
rexml
|
51
|
+
rubocop-ast (>= 1.5.0, < 2.0)
|
52
|
+
ruby-progressbar (~> 1.7)
|
53
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
54
|
+
rubocop-ast (1.5.0)
|
55
|
+
parser (>= 3.0.1.1)
|
56
|
+
rubocop-rake (0.5.1)
|
57
|
+
rubocop
|
58
|
+
rubocop-rspec (2.3.0)
|
59
|
+
rubocop (~> 1.0)
|
60
|
+
rubocop-ast (>= 1.1.0)
|
61
|
+
ruby-progressbar (1.11.0)
|
62
|
+
tzinfo (2.0.4)
|
63
|
+
concurrent-ruby (~> 1.0)
|
64
|
+
unicode-display_width (2.0.0)
|
65
|
+
zeitwerk (2.4.2)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
x86_64-linux
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
active_model_validations_reflection!
|
72
|
+
bundler (~> 2.1)
|
73
|
+
rake (~> 13.0)
|
74
|
+
rspec (~> 3.10.0)
|
75
|
+
rubocop
|
76
|
+
rubocop-rake
|
77
|
+
rubocop-rspec
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
2.2.3
|
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2021 notus.sh
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# ActiveModel::Validations::Reflection
|
2
|
+
|
3
|
+
[](https://travis-ci.org/notus-sh/active_model_validations_reflection)
|
4
|
+
[](https://badge.fury.io/rb/active_model_validations_reflection)
|
5
|
+
|
6
|
+
`ActiveModel::Validations::Reflection` extends `ActiveModel` reflection capabilities on validations.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
`Rack::AddressMunging` is distributed as a gem and available on [rubygems.org](https://rubygems.org/gems/rack-address_munging) so you can add it to your `Gemfile` or install it manually with:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem install active_model_validations_reflection
|
14
|
+
```
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
`ActiveModel`, at least since 3.0, provide a basic mechanism to inspect validations on a model through [`ActiveModel::Validations::ClassMethods#validators`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators) and [`ActiveModel::Validations::ClassMethods#validators_on`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validators_on).
|
19
|
+
|
20
|
+
This extends it with easier filtering on validators on class level and contextual filtering on instance level.
|
21
|
+
|
22
|
+
### `.validators_of_kinds(*kinds)` & `.validators_on_of_kinds(attribute, *kinds)`
|
23
|
+
|
24
|
+
`.validators_of_kinds` & `.validators_on_of_kinds` are class methods to filter validators by kind, respectively on all or a single attribute.
|
25
|
+
|
26
|
+
Any kind supported by [`validates`](https://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates) (`:presence` , `:inclusion`, `:uniqueness`, or custom validator's kind) can be used to filter validators.
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class Article < ApplicationRecord
|
30
|
+
include ActiveModel::Validations::Reflection
|
31
|
+
|
32
|
+
validates :date,
|
33
|
+
presence: true,
|
34
|
+
timeliness: { type: :date }
|
35
|
+
end
|
36
|
+
|
37
|
+
Article.validators_of_kinds(:presence)
|
38
|
+
# => [#<ActiveModel::Validations::PresenceValidator […]>]
|
39
|
+
Article.validators_on_of_kinds(:date, :timeliness)
|
40
|
+
# => [#<ValidatesTimeliness::Validator […]>]
|
41
|
+
```
|
42
|
+
|
43
|
+
### `.flat_validators_of_kinds(*kinds)` & `.flat_validators_on_of_kinds(attribute, *kinds)`
|
44
|
+
|
45
|
+
Same as the above two but only return validators without conditions (`:if` / `:unless`).
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class Article < ApplicationRecord
|
49
|
+
include ActiveModel::Validations::Reflection
|
50
|
+
|
51
|
+
enum :status, %i[draft published]
|
52
|
+
|
53
|
+
validates :date,
|
54
|
+
presence: true,
|
55
|
+
timeliness: { type: :date, if: :published? }
|
56
|
+
end
|
57
|
+
|
58
|
+
Article.validators_of_kinds(:presence)
|
59
|
+
# => [#<ActiveModel::Validations::PresenceValidator […]>]
|
60
|
+
Article.validators_on_of_kinds(:date, :timeliness)
|
61
|
+
# => []
|
62
|
+
```
|
63
|
+
|
64
|
+
### `#relevant_validators_on(attribute, *kinds)`
|
65
|
+
|
66
|
+
Returns validator that will be applied to the named attribute considering the instance current state: flat validators and conditional validators whose condition is met.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class Article < ApplicationRecord
|
70
|
+
include ActiveModel::Validations::Reflection
|
71
|
+
|
72
|
+
enum :status, %i[draft published]
|
73
|
+
|
74
|
+
validates :date,
|
75
|
+
presence: true,
|
76
|
+
timeliness: { type: :date, if: :published? }
|
77
|
+
end
|
78
|
+
|
79
|
+
article = Article.new
|
80
|
+
article.relevant_validators_on(:date)
|
81
|
+
# => [#<ActiveModel::Validations::PresenceValidator […]>]
|
82
|
+
|
83
|
+
article.status = :published
|
84
|
+
article.relevant_validators_on(:date)
|
85
|
+
# => [#<ActiveModel::Validations::PresenceValidator […]>, #<ValidatesTimeliness::Validator […]>]
|
86
|
+
```
|
87
|
+
|
88
|
+
## Contributing
|
89
|
+
|
90
|
+
Bug reports and pull requests are welcome on GitHub at <https://github.com/notus-sh/active_model_validations_reflection>.
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'lib/active_model_validations_reflection/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'active_model_validations_reflection'
|
7
|
+
spec.version = ActiveModelValidationsReflection::VERSION
|
8
|
+
spec.licenses = ['Apache-2.0']
|
9
|
+
spec.authors = ['Gaël-Ian Havard']
|
10
|
+
spec.email = ['gael-ian@notus.sh']
|
11
|
+
|
12
|
+
spec.summary = 'ActiveModel extension on validations reflection'
|
13
|
+
spec.description = 'An ActiveModel extension for more expressive validations reflection'
|
14
|
+
spec.homepage = 'https://github.com/notus-sh/active_model_validations_reflection'
|
15
|
+
|
16
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
17
|
+
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
20
|
+
f.match(%r{^(test|spec|features)/})
|
21
|
+
end
|
22
|
+
|
23
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
|
24
|
+
|
25
|
+
spec.add_dependency 'activemodel', '>= 3.2'
|
26
|
+
spec.add_dependency 'activesupport', '>= 3.2'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 2.1'
|
29
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
30
|
+
spec.add_development_dependency 'rspec', '~> 3.10.0'
|
31
|
+
spec.add_development_dependency 'rubocop'
|
32
|
+
spec.add_development_dependency 'rubocop-rake'
|
33
|
+
spec.add_development_dependency 'rubocop-rspec'
|
34
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module ActiveModel
|
6
|
+
module Validations
|
7
|
+
module Reflection
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module Helpers
|
11
|
+
class << self
|
12
|
+
def flat_validator?(validator)
|
13
|
+
!(validator.options.key?(:if) || validator.options.key?(:unless))
|
14
|
+
end
|
15
|
+
|
16
|
+
def relevant_validator?(validator, instance)
|
17
|
+
condition = validator.options.key?(:if) ? validator.options[:if] : validator.options[:unless]
|
18
|
+
result = evaluate_condition(condition, instance)
|
19
|
+
|
20
|
+
(validator.options.key?(:unless) ? !result : !!result)
|
21
|
+
end
|
22
|
+
|
23
|
+
def evaluate_condition(condition, instance)
|
24
|
+
return condition.call(instance) if condition.respond_to?(:call)
|
25
|
+
return instance.send(condition) if condition.is_a?(::Symbol) && instance.respond_to?(condition)
|
26
|
+
|
27
|
+
condition
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def validators_of_kinds(*kinds)
|
34
|
+
return validators if kinds.size.zero?
|
35
|
+
|
36
|
+
validators.select do |validator|
|
37
|
+
kinds.include?(validator.kind)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def flat_validators_of_kinds(*kinds)
|
42
|
+
validators_of_kinds(*kinds).select do |validator|
|
43
|
+
Helpers.flat_validator?(validator)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def validators_on_of_kinds(attribute, *kinds)
|
48
|
+
return validators_on(attribute) if kinds.size.zero?
|
49
|
+
|
50
|
+
validators_on(attribute).select do |validator|
|
51
|
+
kinds.include?(validator.kind)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def flat_validators_on_of_kinds(attribute, *kinds)
|
56
|
+
validators_on_of_kinds(attribute, *kinds).select do |validator|
|
57
|
+
Helpers.flat_validator?(validator)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def relevant_validators_on(attribute, *kinds)
|
63
|
+
self.class.validators_on_of_kinds(attribute, *kinds).select do |validator|
|
64
|
+
next true if Helpers.flat_validator?(validator)
|
65
|
+
|
66
|
+
Helpers.relevant_validator?(validator, self)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,169 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_validations_reflection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gaël-Ian Havard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-05-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.10.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.10.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rubocop-rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: An ActiveModel extension for more expressive validations reflection
|
126
|
+
email:
|
127
|
+
- gael-ian@notus.sh
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- Gemfile
|
137
|
+
- Gemfile.lock
|
138
|
+
- LICENSE
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- active_model_validations_reflection.gemspec
|
142
|
+
- lib/active_model/validations/reflection.rb
|
143
|
+
- lib/active_model_validations_reflection.rb
|
144
|
+
- lib/active_model_validations_reflection/version.rb
|
145
|
+
homepage: https://github.com/notus-sh/active_model_validations_reflection
|
146
|
+
licenses:
|
147
|
+
- Apache-2.0
|
148
|
+
metadata:
|
149
|
+
allowed_push_host: https://rubygems.org
|
150
|
+
post_install_message:
|
151
|
+
rdoc_options: []
|
152
|
+
require_paths:
|
153
|
+
- lib
|
154
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
155
|
+
requirements:
|
156
|
+
- - ">="
|
157
|
+
- !ruby/object:Gem::Version
|
158
|
+
version: 2.6.0
|
159
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
requirements:
|
161
|
+
- - ">="
|
162
|
+
- !ruby/object:Gem::Version
|
163
|
+
version: '0'
|
164
|
+
requirements: []
|
165
|
+
rubygems_version: 3.2.3
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: ActiveModel extension on validations reflection
|
169
|
+
test_files: []
|