Module: CMDx::Coercions::Symbol

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

Overview

Coerces values to Symbol type using Ruby’s to_sym method.

This coercion handles various input types by converting them to symbols. It provides error handling for values that cannot be converted to symbols and raises appropriate CMDx coercion errors with localized messages.

Instance Method Summary collapse

Instance Method Details

#call(value, options = {}) ⇒ Symbol

Coerces a value to Symbol type.

Examples:

Basic symbol coercion

Symbol.call("hello")           # => :hello
Symbol.call("user_id")         # => :user_id
Symbol.call("")                # => :""
Symbol.call(:existing)         # => :existing

Parameters:

  • value (Object)

    The value to coerce to a symbol

  • options (Hash) (defaults to: {})

    Optional configuration parameters (unused in this coercion)

Options Hash (options):

  • :* (Object)

    Any configuration option (unused)

Returns:

  • (Symbol)

    The coerced symbol value

Raises:

  • (CoercionError)

    If the value cannot be converted to a symbol



31
32
33
34
35
36
# File 'lib/cmdx/coercions/symbol.rb', line 31

def call(value, options = {})
  value.to_sym
rescue NoMethodError
  type = Locale.t("cmdx.types.symbol")
  raise CoercionError, Locale.t("cmdx.coercions.into_a", type:)
end