Class: Typed::Coercion::CoercerRegistry

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Singleton
Defined in:
lib/typed/coercion/coercer_registry.rb

Constant Summary collapse

Registry =
T.type_alias { T::Array[T.class_of(Coercer)] }
DEFAULT_COERCERS =
T.let(
  [
    StringCoercer,
    SymbolCoercer,
    BooleanCoercer,
    IntegerCoercer,
    FloatCoercer,
    DateCoercer,
    DateTimeCoercer,
    EnumCoercer,
    StructCoercer,
    TypedArrayCoercer,
    TypedHashCoercer
  ],
  Registry
)

Instance Method Summary collapse

Constructor Details

#initializeCoercerRegistry

Returns a new instance of CoercerRegistry.



32
33
34
# File 'lib/typed/coercion/coercer_registry.rb', line 32

def initialize
  @available = T.let(DEFAULT_COERCERS.clone, Registry)
end

Instance Method Details

#register(coercer) ⇒ Object



37
38
39
# File 'lib/typed/coercion/coercer_registry.rb', line 37

def register(coercer)
  @available.prepend(coercer)
end

#reset!Object



42
43
44
# File 'lib/typed/coercion/coercer_registry.rb', line 42

def reset!
  @available = DEFAULT_COERCERS.clone
end

#select_coercer_by(type:) ⇒ Object



47
48
49
# File 'lib/typed/coercion/coercer_registry.rb', line 47

def select_coercer_by(type:)
  @available.find { |coercer| coercer.new.used_for_type?(type) }
end