Class: Dry::Types::PredicateInferrer::Compiler Private

Inherits:
Object
  • Object
show all
Extended by:
Core::ClassAttributes
Defined in:
lib/dry/types/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.



49
50
51
# File 'lib/dry/types/predicate_inferrer.rb', line 49

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:



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

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.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/dry/types/predicate_inferrer.rb', line 54

def infer_predicate(type) # rubocop:disable Metrics/PerceivedComplexity
  pred = TYPE_TO_PREDICATE.fetch(type) do
    if type.name.nil? || self.class.infer_predicate_by_class_name.equal?(false)
      nil
    else
      candidate = :"#{type.name.split("::").last.downcase}?"

      if registry.key?(candidate)
        if self.class.infer_predicate_by_class_name
          candidate
        else
          raise ::KeyError, <<~MESSAGE
            Automatic predicate inferring from class names is deprecated
            and will be removed in dry-types 2.0.
            Use `Dry::Types::PredicateInferrer::Compiler.infer_predicate_by_class_name true`
            to restore the previous behavior
            or `Dry::Types::PredicateInferrer::Compiler.infer_predicate_by_class_name false`
            to explicitly opt-out (i.e. no exception + no inferring).
            Note: for dry-schema and dry-validation use Dry::Schema::PredicateInferrer::Compiler.
          MESSAGE
        end
      else
        nil
      end
    end
  end

  if pred.nil?
    EMPTY_ARRAY
  else
    [pred]
  end
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.



89
90
91
92
# File 'lib/dry/types/predicate_inferrer.rb', line 89

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.



165
166
167
168
# File 'lib/dry/types/predicate_inferrer.rb', line 165

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.



160
161
162
# File 'lib/dry/types/predicate_inferrer.rb', line 160

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.



113
114
115
# File 'lib/dry/types/predicate_inferrer.rb', line 113

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.



148
149
150
151
152
153
154
155
156
157
# File 'lib/dry/types/predicate_inferrer.rb', line 148

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.



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

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.



129
130
131
132
# File 'lib/dry/types/predicate_inferrer.rb', line 129

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

#visit_hash(_) ⇒ Object Also known as: visit_schema

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.



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

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.



118
119
120
# File 'lib/dry/types/predicate_inferrer.rb', line 118

def visit_lax(node)
  visit(node)
end

#visit_map(_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.

Raises:

  • (NotImplementedError)


189
190
191
# File 'lib/dry/types/predicate_inferrer.rb', line 189

def visit_map(_node)
  raise NotImplementedError, "map types are not supported yet"
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.



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

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

  if !predicate.empty? && 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.



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/dry/types/predicate_inferrer.rb', line 171

def visit_predicate(node)
  pred, args = node

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

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



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dry/types/predicate_inferrer.rb', line 135

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