Class: Dry::Schema::PredicateInferrer Private

Inherits:
Object
  • Object
show all
Extended by:
Core::Cache
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.

PredicateInferrer is used internally by ‘Macros::Value` for inferring type-check predicates from type specs.

Constant Summary collapse

TYPE_TO_PREDICATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Hash.new do |hash, type|
  primitive = type.meta[:maybe] ? type.right.primitive : type.primitive

  if hash.key?(primitive)
    hash[primitive]
  else
    :"#{primitive.name.split('::').last.downcase}?"
  end
end

Class Method Summary collapse

Class Method Details

.[](type) ⇒ Symbol

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.

Infer predicate identifier from the provided type

Returns:

  • (Symbol)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dry/schema/predicate_inferrer.rb', line 35

def self.[](type)
  fetch_or_store(type.hash) {
    predicates =
      if type.is_a?(Dry::Types::Sum) && !type.meta[:maybe]
        [self[type.left], self[type.right]]
      else
        TYPE_TO_PREDICATE[type]
      end

    Array(predicates).flatten
  }
end