Class: Typed::Coercion::SymbolCoercer

Inherits:
Coercer
  • Object
show all
Extended by:
T::Generic
Defined in:
lib/typed/coercion/symbol_coercer.rb

Constant Summary collapse

Target =
type_member { {fixed: Symbol} }

Instance Method Summary collapse

Instance Method Details

#coerce(type:, value:) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/typed/coercion/symbol_coercer.rb', line 16

def coerce(type:, value:)
  return Failure.new(CoercionError.new("Type must be a Symbol.")) unless used_for_type?(type)

  if value.respond_to?(:to_sym)
    Success.new(value.to_sym)
  else
    Failure.new(CoercionError.new("Value cannot be coerced into Symbol. Consider adding a #to_sym implementation."))
  end
end

#used_for_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/typed/coercion/symbol_coercer.rb', line 11

def used_for_type?(type)
  type == T::Utils.coerce(Symbol)
end