Class: Dry::Types::Compiler
- Inherits:
-
Object
- Object
- Dry::Types::Compiler
- Defined in:
- lib/dry/types/compiler.rb,
lib/dry/types/compat/form_types.rb
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
- #call(ast) ⇒ Object
-
#initialize(registry) ⇒ Compiler
constructor
A new instance of Compiler.
- #merge_with(hash_id, constructor, schema) ⇒ Object
- #merge_with_schema(hash_id, schema) ⇒ Object
- #visit(node) ⇒ Object
- #visit_array(node) ⇒ Object
- #visit_constrained(node) ⇒ Object
- #visit_constructor(node) ⇒ Object
- #visit_definition(node) ⇒ Object
- #visit_enum(node) ⇒ Object
- #visit_form_array(node) ⇒ Object
- #visit_form_hash(node) ⇒ Object
- #visit_hash(node) ⇒ Object
- #visit_hash_schema(node) ⇒ Object
- #visit_json_array(node) ⇒ Object
- #visit_json_hash(node) ⇒ Object
- #visit_map(node) ⇒ Object
- #visit_member(node) ⇒ Object
- #visit_params_array(node) ⇒ Object
- #visit_params_hash(node) ⇒ Object
- #visit_rule(node) ⇒ Object
- #visit_safe(node) ⇒ Object
- #visit_sum(node) ⇒ Object
Constructor Details
#initialize(registry) ⇒ Compiler
Returns a new instance of Compiler.
6 7 8 |
# File 'lib/dry/types/compiler.rb', line 6 def initialize(registry) @registry = registry end |
Instance Attribute Details
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
4 5 6 |
# File 'lib/dry/types/compiler.rb', line 4 def registry @registry end |
Instance Method Details
#call(ast) ⇒ Object
10 11 12 |
# File 'lib/dry/types/compiler.rb', line 10 def call(ast) visit(ast) end |
#merge_with(hash_id, constructor, schema) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/dry/types/compiler.rb', line 106 def merge_with(hash_id, constructor, schema) registry[hash_id].schema( schema.map { |key| visit(key) }.reduce({}, :update), constructor ) end |
#merge_with_schema(hash_id, schema) ⇒ Object
113 114 115 116 117 |
# File 'lib/dry/types/compiler.rb', line 113 def merge_with_schema(hash_id, schema) registry[hash_id].instantiate( schema.map { |key| visit(key) }.reduce({}, :update) ) end |
#visit(node) ⇒ Object
14 15 16 17 |
# File 'lib/dry/types/compiler.rb', line 14 def visit(node) type, body = node send(:"visit_#{ type }", body) end |
#visit_array(node) ⇒ Object
55 56 57 58 59 |
# File 'lib/dry/types/compiler.rb', line 55 def visit_array(node) member, = node member = member.is_a?(Class) ? member : visit(member) registry['array'].of(member).() end |
#visit_constrained(node) ⇒ Object
19 20 21 22 |
# File 'lib/dry/types/compiler.rb', line 19 def visit_constrained(node) definition, rule, = node Types::Constrained.new(visit(definition), rule: visit_rule(rule)).() end |
#visit_constructor(node) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/dry/types/compiler.rb', line 24 def visit_constructor(node) definition, fn_register_name, = node fn = Dry::Types::FnContainer[fn_register_name] primitive = visit(definition) Types::Constructor.new(primitive, meta: , fn: fn) end |
#visit_definition(node) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/dry/types/compiler.rb', line 36 def visit_definition(node) type, = node if registry.registered?(type) registry[type].() else Definition.new(type, meta: ) end end |
#visit_enum(node) ⇒ Object
96 97 98 99 |
# File 'lib/dry/types/compiler.rb', line 96 def visit_enum(node) type, mapping, = node Enum.new(visit(type), mapping: mapping, meta: ) end |
#visit_form_array(node) ⇒ Object
21 22 23 24 |
# File 'lib/dry/types/compat/form_types.rb', line 21 def visit_form_array(node) member, = node registry['params.array'].of(visit(member)).() end |
#visit_form_hash(node) ⇒ Object
16 17 18 19 |
# File 'lib/dry/types/compat/form_types.rb', line 16 def visit_form_hash(node) schema, = node merge_with('params.hash', :symbolized, schema).() end |
#visit_hash(node) ⇒ Object
61 62 63 64 |
# File 'lib/dry/types/compiler.rb', line 61 def visit_hash(node) constructor, schema, = node merge_with('hash', constructor, schema).() end |
#visit_hash_schema(node) ⇒ Object
66 67 68 69 |
# File 'lib/dry/types/compiler.rb', line 66 def visit_hash_schema(node) schema, = node merge_with_schema('hash', schema).() end |
#visit_json_array(node) ⇒ Object
76 77 78 79 |
# File 'lib/dry/types/compiler.rb', line 76 def visit_json_array(node) member, = node registry['json.array'].of(visit(member)).() end |
#visit_json_hash(node) ⇒ Object
71 72 73 74 |
# File 'lib/dry/types/compiler.rb', line 71 def visit_json_hash(node) schema, = node merge_with('json.hash', :symbolized, schema).() end |
#visit_map(node) ⇒ Object
101 102 103 104 |
# File 'lib/dry/types/compiler.rb', line 101 def visit_map(node) key_type, value_type, = node registry['hash'].map(visit(key_type), visit(value_type)).() end |
#visit_member(node) ⇒ Object
91 92 93 94 |
# File 'lib/dry/types/compiler.rb', line 91 def visit_member(node) name, type = node { name => visit(type) } end |
#visit_params_array(node) ⇒ Object
86 87 88 89 |
# File 'lib/dry/types/compiler.rb', line 86 def visit_params_array(node) member, = node registry['params.array'].of(visit(member)).() end |
#visit_params_hash(node) ⇒ Object
81 82 83 84 |
# File 'lib/dry/types/compiler.rb', line 81 def visit_params_hash(node) schema, = node merge_with('params.hash', :symbolized, schema).() end |
#visit_rule(node) ⇒ Object
46 47 48 |
# File 'lib/dry/types/compiler.rb', line 46 def visit_rule(node) Dry::Types.rule_compiler.([node])[0] end |
#visit_safe(node) ⇒ Object
31 32 33 34 |
# File 'lib/dry/types/compiler.rb', line 31 def visit_safe(node) ast, = node Types::Safe.new(visit(ast), meta: ) end |
#visit_sum(node) ⇒ Object
50 51 52 53 |
# File 'lib/dry/types/compiler.rb', line 50 def visit_sum(node) *types, = node types.map { |type| visit(type) }.reduce(:|).() end |