Class: Dry::Types::PredicateInferrer
- Inherits:
-
Object
- Object
- Dry::Types::PredicateInferrer
- 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
- #compiler ⇒ Compiler readonly private
Instance Method Summary collapse
-
#[](type) ⇒ Symbol
private
Infer predicate identifier from the provided type.
-
#initialize(registry = PredicateRegistry.new) ⇒ PredicateInferrer
constructor
private
A new instance of PredicateInferrer.
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
#compiler ⇒ Compiler (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.
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
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 |