Class: Dry::Types::PredicateInferrer

Inherits:
Object
  • Object
show all
Extended by:
Core::Cache
Defined in:
lib/dry/types/predicate_inferrer.rb

Overview

PredicateInferrer returns the list of predicates used by a type.

Defined Under Namespace

Classes: Compiler

Constant Summary collapse

TYPE_TO_PREDICATE =
{
  ::DateTime => :date_time?,
  ::Date => :date?,
  ::Time => :time?,
  ::FalseClass => :false?,
  ::Integer => :int?,
  ::Float => :float?,
  ::NilClass => :nil?,
  ::String => :str?,
  ::TrueClass => :true?,
  ::BigDecimal => :decimal?,
  ::Array => :array?
}.freeze
REDUCED_TYPES =
{
  [[[:true?], [:false?]]] => %i[bool?]
}.freeze
HASH =
%i[hash?].freeze
ARRAY =
%i[array?].freeze
NIL =
%i[nil?].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = PredicateRegistry.new) ⇒ 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.



214
215
216
# File 'lib/dry/types/predicate_inferrer.rb', line 214

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

Instance Attribute Details

#compilerCompiler (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:



211
212
213
# File 'lib/dry/types/predicate_inferrer.rb', line 211

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

Parameters:

Returns:

  • (Symbol)


224
225
226
227
228
229
230
231
232
233
234
# File 'lib/dry/types/predicate_inferrer.rb', line 224

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

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