Class: Dry::Types::Compiler Private
- Inherits:
-
Object
- Object
- Dry::Types::Compiler
- Defined in:
- lib/dry/types/compiler.rb
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Instance Attribute Summary collapse
- #registry ⇒ Object readonly private
Instance Method Summary collapse
- #call(ast) ⇒ Object private
- #compile_fn(fn) ⇒ Object private
-
#initialize(registry) ⇒ Compiler
constructor
private
A new instance of Compiler.
- #visit(node) ⇒ Object private
- #visit_any(meta) ⇒ Object private
- #visit_array(node) ⇒ Object private
- #visit_constrained(node) ⇒ Object private
- #visit_constructor(node) ⇒ Object private
- #visit_enum(node) ⇒ Object private
- #visit_hash(node) ⇒ Object private
- #visit_json_array(node) ⇒ Object private
- #visit_json_hash(node) ⇒ Object private
- #visit_key(node) ⇒ Object private
- #visit_lax(node) ⇒ Object private
- #visit_map(node) ⇒ Object private
- #visit_nominal(node) ⇒ Object private
- #visit_params_array(node) ⇒ Object private
- #visit_params_hash(node) ⇒ Object private
- #visit_rule(node) ⇒ Object private
- #visit_schema(node) ⇒ Object private
- #visit_sum(node) ⇒ Object private
Constructor Details
#initialize(registry) ⇒ Compiler
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Compiler.
11 12 13 |
# File 'lib/dry/types/compiler.rb', line 11 def initialize(registry) @registry = registry end |
Instance Attribute Details
#registry ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
9 10 11 |
# File 'lib/dry/types/compiler.rb', line 9 def registry @registry end |
Instance Method Details
#call(ast) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
15 |
# File 'lib/dry/types/compiler.rb', line 15 def call(ast) = visit(ast) |
#compile_fn(fn) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/dry/types/compiler.rb', line 114 def compile_fn(fn) type, *node = fn case type when :id ::Dry::Types::FnContainer[node.fetch(0)] when :callable node.fetch(0) when :method target, method = node target.method(method) else raise ::ArgumentError, "Cannot build callable from #{fn.inspect}" end end |
#visit(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
17 18 19 20 |
# File 'lib/dry/types/compiler.rb', line 17 def visit(node) type, body = node send(:"visit_#{type}", body) end |
#visit_any(meta) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
110 111 112 |
# File 'lib/dry/types/compiler.rb', line 110 def visit_any() registry["any"].() end |
#visit_array(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
59 60 61 62 63 |
# File 'lib/dry/types/compiler.rb', line 59 def visit_array(node) member, = node member = visit(member) unless member.is_a?(::Class) registry["nominal.array"].of(member).() end |
#visit_constrained(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
22 23 24 25 26 |
# File 'lib/dry/types/compiler.rb', line 22 def visit_constrained(node) nominal, rule = node type = visit(nominal) type.constrained_type.new(type, rule: visit_rule(rule)) end |
#visit_constructor(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
28 29 30 31 32 |
# File 'lib/dry/types/compiler.rb', line 28 def visit_constructor(node) nominal, fn = node primitive = visit(nominal) primitive.constructor(compile_fn(fn)) end |
#visit_enum(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
100 101 102 103 |
# File 'lib/dry/types/compiler.rb', line 100 def visit_enum(node) type, mapping = node Enum.new(visit(type), mapping: mapping) end |
#visit_hash(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
65 66 67 68 |
# File 'lib/dry/types/compiler.rb', line 65 def visit_hash(node) opts, = node registry["nominal.hash"].with(**opts, meta: ) end |
#visit_json_array(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
80 81 82 83 |
# File 'lib/dry/types/compiler.rb', line 80 def visit_json_array(node) member, = node registry["json.array"].of(visit(member)).() end |
#visit_json_hash(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
75 76 77 78 |
# File 'lib/dry/types/compiler.rb', line 75 def visit_json_hash(node) keys, = node registry["json.hash"].schema(keys.map { |key| visit(key) }, ) end |
#visit_key(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
95 96 97 98 |
# File 'lib/dry/types/compiler.rb', line 95 def visit_key(node) name, required, type = node Schema::Key.new(visit(type), name, required: required) end |
#visit_lax(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
34 35 36 |
# File 'lib/dry/types/compiler.rb', line 34 def visit_lax(node) Types::Lax.new(visit(node)) end |
#visit_map(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
105 106 107 108 |
# File 'lib/dry/types/compiler.rb', line 105 def visit_map(node) key_type, value_type, = node registry["nominal.hash"].map(visit(key_type), visit(value_type)).() end |
#visit_nominal(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dry/types/compiler.rb', line 39 def visit_nominal(node) type, = node nominal_name = "nominal.#{Types.identifier(type)}" if registry.registered?(nominal_name) registry[nominal_name].() else Nominal.new(type, meta: ) end end |
#visit_params_array(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
90 91 92 93 |
# File 'lib/dry/types/compiler.rb', line 90 def visit_params_array(node) member, = node registry["params.array"].of(visit(member)).() end |
#visit_params_hash(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
85 86 87 88 |
# File 'lib/dry/types/compiler.rb', line 85 def visit_params_hash(node) keys, = node registry["params.hash"].schema(keys.map { |key| visit(key) }, ) end |
#visit_rule(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
50 51 52 |
# File 'lib/dry/types/compiler.rb', line 50 def visit_rule(node) ::Dry::Types.rule_compiler.([node])[0] end |
#visit_schema(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
70 71 72 73 |
# File 'lib/dry/types/compiler.rb', line 70 def visit_schema(node) keys, , = node registry["nominal.hash"].schema(keys.map { |key| visit(key) }).with(**, meta: ) end |
#visit_sum(node) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
54 55 56 57 |
# File 'lib/dry/types/compiler.rb', line 54 def visit_sum(node) *types, = node types.map { |type| visit(type) }.reduce(:|).() end |