Class: Cuprum::Operation
- Includes:
- Mixin
- Defined in:
- lib/cuprum/operation.rb
Overview
Functional object with syntactic sugar for tracking the last result.
An Operation is like a Command, but with two key differences. First, an Operation retains a reference to the result object from the most recent time the operation was called and delegates the methods defined by Cuprum::Result to the most recent result. This allows a called Operation to replace a Cuprum::Result in any code that expects or returns a result. Second, the #call method returns the operation instance, rather than the result itself.
These two features allow developers to simplify logic around calling and using the results of operations, and reduce the need for boilerplate code (particularly when using an operation as part of an existing framework, such as inside of an asynchronous worker or a Rails controller action).
Like a Command, an Operation can be defined directly by passing an implementation block to the constructor or by creating a subclass that overwrites the #process method.
Defined Under Namespace
Modules: Mixin
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation
Calls the command implementation and stores the result.
-
#called? ⇒ Boolean
True if the operation has been called and has a reference to the most recent result; otherwise false.
-
#error ⇒ Object
The error (if any) from the most recent result, or nil if the operation has not been called.
-
#failure? ⇒ Boolean
True if the most recent result had an error, or false if the most recent result had no error or if the operation has not been called.
-
#reset! ⇒ Object
Clears the reference to the most recent call of the operation, if any.
-
#result ⇒ Cuprum::Result
The result from the most recent call of the operation.
-
#success? ⇒ Boolean
True if the most recent result had no error, or false if the most recent result had an error or if the operation has not been called.
-
#to_cuprum_result ⇒ Cuprum::Result
The most recent result if the operation was previously called; otherwise, returns a failing result with a Cuprum::Errors::OperationNotCalled error.
-
#value ⇒ Object
The value of the most recent result, or nil if the operation has not been called.
Methods included from Mixin
Methods inherited from Command
Methods included from Steps
Methods included from Currying
Methods included from Processing
Constructor Details
This class inherits a constructor from Cuprum::Command
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation
|
# File 'lib/cuprum/operation.rb', line 137
|
#called? ⇒ Boolean
Returns true if the operation has been called and has a reference to the most recent result; otherwise false.
|
# File 'lib/cuprum/operation.rb', line 140
|
#error ⇒ Object
Returns the error (if any) from the most recent result, or nil if the operation has not been called.
|
# File 'lib/cuprum/operation.rb', line 143
|
#failure? ⇒ Boolean
Returns true if the most recent result had an error, or false if the most recent result had no error or if the operation has not been called.
|
# File 'lib/cuprum/operation.rb', line 146
|
#reset! ⇒ Object
Clears the reference to the most recent call of the operation, if any. This allows the result and any referenced data to be garbage collected. Use this method to clear any instance variables or state internal to the operation (an operation should never have external state apart from the last result).
If the operation cannot be run more than once, this method should raise an error.
|
# File 'lib/cuprum/operation.rb', line 149
|
#result ⇒ Cuprum::Result
Returns The result from the most recent call of the operation.
|
# File 'lib/cuprum/operation.rb', line 152
|
#success? ⇒ Boolean
Returns true if the most recent result had no error, or false if the most recent result had an error or if the operation has not been called.
|
# File 'lib/cuprum/operation.rb', line 155
|
#to_cuprum_result ⇒ Cuprum::Result
Returns the most recent result if the operation was previously called; otherwise, returns a failing result with a Cuprum::Errors::OperationNotCalled error.
|
# File 'lib/cuprum/operation.rb', line 158
|
#value ⇒ Object
Returns the value of the most recent result, or nil if the operation has not been called.
|
# File 'lib/cuprum/operation.rb', line 161
|