Module: CMDx::Validators::Validate

Extended by:
Validate
Included in:
Validate
Defined in:
lib/cmdx/validators/validate.rb

Overview

Invokes an inline ‘:validate` handler. Used by #validate for each handler passed under the `:validate` option.

Instance Method Summary collapse

Instance Method Details

#call(task, value, handler) ⇒ Validators::Failure, ...

Returns handler’s return value.

Parameters:

  • task (Task)

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

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

Returns:

Raises:

  • (ArgumentError)

    when ‘handler` isn’t a supported type



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cmdx/validators/validate.rb', line 16

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, "validate handler must be a Symbol, Proc, or respond to #call"
  end
end