Class: Dry::Validation::InputProcessorCompiler
- Inherits:
-
Object
- Object
- Dry::Validation::InputProcessorCompiler
- Defined in:
- lib/dry/validation/input_processor_compiler.rb
Defined Under Namespace
Constant Summary collapse
- DEFAULT_TYPE_NODE =
[[:type, 'string']].freeze
Instance Attribute Summary collapse
-
#type_compiler ⇒ Object
readonly
Returns the value of attribute type_compiler.
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) ⇒ Object
- #visit_key(node, *args) ⇒ Object
- #visit_or(node, *args) ⇒ Object
- #visit_predicate(node, *args) ⇒ Object
- #visit_schema(node, *args) ⇒ Object
- #visit_set(node) ⇒ Object
- #visit_val(node, *args) ⇒ Object
Constructor Details
#initialize ⇒ InputProcessorCompiler
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 (readonly)
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
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 83 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
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 36 def visit_and(node, first = true) if first name, type = node.map { |n| visit(n, false) }.uniq [:key, [name, type]] else result = node.map { |n| visit(n, first) }.uniq if result.size == 1 result.first else (result - self.class::DEFAULT_TYPE_NODE).first end end end |
#visit_each(node, *args) ⇒ Object
69 70 71 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 69 def visit_each(node, *args) array_node(visit(node, *args)) end |
#visit_implication(node) ⇒ Object
51 52 53 54 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 51 def visit_implication(node) key, types = node [:key, [visit(key), visit(types, false)]] end |
#visit_key(node, *args) ⇒ Object
56 57 58 59 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 56 def visit_key(node, *args) _, other = node visit(other, *args) end |
#visit_or(node, *args) ⇒ Object
31 32 33 34 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 31 def visit_or(node, *args) left, right = node [:sum, [visit(left, *args), visit(right, *args)]] end |
#visit_predicate(node, *args) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 73 def visit_predicate(node, *args) id, args = node if id == :key? args[0] else type(id, args) end end |
#visit_schema(node, *args) ⇒ Object
27 28 29 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 27 def visit_schema(node, *args) hash_node(node.input_processor_ast(identifier)) end |
#visit_set(node) ⇒ Object
65 66 67 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 65 def visit_set(node, *) hash_node(node.map { |n| visit(n) }) end |
#visit_val(node, *args) ⇒ Object
61 62 63 |
# File 'lib/dry/validation/input_processor_compiler.rb', line 61 def visit_val(node, *args) visit(node, *args) end |