Module: CMDx::Coercions::Coerce

Extended by:
Coerce
Included in:
Coerce
Defined in:
lib/cmdx/coercions/coerce.rb

Overview

Invokes an inline ‘:coerce` handler (Symbol method name, Proc, or anything with `#call`). Used by #coerce for non-built-in rules.

Instance Method Summary collapse

Instance Method Details

#call(task, value, handler) ⇒ Object

Returns the handler’s return value.

Parameters:

  • task (Task)

    receiver for Symbol/Proc handlers, also passed to callable handlers

  • value (Object)
  • handler (Symbol, Proc, #call)

Returns:

  • (Object)

    the handler’s return value

Raises:

  • (ArgumentError)

    when ‘handler` isn’t a supported type



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cmdx/coercions/coerce.rb', line 17

def call(task, value, handler)
  case handler
  when ::Symbol
    task.send(handler, value)
  when ::Proc
    task.instance_exec(value, &handler)
  else
    return handler.call(value, task) if handler.respond_to?(:call)

    raise ArgumentError, "coerce handler must be a Symbol, Proc, or respond to #call"
  end
end