Class: Dry::Data::Compiler

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/data/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/data/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/data/compiler.rb', line 4

def registry
  @registry
end

Instance Method Details

#call(ast) ⇒ Object



10
11
12
# File 'lib/dry/data/compiler.rb', line 10

def call(ast)
  visit(ast)
end

#visit(node, *args) ⇒ Object



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

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

#visit_array(node) ⇒ Object



33
34
35
# File 'lib/dry/data/compiler.rb', line 33

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

#visit_form_array(node) ⇒ Object



37
38
39
# File 'lib/dry/data/compiler.rb', line 37

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

#visit_form_hash(node) ⇒ Object



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

def visit_form_hash(node)
  constructor, schema = node
  registry['form.hash'].public_send(constructor, schema.map { |key| visit(key) }.reduce(:merge))
end

#visit_hash(node) ⇒ Object



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

def visit_hash(node)
  constructor, schema = node
  registry['hash'].public_send(constructor, schema.map { |key| visit(key) }.reduce(:merge))
end

#visit_key(node) ⇒ Object



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

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

#visit_sum(node) ⇒ Object



29
30
31
# File 'lib/dry/data/compiler.rb', line 29

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

#visit_type(node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/dry/data/compiler.rb', line 18

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

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