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?,
  BigDecimal => :decimal?
}.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.

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

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[hash?].freeze
ARRAY =

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[array?].freeze
NIL =

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[nil?].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.



174
175
176
# File 'lib/dry/schema/predicate_inferrer.rb', line 174

def initialize(registry)
  @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.



171
172
173
# File 'lib/dry/schema/predicate_inferrer.rb', line 171

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



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/dry/schema/predicate_inferrer.rb', line 183

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

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