Class: Dry::Validation::Schema::Value
- Inherits:
-
DSL
- Object
- DSL
- Dry::Validation::Schema::Value
show all
- Defined in:
- lib/dry/validation/schema/value.rb
Direct Known Subclasses
Check
Instance Attribute Summary collapse
Attributes inherited from DSL
#checks, #name, #options, #parent, #rules
Instance Method Summary
collapse
Methods inherited from DSL
[], #add_check, #add_rule, #attr, #key, #not, #optional, #path, #rule_ast, #to_ast, #to_rule, #with
Constructor Details
#initialize(options = {}) ⇒ Value
Returns a new instance of Value.
9
10
11
12
13
|
# File 'lib/dry/validation/schema/value.rb', line 9
def initialize(options = {})
super
@type = options.fetch(:type, :key)
@schema_class = options.fetch(:schema_class, ::Class.new(Schema))
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/dry/validation/schema/value.rb', line 99
def method_missing(meth, *args, &block)
val_rule = create_rule([:val, [:predicate, [meth, args]]])
if block
val = Value.new.instance_eval(&block)
new_rule = create_rule([:and, [val_rule.to_ast, val.to_ast]])
add_rule(new_rule)
else
val_rule
end
end
|
Instance Attribute Details
#schema_class ⇒ Object
Returns the value of attribute schema_class.
7
8
9
|
# File 'lib/dry/validation/schema/value.rb', line 7
def schema_class
@schema_class
end
|
#type ⇒ Object
Returns the value of attribute type.
7
8
9
|
# File 'lib/dry/validation/schema/value.rb', line 7
def type
@type
end
|
Instance Method Details
#check(name, options = {}) ⇒ Object
93
94
95
|
# File 'lib/dry/validation/schema/value.rb', line 93
def check(name, options = {})
Check[name, options.merge(type: type)]
end
|
#class ⇒ Object
25
26
27
|
# File 'lib/dry/validation/schema/value.rb', line 25
def class
Value
end
|
15
16
17
18
19
|
# File 'lib/dry/validation/schema/value.rb', line 15
def configure(&block)
klass = ::Class.new(schema_class, &block)
@schema_class = klass
self
end
|
#confirmation ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/dry/validation/schema/value.rb', line 81
def confirmation
conf = :"#{name}_confirmation"
parent.optional(conf).maybe
rule(conf => [conf, name]) { |left, right| left.eql?(right) }
end
|
#each(*predicates, &block) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/dry/validation/schema/value.rb', line 34
def each(*predicates, &block)
left = array?
right =
if predicates.size > 0
inferred = predicates
.reduce(Value.new) { |a, e| a.__send__(*::Kernel.Array(e)) }
create_rule([:each, inferred.to_ast])
else
val = Value[name].instance_eval(&block)
create_rule([:each, [:set, val.rule_ast]])
end
rule = left.and(right)
add_rule(rule) if root?
rule
end
|
#root? ⇒ Boolean
21
22
23
|
# File 'lib/dry/validation/schema/value.rb', line 21
def root?
name.nil?
end
|
#rule(id = nil, **options, &block) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/dry/validation/schema/value.rb', line 68
def rule(id = nil, **options, &block)
if id
val = Value[id]
res = val.instance_exec(&block)
else
id, deps = options.to_a.first
val = Value[id]
res = val.instance_exec(*deps.map { |name| val.value(name) }, &block)
end
add_check(val.with(rules: [res.with(deps: deps || [])]))
end
|
#schema(other = nil, &block) ⇒ Object
29
30
31
32
|
# File 'lib/dry/validation/schema/value.rb', line 29
def schema(other = nil, &block)
schema = Schema.create_class(self, other, &block)
hash?.and(schema)
end
|
#value(name) ⇒ Object
89
90
91
|
# File 'lib/dry/validation/schema/value.rb', line 89
def value(name)
check(name, rules: rules)
end
|
#when(*predicates, &block) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/dry/validation/schema/value.rb', line 56
def when(*predicates, &block)
left = predicates
.reduce(Check[path, type: type]) { |a, e| a.__send__(*::Kernel.Array(e)) }
right = Value.new(type: type)
right.instance_eval(&block)
add_check(left.then(create_rule(right.to_ast)))
self
end
|