Class: Dry::Types::Compiler Private

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#registryObject (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
16
17
# File 'lib/dry/types/compiler.rb', line 15

def call(ast)
  visit(ast)
end

#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.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dry/types/compiler.rb', line 116

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.



19
20
21
22
# File 'lib/dry/types/compiler.rb', line 19

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.



112
113
114
# File 'lib/dry/types/compiler.rb', line 112

def visit_any(meta)
  registry["any"].meta(meta)
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.



61
62
63
64
65
# File 'lib/dry/types/compiler.rb', line 61

def visit_array(node)
  member, meta = node
  member = member.is_a?(Class) ? member : visit(member)
  registry["nominal.array"].of(member).meta(meta)
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.



24
25
26
27
28
# File 'lib/dry/types/compiler.rb', line 24

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.



30
31
32
33
34
# File 'lib/dry/types/compiler.rb', line 30

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.



102
103
104
105
# File 'lib/dry/types/compiler.rb', line 102

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.



67
68
69
70
# File 'lib/dry/types/compiler.rb', line 67

def visit_hash(node)
  opts, meta = node
  registry["nominal.hash"].with(**opts, meta: 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.



82
83
84
85
# File 'lib/dry/types/compiler.rb', line 82

def visit_json_array(node)
  member, meta = node
  registry["json.array"].of(visit(member)).meta(meta)
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.



77
78
79
80
# File 'lib/dry/types/compiler.rb', line 77

def visit_json_hash(node)
  keys, meta = node
  registry["json.hash"].schema(keys.map { |key| visit(key) }, meta)
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.



97
98
99
100
# File 'lib/dry/types/compiler.rb', line 97

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.



36
37
38
# File 'lib/dry/types/compiler.rb', line 36

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.



107
108
109
110
# File 'lib/dry/types/compiler.rb', line 107

def visit_map(node)
  key_type, value_type, meta = node
  registry["nominal.hash"].map(visit(key_type), visit(value_type)).meta(meta)
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.



41
42
43
44
45
46
47
48
49
50
# File 'lib/dry/types/compiler.rb', line 41

def visit_nominal(node)
  type, meta = node
  nominal_name = "nominal.#{Types.identifier(type)}"

  if registry.registered?(nominal_name)
    registry[nominal_name].meta(meta)
  else
    Nominal.new(type, meta: 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.



92
93
94
95
# File 'lib/dry/types/compiler.rb', line 92

def visit_params_array(node)
  member, meta = node
  registry["params.array"].of(visit(member)).meta(meta)
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.



87
88
89
90
# File 'lib/dry/types/compiler.rb', line 87

def visit_params_hash(node)
  keys, meta = node
  registry["params.hash"].schema(keys.map { |key| visit(key) }, meta)
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.



52
53
54
# File 'lib/dry/types/compiler.rb', line 52

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.



72
73
74
75
# File 'lib/dry/types/compiler.rb', line 72

def visit_schema(node)
  keys, options, meta = node
  registry["nominal.hash"].schema(keys.map { |key| visit(key) }).with(**options, meta: 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.



56
57
58
59
# File 'lib/dry/types/compiler.rb', line 56

def visit_sum(node)
  *types, meta = node
  types.map { |type| visit(type) }.reduce(:|).meta(meta)
end