Class: Dry::Validation::InputProcessorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::InputProcessorCompiler
show all
- Defined in:
- lib/dry/validation/input_processor_compiler.rb
Defined Under Namespace
Classes: Form, JSON, Sanitizer
Constant Summary
collapse
- DEFAULT_TYPE_NODE =
[[:type, 'string']].freeze
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#call(ast) ⇒ Object
-
#initialize ⇒ InputProcessorCompiler
constructor
A new instance of InputProcessorCompiler.
-
#schema_ast(ast) ⇒ Object
-
#type(predicate, args) ⇒ Object
-
#visit(node, *args) ⇒ Object
-
#visit_and(node, first = true) ⇒ Object
-
#visit_each(node, *args) ⇒ Object
-
#visit_implication(node, *args) ⇒ Object
-
#visit_key(node, *args) ⇒ Object
-
#visit_not(node, *args) ⇒ Object
-
#visit_or(node, *args) ⇒ Object
-
#visit_predicate(node) ⇒ Object
-
#visit_rule(node, *args) ⇒ Object
-
#visit_schema(schema, *args) ⇒ Object
-
#visit_set(node) ⇒ Object
-
#visit_type(type, *args) ⇒ Object
-
#visit_val(node, *args) ⇒ Object
Constructor Details
Returns a new instance of InputProcessorCompiler.
11
12
13
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 11
def initialize
@type_compiler = Dry::Types::Compiler.new(Dry::Types)
end
|
Instance Attribute Details
#type_compiler ⇒ Object
Returns the value of attribute type_compiler.
7
8
9
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 7
def type_compiler
@type_compiler
end
|
Instance Method Details
#call(ast) ⇒ Object
15
16
17
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 15
def call(ast)
type_compiler.(hash_node(schema_ast(ast)))
end
|
#schema_ast(ast) ⇒ Object
19
20
21
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 19
def schema_ast(ast)
ast.map { |node| visit(node) }
end
|
#type(predicate, args) ⇒ Object
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 114
def type(predicate, args)
default = self.class::PREDICATE_MAP[:default]
if predicate == :type?
const = args[0]
[:type, self.class::CONST_MAP[const] || Types.identifier(const)]
else
[:type, self.class::PREDICATE_MAP[predicate] || default]
end
end
|
#visit(node, *args) ⇒ Object
23
24
25
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 23
def visit(node, *args)
send(:"visit_#{node[0]}", node[1], *args)
end
|
#visit_and(node, first = true) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 51
def visit_and(node, first = true)
if first
name, type = node.map { |n| visit(n, false) }.uniq
if name.is_a?(Array)
type
else
[:key, [name, type]]
end
else
result = node.map { |n| visit(n, first) }.uniq
if result.size == 1
result.first
else
(result - self.class::DEFAULT_TYPE_NODE).last
end
end
end
|
#visit_each(node, *args) ⇒ Object
100
101
102
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 100
def visit_each(node, *args)
array_node(visit(node, *args))
end
|
#visit_implication(node, *args) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 71
def visit_implication(node, *args)
left, right = node
key = visit(left)
if key.is_a?(Symbol)
[:key, [key, visit(right, false)]]
else
[:sum, [key, visit(right, false)]]
end
end
|
#visit_key(node, *args) ⇒ Object
87
88
89
90
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 87
def visit_key(node, *args)
_, other = node
visit(other, *args)
end
|
#visit_not(node, *args) ⇒ Object
83
84
85
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 83
def visit_not(node, *args)
visit(node, *args)
end
|
#visit_or(node, *args) ⇒ Object
46
47
48
49
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 46
def visit_or(node, *args)
left, right = node
[:sum, [visit(left, *args), visit(right, *args)]]
end
|
#visit_predicate(node) ⇒ Object
104
105
106
107
108
109
110
111
112
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 104
def visit_predicate(node, *)
id, args = node
if id == :key?
args[0][1]
else
type(id, args.map(&:last))
end
end
|
#visit_rule(node, *args) ⇒ Object
27
28
29
30
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 27
def visit_rule(node, *args)
_, rule = node
visit(rule, *args)
end
|
#visit_schema(schema, *args) ⇒ Object
42
43
44
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 42
def visit_schema(schema, *args)
hash_node(schema.input_processor_ast(identifier))
end
|
#visit_set(node) ⇒ Object
96
97
98
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 96
def visit_set(node, *)
hash_node(node.map { |n| visit(n) })
end
|
#visit_type(type, *args) ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 32
def visit_type(type, *args)
if type.is_a?(Types::Constructor)
[:constructor, [type.primitive, type.fn]]
elsif type.respond_to?(:rule)
visit(type.rule.to_ast, *args)
else
DEFAULT_TYPE_NODE.first
end
end
|
#visit_val(node, *args) ⇒ Object
92
93
94
|
# File 'lib/dry/validation/input_processor_compiler.rb', line 92
def visit_val(node, *args)
visit(node, *args)
end
|