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
readonly
The result from the most recent call of the operation.
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.
-
#status ⇒ Symbol?
The status of the most recent result, or nil if the operation has not been called.
-
#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.
Instance Attribute Details
#result ⇒ Cuprum::Result (readonly)
Returns The result from the most recent call of the operation.
51 52 53 |
# File 'lib/cuprum/operation.rb', line 51 def result @result end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation
66 67 68 69 70 71 72 |
# File 'lib/cuprum/operation.rb', line 66 def call(*args, **kwargs, &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.
76 77 78 |
# File 'lib/cuprum/operation.rb', line 76 def called? !result.nil? end |
#error ⇒ Object
Returns the error (if any) from the most recent result, or nil if the operation has not been called.
82 83 84 |
# File 'lib/cuprum/operation.rb', line 82 def error called? ? result.error : nil end |
#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.
89 90 91 |
# File 'lib/cuprum/operation.rb', line 89 def failure? called? ? result.failure? : 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.
101 102 103 |
# File 'lib/cuprum/operation.rb', line 101 def reset! @result = nil end |
#status ⇒ Symbol?
Returns the status of the most recent result, or nil if the operation has not been called.
107 108 109 |
# File 'lib/cuprum/operation.rb', line 107 def status called? ? result.status : nil end |
#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.
114 115 116 |
# File 'lib/cuprum/operation.rb', line 114 def success? called? ? result.success? : false end |
#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.
121 122 123 124 125 126 127 |
# File 'lib/cuprum/operation.rb', line 121 def to_cuprum_result return result if result error = Cuprum::Errors::OperationNotCalled.new(operation: self) Cuprum::Result.new(error: error) end |
#value ⇒ Object
Returns the value of the most recent result, or nil if the operation has not been called.
131 132 133 |
# File 'lib/cuprum/operation.rb', line 131 def value called? ? result.value : nil end |