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.



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

def initialize(registry)
  @registry = registry
end

Instance Attribute Details

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

Returns:



40
41
42
# File 'lib/dry/schema/predicate_inferrer.rb', line 40

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.



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

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.



53
54
55
56
# File 'lib/dry/schema/predicate_inferrer.rb', line 53

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

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



128
129
130
131
# File 'lib/dry/schema/predicate_inferrer.rb', line 128

def visit_and(node)
  left, right = node
  visit(left) + visit(right)
end

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



123
124
125
# File 'lib/dry/schema/predicate_inferrer.rb', line 123

def visit_any(_)
  EMPTY_ARRAY
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.



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

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.



111
112
113
114
115
116
117
118
119
120
# File 'lib/dry/schema/predicate_inferrer.rb', line 111

def visit_constrained(node)
  other, rules = node
  predicates = visit(rules)

  if predicates.empty?
    visit(other)
  else
    [*visit(other), *merge_predicates(predicates)]
  end
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.



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

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.



92
93
94
95
# File 'lib/dry/schema/predicate_inferrer.rb', line 92

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.



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

def visit_hash(_)
  HASH
end

#visit_lax(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
# File 'lib/dry/schema/predicate_inferrer.rb', line 81

def visit_lax(node)
  visit(node)
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.



59
60
61
62
63
64
65
66
67
68
# File 'lib/dry/schema/predicate_inferrer.rb', line 59

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

  if registry.key?(predicate[0])
    predicate
  else
    [type?: type]
  end
end

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dry/schema/predicate_inferrer.rb', line 134

def visit_predicate(node)
  pred, args = node

  if pred.equal?(:type?)
    EMPTY_ARRAY
  elsif registry.key?(pred)
    *curried, _ = args
    values = curried.map { |_, v| v }

    if values.empty?
      [pred]
    else
      [pred => values[0]]
    end
  else
    EMPTY_ARRAY
  end
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.



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dry/schema/predicate_inferrer.rb', line 98

def visit_sum(node)
  left_node, right_node, = node
  left = visit(left_node)
  right = visit(right_node)

  if left.eql?(NIL)
    right
  else
    [[left, right]]
  end
end