Module: CMDx::Coercions::Symbol

Extended by:
Symbol
Included in:
Symbol
Defined in:
lib/cmdx/coercions/symbol.rb

Overview

Coerces to Symbol via ‘#to_s.to_sym`. Fails only when `value` has no `#to_s` (i.e. `BasicObject` instances).

Instance Method Summary collapse

Instance Method Details

#call(value, options = EMPTY_HASH) ⇒ Symbol, Coercions::Failure

Parameters:

  • value (Object)
  • options (Hash{Symbol => Object}) (defaults to: EMPTY_HASH)

Options Hash (options):

  • reserved (Object)

    for future per-coercion configuration (currently ignored)

Returns:



15
16
17
18
19
20
21
22
# File 'lib/cmdx/coercions/symbol.rb', line 15

def call(value, options = EMPTY_HASH)
  return value if value.is_a?(::Symbol)

  value.to_s.to_sym
rescue NoMethodError
  type = I18nProxy.t("cmdx.types.symbol")
  Failure.new(I18nProxy.t("cmdx.coercions.into_a", type:))
end