Module: SimpleCommandDispatcher::Commands::CommandCallable

Defined in:
lib/simple_command_dispatcher/commands/command_callable.rb,
lib/simple_command_dispatcher/commands/utils.rb,
lib/simple_command_dispatcher/commands/errors.rb

Overview

CommandCallable provides a standardized interface for command objects with built-in success/failure tracking and error handling.

When prepended to a command class, it:

  • Adds a class-level ‘.call` method that instantiates and executes the command

  • Tracks command execution with ‘success?` and `failure?` methods

  • Provides error collection via the ‘errors` object

  • Stores the command’s return value in ‘result`

Examples:

Basic usage

class AuthenticateUser
  prepend SimpleCommandDispatcher::Commands::CommandCallable

  def initialize(email:, password:)
    @email = email
    @password = password
  end

  def call
    return nil unless user = User.find_by(email: @email)
    return nil unless user.authenticate(@password)

    user
  end
end

command = AuthenticateUser.call(email: '[email protected]', password: 'secret')
command.success? # => true if user found and authenticated
command.result   # => User object or nil

Defined Under Namespace

Modules: ClassMethods, Utils Classes: Errors, NotImplementedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#resultObject (readonly)

Returns the return value from the command’s call method.

Returns:

  • (Object)

    the return value from the command’s call method



38
39
40
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 38

def result
  @result
end

Class Method Details

.prepended(base) ⇒ Object



51
52
53
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 51

def self.prepended(base)
  base.extend ClassMethods
end

Instance Method Details

#callself

Executes the command by calling super (your command’s implementation). Tracks execution state and stores the result.

Returns:

  • (self)

    the command instance for method chaining

Raises:



60
61
62
63
64
65
66
67
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 60

def call
  raise NotImplementedError unless defined?(super)

  @called = true
  @result = super

  self
end

#errorsErrors

Returns the errors collection for this command. If the command class defines its own errors method, that will be used instead.

Returns:

  • (Errors)

    the errors collection



88
89
90
91
92
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 88

def errors
  return super if defined?(super)

  @errors ||= Errors.new
end

#failure?Boolean

Returns true if the command was called but has errors.

Returns:

  • (Boolean)

    true if called and errors are present



80
81
82
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 80

def failure?
  called? && errors.any?
end

#success?Boolean Also known as: successful?

Returns true if the command was called successfully (no errors).

Returns:

  • (Boolean)

    true if called and no errors present



72
73
74
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 72

def success?
  called? && !failure?
end