module Cuprum::Operation::Mixin
Module-based implementation of the Operation
methods. Use this to convert an already-defined command into an operation.
@example
class CustomOperation < CustomCommand include Cuprum::Operation::Mixin end
Attributes
@return [Cuprum::Result] The result from the most recent call of the
operation.
Public Instance Methods
@overload call(*arguments, **keywords, &block)
Executes the logic encoded in the constructor block, or the #process method if no block was passed to the constructor, and returns the operation object. @param arguments [Array] Arguments to be passed to the implementation. @param keywords [Hash] Keywords to be passed to the implementation. @return [Cuprum::Operation] the called operation. @yield If a block argument is given, it will be passed to the implementation.
@see Cuprum::Command#call
# File lib/cuprum/operation.rb, line 67 def call(*args, **kwargs, &block) reset! if called? # Clear reference to most recent result. @result = super self end
@return [Boolean] true if the operation has been called and has a
reference to the most recent result; otherwise false.
# File lib/cuprum/operation.rb, line 77 def called? !result.nil? end
@return [Object] the error (if any) from the most recent result, or nil
if the operation has not been called.
# File lib/cuprum/operation.rb, line 83 def error called? ? result.error : nil end
@return [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.
# File lib/cuprum/operation.rb, line 90 def failure? called? ? result.failure? : false end
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 102 def reset! @result = nil end
@return [Symbol, nil] the status of the most recent result, or nil if
the operation has not been called.
# File lib/cuprum/operation.rb, line 108 def status called? ? result.status : nil end
@return [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.
# File lib/cuprum/operation.rb, line 115 def success? called? ? result.success? : false end
Returns the most result if the operation was previously called. Otherwise, returns a failing result.
@return [Cuprum::Result] the most recent result or failing result.
# File lib/cuprum/operation.rb, line 123 def to_cuprum_result return result if result error = Cuprum::Errors::OperationNotCalled.new(operation: self) Cuprum::Result.new(error: error) end
@return [Object] the value of the most recent result, or nil if the
operation has not been called.
# File lib/cuprum/operation.rb, line 133 def value called? ? result.value : nil end