Class: Dry::Schema::PredicateInferrer::Compiler Private

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/schema/predicate_inferrer.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.

Compiler reduces type AST into a list of predicates

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.



37
38
39
# File 'lib/dry/schema/predicate_inferrer.rb', line 37

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.



34
35
36
# File 'lib/dry/schema/predicate_inferrer.rb', line 34

def registry
  @registry
end

Instance Method Details

#infer_predicate(type) ⇒ 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.



42
43
44
# File 'lib/dry/schema/predicate_inferrer.rb', line 42

def infer_predicate(type)
  TYPE_TO_PREDICATE.fetch(type) { :"#{type.name.split('::').last.downcase}?" }
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.



47
48
49
50
# File 'lib/dry/schema/predicate_inferrer.rb', line 47

def visit(node)
  meth, rest = node
  public_send(:"visit_#{meth}", rest)
end

#visit_array(_) ⇒ 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.



70
71
72
# File 'lib/dry/schema/predicate_inferrer.rb', line 70

def visit_array(_)
  :array?
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.



106
107
108
109
# File 'lib/dry/schema/predicate_inferrer.rb', line 106

def visit_constrained(node)
  other, * = node
  visit(other)
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.



81
82
83
84
# File 'lib/dry/schema/predicate_inferrer.rb', line 81

def visit_constructor(node)
  other, * = node
  visit(other)
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.



87
88
89
90
# File 'lib/dry/schema/predicate_inferrer.rb', line 87

def visit_enum(node)
  other, * = node
  visit(other)
end

#visit_hash(_) ⇒ 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.



65
66
67
# File 'lib/dry/schema/predicate_inferrer.rb', line 65

def visit_hash(_)
  :hash?
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.



53
54
55
56
57
58
59
60
61
62
# File 'lib/dry/schema/predicate_inferrer.rb', line 53

def visit_nominal(node)
  type = node[0]
  predicate = infer_predicate(type)

  if registry.key?(predicate)
    predicate
  else
    { type?: type }
  end
end

#visit_safe(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.



75
76
77
78
# File 'lib/dry/schema/predicate_inferrer.rb', line 75

def visit_safe(node)
  other, * = node
  visit(other)
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.



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dry/schema/predicate_inferrer.rb', line 93

def visit_sum(node)
  left, right = node

  predicates = [visit(left), visit(right)]

  if predicates.first == :nil?
    predicates[1..predicates.size - 1]
  else
    predicates
  end
end