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



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

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

#visit(node) ⇒ Object



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

def visit(node)
  type, body = node
  send(:"visit_#{ type }", body)
end

#visit_array(node) ⇒ Object



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

def visit_array(node)
  member, meta = node
  registry['array'].member(visit(member)).meta(meta)
end

#visit_constructor(node) ⇒ Object



19
20
21
22
23
24
# File 'lib/dry/types/compiler.rb', line 19

def visit_constructor(node)
  definition, fn_register_name, meta = node
  fn = Dry::Types::FnContainer[fn_register_name]
  primitive = visit(definition)
  Types::Constructor.new(primitive, &fn)
end

#visit_definition(node) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/dry/types/compiler.rb', line 31

def visit_definition(node)
  type, meta = node

  if registry.registered?(type)
    registry[type].meta(meta)
  else
    Definition.new(type, meta: meta)
  end
end

#visit_form_array(node) ⇒ Object



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

def visit_form_array(node)
  member, meta = node
  registry['form.array'].member(visit(member)).meta(meta)
end

#visit_form_hash(node) ⇒ Object



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

def visit_form_hash(node)
  schema, meta = node
  merge_with('form.hash', :symbolized, schema).meta(meta)
end

#visit_hash(node) ⇒ Object



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

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

#visit_json_array(node) ⇒ Object



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

def visit_json_array(node)
  member, meta = node
  registry['json.array'].member(visit(member)).meta(meta)
end

#visit_json_hash(node) ⇒ Object



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

def visit_json_hash(node)
  schema, meta = node
  merge_with('json.hash', :symbolized, schema).meta(meta)
end

#visit_member(node) ⇒ Object



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

def visit_member(node)
  name, type = node
  { name => visit(type) }
end

#visit_safe(node) ⇒ Object



26
27
28
29
# File 'lib/dry/types/compiler.rb', line 26

def visit_safe(node)
  ast, meta = node
  Types::Safe.new(visit(ast), meta: meta)
end

#visit_sum(node) ⇒ Object



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

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