Class: Dry::Types::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/types/compiler.rb

Overview

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.

API:

  • private

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.

API:

  • private



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.

API:

  • private



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.

API:

  • private



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.

API:

  • private



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/dry/types/compiler.rb', line 121

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.

API:

  • private



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.

API:

  • private



117
118
119
# File 'lib/dry/types/compiler.rb', line 117

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.

API:

  • private



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

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.

API:

  • private



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.

API:

  • private



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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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.

API:

  • private



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

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.

API:

  • private



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dry/types/compiler.rb', line 39

def visit_nominal(node)
  type, options, meta =
    case node
    in [type, options, meta]
      node
    in [type, meta]
      [type, EMPTY_HASH, meta]
    end

  nominal_name = "nominal.#{Types.identifier(type)}"

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

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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.

API:

  • private



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

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