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_hash(node) ⇒ Object



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

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

#visit_key(node) ⇒ Object



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

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

#visit_sum(node) ⇒ Object



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

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

#visit_type(node) ⇒ Object



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

def visit_type(node)
  type, args = node

  if respond_to?(:"visit_#{type}")
    send(:"visit_#{type}", args)
  else
    registry[type]
  end
end