dry-validation 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/LICENSE +1 -1
- data/README.md +9 -9
- data/dry-validation.gemspec +2 -1
- data/lib/dry/validation.rb +1 -0
- data/lib/dry/validation/input_type_compiler.rb +2 -2
- data/lib/dry/validation/schema.rb +7 -6
- data/lib/dry/validation/schema/key.rb +0 -2
- data/lib/dry/validation/version.rb +1 -1
- data/spec/integration/custom_predicates_spec.rb +1 -1
- data/spec/integration/schema_form_spec.rb +0 -2
- data/spec/unit/input_type_compiler_spec.rb +57 -0
- metadata +20 -89
- data/lib/dry/validation/predicate.rb +0 -35
- data/lib/dry/validation/predicate_set.rb +0 -22
- data/lib/dry/validation/predicates.rb +0 -125
- data/lib/dry/validation/rule.rb +0 -90
- data/lib/dry/validation/rule/check.rb +0 -15
- data/lib/dry/validation/rule/composite.rb +0 -63
- data/lib/dry/validation/rule/each.rb +0 -13
- data/lib/dry/validation/rule/group.rb +0 -21
- data/lib/dry/validation/rule/key.rb +0 -17
- data/lib/dry/validation/rule/result.rb +0 -119
- data/lib/dry/validation/rule/set.rb +0 -22
- data/lib/dry/validation/rule/value.rb +0 -13
- data/lib/dry/validation/rule_compiler.rb +0 -81
- data/spec/shared/predicates.rb +0 -35
- data/spec/unit/predicate_spec.rb +0 -27
- data/spec/unit/predicates/bool_spec.rb +0 -34
- data/spec/unit/predicates/date_spec.rb +0 -31
- data/spec/unit/predicates/date_time_spec.rb +0 -31
- data/spec/unit/predicates/decimal_spec.rb +0 -32
- data/spec/unit/predicates/empty_spec.rb +0 -38
- data/spec/unit/predicates/eql_spec.rb +0 -21
- data/spec/unit/predicates/exclusion_spec.rb +0 -35
- data/spec/unit/predicates/filled_spec.rb +0 -38
- data/spec/unit/predicates/float_spec.rb +0 -31
- data/spec/unit/predicates/format_spec.rb +0 -21
- data/spec/unit/predicates/gt_spec.rb +0 -40
- data/spec/unit/predicates/gteq_spec.rb +0 -40
- data/spec/unit/predicates/inclusion_spec.rb +0 -35
- data/spec/unit/predicates/int_spec.rb +0 -34
- data/spec/unit/predicates/key_spec.rb +0 -29
- data/spec/unit/predicates/lt_spec.rb +0 -40
- data/spec/unit/predicates/lteq_spec.rb +0 -40
- data/spec/unit/predicates/max_size_spec.rb +0 -49
- data/spec/unit/predicates/min_size_spec.rb +0 -49
- data/spec/unit/predicates/none_spec.rb +0 -28
- data/spec/unit/predicates/size_spec.rb +0 -55
- data/spec/unit/predicates/str_spec.rb +0 -32
- data/spec/unit/predicates/time_spec.rb +0 -31
- data/spec/unit/rule/check_spec.rb +0 -29
- data/spec/unit/rule/conjunction_spec.rb +0 -28
- data/spec/unit/rule/disjunction_spec.rb +0 -36
- data/spec/unit/rule/each_spec.rb +0 -20
- data/spec/unit/rule/group_spec.rb +0 -12
- data/spec/unit/rule/implication_spec.rb +0 -14
- data/spec/unit/rule/key_spec.rb +0 -27
- data/spec/unit/rule/set_spec.rb +0 -32
- data/spec/unit/rule/value_spec.rb +0 -42
- data/spec/unit/rule_compiler_spec.rb +0 -123
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d2494cc932a3cd69938b609aa9ad9b19477097a
|
4
|
+
data.tar.gz: 1f8ea70284ddb90ef3c84f05f78684e66cfd040d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1acce326a10195fe64fd864aa784b5c975122636e194b658572ff1bde82fd7eb5f31fb13bd785447243446f4cd6c31c7e2594feb6082270feb1e43a9fbe2395
|
7
|
+
data.tar.gz: 61f64697186fd3ad6ca35b5a1ee5d9b9725fe705a45a72fc5fd0fb1b1c3f4180f9564d0bb85ed1302195d3c5a2b3fb27ea548412ad57167b0d9a50928bb461d5
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
# v0.5.0 2016-01-11
|
2
|
+
|
3
|
+
### Changed
|
4
|
+
|
5
|
+
* Now depends on `[dry-logic](https://github.com/dryrb/dry-logic` for predicates and rules (solnic)
|
6
|
+
* `dry/validation/schema/form` is now required by default (solnic)
|
7
|
+
|
8
|
+
### Fixed
|
9
|
+
|
10
|
+
* `Schema::Form` uses safe `form.array` and `form.hash` types which fixes #42 (solnic)
|
11
|
+
|
12
|
+
[Compare v0.5.1...v0.5.0](https://github.com/dryrb/dry-validation/compare/v0.4.1...v0.5.0)
|
13
|
+
|
1
14
|
# v0.4.1 2015-12-27
|
2
15
|
|
3
16
|
### Added
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -42,11 +42,11 @@ be an empty string.
|
|
42
42
|
|
43
43
|
## The DSL
|
44
44
|
|
45
|
-
The core of `dry-validation` is rule composition and predicate logic
|
46
|
-
is a simple front-end
|
47
|
-
|
48
|
-
|
49
|
-
logic expressed in a concise way.
|
45
|
+
The core of `dry-validation` is rule composition and predicate logic provided by
|
46
|
+
[dry-logic](https://github.com/dryrb/dry-logic). The DSL is a simple front-end
|
47
|
+
for it. It only allows you to define the rules by using predicate identifiers.
|
48
|
+
There are no magical options, conditionals and custom validation blocks known from
|
49
|
+
other libraries. The focus is on pure validation logic expressed in a concise way.
|
50
50
|
|
51
51
|
The DSL is very abstract, it builds [a rule AST](https://github.com/dryrb/dry-validation/wiki/Rule-AST)
|
52
52
|
which is compiled into an array of rule objects. This means alternative interfaces could
|
@@ -74,11 +74,11 @@ Please refer to [the wiki](https://github.com/dryrb/dry-validation/wiki) for ful
|
|
74
74
|
``` ruby
|
75
75
|
class UserSchema < Dry::Validation::Schema
|
76
76
|
key(:name) { |name| name.filled? }
|
77
|
-
|
77
|
+
|
78
78
|
key(:email) { |email| email.filled? & email.format?(EMAIL_REGEX) }
|
79
|
-
|
79
|
+
|
80
80
|
key(:age) { |age| age.none? | age.int? }
|
81
|
-
|
81
|
+
|
82
82
|
key(:address) do |address|
|
83
83
|
address.key(:street, &:filled?)
|
84
84
|
address.key(:city, &:filled?)
|
@@ -89,7 +89,7 @@ end
|
|
89
89
|
|
90
90
|
## Status and Roadmap
|
91
91
|
|
92
|
-
This library is in an early stage of development but you are
|
92
|
+
This library is in an early stage of development but you are encouraged to
|
93
93
|
try it out and provide feedback.
|
94
94
|
|
95
95
|
For planned features check out [the issues](https://github.com/dryrb/dry-validation/labels/feature).
|
data/dry-validation.gemspec
CHANGED
@@ -18,7 +18,8 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.add_runtime_dependency 'dry-configurable', '~> 0.1'
|
19
19
|
spec.add_runtime_dependency 'dry-container', '~> 0.2', '>= 0.2.6'
|
20
20
|
spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
|
21
|
-
spec.add_runtime_dependency 'dry-
|
21
|
+
spec.add_runtime_dependency 'dry-logic', '~> 0.1'
|
22
|
+
spec.add_runtime_dependency 'dry-data', '~> 0.5', '>= 0.5.0'
|
22
23
|
|
23
24
|
spec.add_development_dependency 'bundler'
|
24
25
|
spec.add_development_dependency 'rake'
|
data/lib/dry/validation.rb
CHANGED
@@ -68,11 +68,11 @@ module Dry
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def visit_set(node, *args)
|
71
|
-
[:type, ['hash', [:symbolized, node[1].map { |n| visit(n) }]]]
|
71
|
+
[:type, ['form.hash', [:symbolized, node[1].map { |n| visit(n) }]]]
|
72
72
|
end
|
73
73
|
|
74
74
|
def visit_each(node, *args)
|
75
|
-
[:type, ['array', visit(node[1], *args)]]
|
75
|
+
[:type, ['form.array', visit(node[1], *args)]]
|
76
76
|
end
|
77
77
|
|
78
78
|
def visit_predicate(node, *args)
|
@@ -1,7 +1,8 @@
|
|
1
|
+
require 'dry/logic/predicates'
|
2
|
+
require 'dry/logic/rule_compiler'
|
3
|
+
|
1
4
|
require 'dry/validation/schema/definition'
|
2
|
-
require 'dry/validation/predicates'
|
3
5
|
require 'dry/validation/error'
|
4
|
-
require 'dry/validation/rule_compiler'
|
5
6
|
require 'dry/validation/messages'
|
6
7
|
require 'dry/validation/error_compiler'
|
7
8
|
require 'dry/validation/hint_compiler'
|
@@ -14,7 +15,7 @@ module Dry
|
|
14
15
|
extend Dry::Configurable
|
15
16
|
extend Definition
|
16
17
|
|
17
|
-
setting :predicates, Predicates
|
18
|
+
setting :predicates, Logic::Predicates
|
18
19
|
setting :messages, :yaml
|
19
20
|
setting :messages_file
|
20
21
|
setting :namespace
|
@@ -74,7 +75,7 @@ module Dry
|
|
74
75
|
attr_reader :hint_compiler
|
75
76
|
|
76
77
|
def initialize(error_compiler = self.class.error_compiler, hint_compiler = self.class.hint_compiler)
|
77
|
-
compiler = RuleCompiler.new(self)
|
78
|
+
compiler = Logic::RuleCompiler.new(self)
|
78
79
|
@rules = compiler.(self.class.rules.map(&:to_ary))
|
79
80
|
@checks = self.class.checks
|
80
81
|
@groups = compiler.(self.class.groups.map(&:to_ary))
|
@@ -91,7 +92,7 @@ module Dry
|
|
91
92
|
end
|
92
93
|
|
93
94
|
if checks.size > 0
|
94
|
-
compiled_checks = RuleCompiler.new(result.to_h).(checks)
|
95
|
+
compiled_checks = Logic::RuleCompiler.new(result.to_h).(checks)
|
95
96
|
|
96
97
|
compiled_checks.each do |rule|
|
97
98
|
result << rule.()
|
@@ -113,7 +114,7 @@ module Dry
|
|
113
114
|
if predicates.key?(name)
|
114
115
|
predicates[name]
|
115
116
|
elsif respond_to?(name)
|
116
|
-
Predicate.new(name, &method(name))
|
117
|
+
Logic::Predicate.new(name, &method(name))
|
117
118
|
else
|
118
119
|
raise ArgumentError, "+#{name}+ is not a valid predicate name"
|
119
120
|
end
|
@@ -251,4 +251,61 @@ RSpec.describe Dry::Validation::InputTypeCompiler, '#call' do
|
|
251
251
|
author: { books: [published: false] }
|
252
252
|
)
|
253
253
|
end
|
254
|
+
|
255
|
+
it 'supports array? with an each rule' do
|
256
|
+
rule_ast = [
|
257
|
+
[
|
258
|
+
:and, [
|
259
|
+
[:key, [:ids, [:predicate, [:key?, []]]]],
|
260
|
+
[
|
261
|
+
:and, [
|
262
|
+
[:val, [:ids, [:predicate, [:array?, []]]]],
|
263
|
+
[:each, [:ids, [:val, [:ids, [:predicate, [:int?, []]]]]]]
|
264
|
+
]
|
265
|
+
]
|
266
|
+
]
|
267
|
+
]
|
268
|
+
]
|
269
|
+
|
270
|
+
input_type = compiler.(rule_ast)
|
271
|
+
|
272
|
+
expect(input_type.('ids' => 'oops')).to eql(ids: 'oops')
|
273
|
+
|
274
|
+
expect(input_type.('ids' => %w(1 2 3))).to eql(ids: [1, 2, 3])
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'supports hash? with a set rule' do
|
278
|
+
rule_ast = [
|
279
|
+
[
|
280
|
+
:and, [
|
281
|
+
[:key, [:address, [:predicate, [:key?, []]]]],
|
282
|
+
[
|
283
|
+
:and, [
|
284
|
+
[:val, [:address, [:predicate, [:hash?, []]]]],
|
285
|
+
[
|
286
|
+
:set, [
|
287
|
+
:address, [
|
288
|
+
[
|
289
|
+
:and, [
|
290
|
+
[:key, [:street, [:predicate, [:key?, []]]]],
|
291
|
+
[:val, [:street, [:predicate, [:filled?, []]]]]
|
292
|
+
]
|
293
|
+
]
|
294
|
+
]
|
295
|
+
]
|
296
|
+
]
|
297
|
+
]
|
298
|
+
]
|
299
|
+
]
|
300
|
+
]
|
301
|
+
]
|
302
|
+
|
303
|
+
input_type = compiler.(rule_ast)
|
304
|
+
|
305
|
+
expect(input_type.('address' => 'oops')).to eql(address: 'oops')
|
306
|
+
|
307
|
+
expect(input_type.('address' => { 'street' => 'ok' })).to eql(
|
308
|
+
address: { street: 'ok' }
|
309
|
+
)
|
310
|
+
end
|
254
311
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andy Holland
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-01-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dry-configurable
|
@@ -59,26 +59,40 @@ dependencies:
|
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0.2'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: dry-logic
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.1'
|
69
|
+
type: :runtime
|
70
|
+
prerelease: false
|
71
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.1'
|
62
76
|
- !ruby/object:Gem::Dependency
|
63
77
|
name: dry-data
|
64
78
|
requirement: !ruby/object:Gem::Requirement
|
65
79
|
requirements:
|
66
80
|
- - "~>"
|
67
81
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
82
|
+
version: '0.5'
|
69
83
|
- - ">="
|
70
84
|
- !ruby/object:Gem::Version
|
71
|
-
version: 0.
|
85
|
+
version: 0.5.0
|
72
86
|
type: :runtime
|
73
87
|
prerelease: false
|
74
88
|
version_requirements: !ruby/object:Gem::Requirement
|
75
89
|
requirements:
|
76
90
|
- - "~>"
|
77
91
|
- !ruby/object:Gem::Version
|
78
|
-
version: '0.
|
92
|
+
version: '0.5'
|
79
93
|
- - ">="
|
80
94
|
- !ruby/object:Gem::Version
|
81
|
-
version: 0.
|
95
|
+
version: 0.5.0
|
82
96
|
- !ruby/object:Gem::Dependency
|
83
97
|
name: bundler
|
84
98
|
requirement: !ruby/object:Gem::Requirement
|
@@ -157,20 +171,7 @@ files:
|
|
157
171
|
- lib/dry/validation/messages/i18n.rb
|
158
172
|
- lib/dry/validation/messages/namespaced.rb
|
159
173
|
- lib/dry/validation/messages/yaml.rb
|
160
|
-
- lib/dry/validation/predicate.rb
|
161
|
-
- lib/dry/validation/predicate_set.rb
|
162
|
-
- lib/dry/validation/predicates.rb
|
163
174
|
- lib/dry/validation/result.rb
|
164
|
-
- lib/dry/validation/rule.rb
|
165
|
-
- lib/dry/validation/rule/check.rb
|
166
|
-
- lib/dry/validation/rule/composite.rb
|
167
|
-
- lib/dry/validation/rule/each.rb
|
168
|
-
- lib/dry/validation/rule/group.rb
|
169
|
-
- lib/dry/validation/rule/key.rb
|
170
|
-
- lib/dry/validation/rule/result.rb
|
171
|
-
- lib/dry/validation/rule/set.rb
|
172
|
-
- lib/dry/validation/rule/value.rb
|
173
|
-
- lib/dry/validation/rule_compiler.rb
|
174
175
|
- lib/dry/validation/schema.rb
|
175
176
|
- lib/dry/validation/schema/definition.rb
|
176
177
|
- lib/dry/validation/schema/form.rb
|
@@ -197,45 +198,10 @@ files:
|
|
197
198
|
- spec/integration/schema/xor_spec.rb
|
198
199
|
- spec/integration/schema_form_spec.rb
|
199
200
|
- spec/integration/schema_spec.rb
|
200
|
-
- spec/shared/predicates.rb
|
201
201
|
- spec/spec_helper.rb
|
202
202
|
- spec/unit/error_compiler_spec.rb
|
203
203
|
- spec/unit/hint_compiler_spec.rb
|
204
204
|
- spec/unit/input_type_compiler_spec.rb
|
205
|
-
- spec/unit/predicate_spec.rb
|
206
|
-
- spec/unit/predicates/bool_spec.rb
|
207
|
-
- spec/unit/predicates/date_spec.rb
|
208
|
-
- spec/unit/predicates/date_time_spec.rb
|
209
|
-
- spec/unit/predicates/decimal_spec.rb
|
210
|
-
- spec/unit/predicates/empty_spec.rb
|
211
|
-
- spec/unit/predicates/eql_spec.rb
|
212
|
-
- spec/unit/predicates/exclusion_spec.rb
|
213
|
-
- spec/unit/predicates/filled_spec.rb
|
214
|
-
- spec/unit/predicates/float_spec.rb
|
215
|
-
- spec/unit/predicates/format_spec.rb
|
216
|
-
- spec/unit/predicates/gt_spec.rb
|
217
|
-
- spec/unit/predicates/gteq_spec.rb
|
218
|
-
- spec/unit/predicates/inclusion_spec.rb
|
219
|
-
- spec/unit/predicates/int_spec.rb
|
220
|
-
- spec/unit/predicates/key_spec.rb
|
221
|
-
- spec/unit/predicates/lt_spec.rb
|
222
|
-
- spec/unit/predicates/lteq_spec.rb
|
223
|
-
- spec/unit/predicates/max_size_spec.rb
|
224
|
-
- spec/unit/predicates/min_size_spec.rb
|
225
|
-
- spec/unit/predicates/none_spec.rb
|
226
|
-
- spec/unit/predicates/size_spec.rb
|
227
|
-
- spec/unit/predicates/str_spec.rb
|
228
|
-
- spec/unit/predicates/time_spec.rb
|
229
|
-
- spec/unit/rule/check_spec.rb
|
230
|
-
- spec/unit/rule/conjunction_spec.rb
|
231
|
-
- spec/unit/rule/disjunction_spec.rb
|
232
|
-
- spec/unit/rule/each_spec.rb
|
233
|
-
- spec/unit/rule/group_spec.rb
|
234
|
-
- spec/unit/rule/implication_spec.rb
|
235
|
-
- spec/unit/rule/key_spec.rb
|
236
|
-
- spec/unit/rule/set_spec.rb
|
237
|
-
- spec/unit/rule/value_spec.rb
|
238
|
-
- spec/unit/rule_compiler_spec.rb
|
239
205
|
- spec/unit/schema/rule_spec.rb
|
240
206
|
- spec/unit/schema/value_spec.rb
|
241
207
|
- spec/unit/schema_spec.rb
|
@@ -281,45 +247,10 @@ test_files:
|
|
281
247
|
- spec/integration/schema/xor_spec.rb
|
282
248
|
- spec/integration/schema_form_spec.rb
|
283
249
|
- spec/integration/schema_spec.rb
|
284
|
-
- spec/shared/predicates.rb
|
285
250
|
- spec/spec_helper.rb
|
286
251
|
- spec/unit/error_compiler_spec.rb
|
287
252
|
- spec/unit/hint_compiler_spec.rb
|
288
253
|
- spec/unit/input_type_compiler_spec.rb
|
289
|
-
- spec/unit/predicate_spec.rb
|
290
|
-
- spec/unit/predicates/bool_spec.rb
|
291
|
-
- spec/unit/predicates/date_spec.rb
|
292
|
-
- spec/unit/predicates/date_time_spec.rb
|
293
|
-
- spec/unit/predicates/decimal_spec.rb
|
294
|
-
- spec/unit/predicates/empty_spec.rb
|
295
|
-
- spec/unit/predicates/eql_spec.rb
|
296
|
-
- spec/unit/predicates/exclusion_spec.rb
|
297
|
-
- spec/unit/predicates/filled_spec.rb
|
298
|
-
- spec/unit/predicates/float_spec.rb
|
299
|
-
- spec/unit/predicates/format_spec.rb
|
300
|
-
- spec/unit/predicates/gt_spec.rb
|
301
|
-
- spec/unit/predicates/gteq_spec.rb
|
302
|
-
- spec/unit/predicates/inclusion_spec.rb
|
303
|
-
- spec/unit/predicates/int_spec.rb
|
304
|
-
- spec/unit/predicates/key_spec.rb
|
305
|
-
- spec/unit/predicates/lt_spec.rb
|
306
|
-
- spec/unit/predicates/lteq_spec.rb
|
307
|
-
- spec/unit/predicates/max_size_spec.rb
|
308
|
-
- spec/unit/predicates/min_size_spec.rb
|
309
|
-
- spec/unit/predicates/none_spec.rb
|
310
|
-
- spec/unit/predicates/size_spec.rb
|
311
|
-
- spec/unit/predicates/str_spec.rb
|
312
|
-
- spec/unit/predicates/time_spec.rb
|
313
|
-
- spec/unit/rule/check_spec.rb
|
314
|
-
- spec/unit/rule/conjunction_spec.rb
|
315
|
-
- spec/unit/rule/disjunction_spec.rb
|
316
|
-
- spec/unit/rule/each_spec.rb
|
317
|
-
- spec/unit/rule/group_spec.rb
|
318
|
-
- spec/unit/rule/implication_spec.rb
|
319
|
-
- spec/unit/rule/key_spec.rb
|
320
|
-
- spec/unit/rule/set_spec.rb
|
321
|
-
- spec/unit/rule/value_spec.rb
|
322
|
-
- spec/unit/rule_compiler_spec.rb
|
323
254
|
- spec/unit/schema/rule_spec.rb
|
324
255
|
- spec/unit/schema/value_spec.rb
|
325
256
|
- spec/unit/schema_spec.rb
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
def self.Predicate(block)
|
4
|
-
case block
|
5
|
-
when Method then Predicate.new(block.name, &block)
|
6
|
-
else raise ArgumentError, 'predicate needs an :id'
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class Predicate
|
11
|
-
include Dry::Equalizer(:id)
|
12
|
-
|
13
|
-
attr_reader :id, :args, :fn
|
14
|
-
|
15
|
-
def initialize(id, *args, &block)
|
16
|
-
@id = id
|
17
|
-
@fn = block
|
18
|
-
@args = args
|
19
|
-
end
|
20
|
-
|
21
|
-
def call(*args)
|
22
|
-
fn.(*args)
|
23
|
-
end
|
24
|
-
|
25
|
-
def curry(*args)
|
26
|
-
self.class.new(id, *args, &fn.curry.(*args))
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_ary
|
30
|
-
[:predicate, [id, args]]
|
31
|
-
end
|
32
|
-
alias_method :to_a, :to_ary
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|