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.

Defined Under Namespace

Classes: Compiler

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.

{
  DateTime => :date_time?,
  FalseClass => :false?,
  Integer => :int?,
  NilClass => :nil?,
  String => :str?,
  TrueClass => :true?
}.freeze
REDUCED_TYPES =

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.

{
  %i[true? false?] => :bool?
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry) ⇒ PredicateInferrer

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 PredicateInferrer.



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

def initialize(registry)
  @compiler = Compiler.new(registry)
end

Instance Attribute Details

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



115
116
117
# File 'lib/dry/schema/predicate_inferrer.rb', line 115

def compiler
  @compiler
end

Instance 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)


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dry/schema/predicate_inferrer.rb', line 127

def [](type)
  self.class.fetch_or_store(type.hash) do
    predicates = compiler.visit(type.to_ast)

    if predicates.is_a?(Hash)
      predicates
    else
      Array(REDUCED_TYPES[predicates] || predicates).flatten
    end
  end
end