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`
Defined Under Namespace
Modules: ClassMethods, Utils Classes: Errors, NotImplementedError
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
The return value from the command’s call method.
Class Method Summary collapse
Instance Method Summary collapse
-
#call ⇒ self
Executes the command by calling super (your command’s implementation).
-
#errors ⇒ Errors
Returns the errors collection for this command.
-
#failure? ⇒ Boolean
Returns true if the command was called but has errors.
-
#success? ⇒ Boolean
(also: #successful?)
Returns true if the command was called successfully (no errors).
Instance Attribute Details
#result ⇒ Object (readonly)
Returns 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
#call ⇒ self
Executes the command by calling super (your command’s implementation). Tracks execution state and stores the result.
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 |
#errors ⇒ Errors
Returns the errors collection for this command. If the command class defines its own errors method, that will be used instead.
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.
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).
72 73 74 |
# File 'lib/simple_command_dispatcher/commands/command_callable.rb', line 72 def success? called? && !failure? end |