Class: Dry::Types::Compiler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

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



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

def merge_with(hash_id, constructor, schema)
  registry[hash_id].__send__(
    constructor, schema.map { |key| visit(key) }.reduce({}, :merge)
  )
end

#visit(node, *args) ⇒ Object



14
15
16
# File 'lib/dry/types/compiler.rb', line 14

def visit(node, *args)
  send(:"visit_#{node[0]}", node[1], *args)
end

#visit_array(node) ⇒ Object



38
39
40
# File 'lib/dry/types/compiler.rb', line 38

def visit_array(node)
  registry['array'].member(call(node))
end

#visit_constructor(node) ⇒ Object



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

def visit_constructor(node)
  primitive, fn = node
  Types::Constructor.new(primitive, &fn)
end

#visit_form_array(node) ⇒ Object



42
43
44
# File 'lib/dry/types/compiler.rb', line 42

def visit_form_array(node)
  registry['form.array'].member(call(node))
end

#visit_form_hash(node) ⇒ Object



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

def visit_form_hash(node)
  if node
    constructor, schema = node
    merge_with('form.hash', constructor, schema)
  else
    registry['form.hash']
  end
end

#visit_hash(node) ⇒ Object



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

def visit_hash(node)
  constructor, schema = node
  merge_with('hash', constructor, schema)
end

#visit_json_array(node) ⇒ Object



46
47
48
# File 'lib/dry/types/compiler.rb', line 46

def visit_json_array(node)
  registry['json.array'].member(call(node))
end

#visit_json_hash(node) ⇒ Object



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

def visit_json_hash(node)
  if node
    constructor, schema = node
    merge_with('json.hash', constructor, schema)
  else
    registry['json.hash']
  end
end

#visit_key(node) ⇒ Object



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

def visit_key(node)
  name, types = node
  { name => visit(types) }
end

#visit_sum(node) ⇒ Object



34
35
36
# File 'lib/dry/types/compiler.rb', line 34

def visit_sum(node)
  node.map { |type| visit(type) }.reduce(:|)
end

#visit_type(node) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/dry/types/compiler.rb', line 23

def visit_type(node)
  type, args = node
  meth = :"visit_#{type.tr('.', '_')}"

  if respond_to?(meth) && args
    send(meth, args)
  else
    registry[type]
  end
end