Module: Cuprum::Operation::Mixin
- Included in:
- BuiltIn::IdentityOperation, BuiltIn::NullOperation, Cuprum::Operation
- Defined in:
- lib/cuprum/operation.rb
Overview
Module-based implementation of the Operation methods. Use this to convert an already-defined command into an operation.
Instance Attribute Summary collapse
-
#result ⇒ Cuprum::Result
(also: #to_result)
readonly
The result from the most recent call of the operation.
Instance Method Summary collapse
-
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation
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.
-
#called? ⇒ Boolean
True if the operation has been called and has a reference to the most recent result; otherwise false.
-
#errors ⇒ Array
The errors from the most recent result, or nil if the operation has not been called.
-
#failure? ⇒ Boolean
True if the most recent result had errors, or false if the most recent result had no errors or if the operation has not been called.
-
#halted? ⇒ Boolean
True if the most recent was halted, otherwise false.
-
#reset! ⇒ Object
Clears the reference to the most recent call of the operation, if any.
-
#success? ⇒ Boolean
True if the most recent result had no errors, or false if the most recent result had errors or if the operation has not been called.
-
#value ⇒ Object
The value of the most recent result, or nil if the operation has not been called.
Instance Attribute Details
#result ⇒ Cuprum::Result (readonly) Also known as: to_result
Returns The result from the most recent call of the operation.
47 48 49 |
# File 'lib/cuprum/operation.rb', line 47 def result @result end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation
69 70 71 72 73 74 75 |
# File 'lib/cuprum/operation.rb', line 69 def call *args, &block reset! if called? # Clear reference to most recent result. @result = super self end |
#called? ⇒ Boolean
Returns true if the operation has been called and has a reference to the most recent result; otherwise false.
79 80 81 |
# File 'lib/cuprum/operation.rb', line 79 def called? !result.nil? end |
#errors ⇒ Array
Returns the errors from the most recent result, or nil if the operation has not been called.
85 86 87 |
# File 'lib/cuprum/operation.rb', line 85 def errors called? ? result.errors : nil end |
#failure? ⇒ Boolean
Returns true if the most recent result had errors, or false if the most recent result had no errors or if the operation has not been called.
92 93 94 |
# File 'lib/cuprum/operation.rb', line 92 def failure? called? ? result.failure? : false end |
#halted? ⇒ Boolean
Returns true if the most recent was halted, otherwise false.
97 98 99 |
# File 'lib/cuprum/operation.rb', line 97 def halted? called? ? result.halted? : false end |
#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.
109 110 111 |
# File 'lib/cuprum/operation.rb', line 109 def reset! @result = nil end |
#success? ⇒ Boolean
Returns true if the most recent result had no errors, or false if the most recent result had errors or if the operation has not been called.
116 117 118 |
# File 'lib/cuprum/operation.rb', line 116 def success? called? ? result.success? : false end |
#value ⇒ Object
Returns the value of the most recent result, or nil if the operation has not been called.
122 123 124 |
# File 'lib/cuprum/operation.rb', line 122 def value called? ? result.value : nil end |