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



59
60
61
62
63
# File 'lib/dry/types/compiler.rb', line 59

def visit_array(node)
  member, meta = node
  member = visit(member) unless member.is_a?(::Class)
  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.



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



80
81
82
83
# File 'lib/dry/types/compiler.rb', line 80

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.



75
76
77
78
# File 'lib/dry/types/compiler.rb', line 75

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.



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



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

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.



90
91
92
93
# File 'lib/dry/types/compiler.rb', line 90

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.



85
86
87
88
# File 'lib/dry/types/compiler.rb', line 85

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.



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



54
55
56
57
# File 'lib/dry/types/compiler.rb', line 54

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