Class: Cuprum::Operation
Overview
Functional object that with syntactic sugar for tracking the last result.
An Operation is like a Function, but with an additional trick of tracking its own most recent execution result. This allows us to simplify some conditional logic, especially boilerplate code used to interact with frameworks.
Like a Function, an Operation can be defined directly by passing an implementation block to the constructor or by creating a subclass that overwrites the #process method.
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::Result
Executes the logic encoded in the constructor block, or the #process method if no block was passed to the constructor.
-
#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.
-
#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.
Methods inherited from Function
#chain, #else, #initialize, #then
Constructor Details
This class inherits a constructor from Cuprum::Function
Instance Attribute Details
#result ⇒ Cuprum::Result (readonly)
Returns The result from the most recent call of the operation.
32 33 34 |
# File 'lib/cuprum/operation.rb', line 32 def result @result end |
Instance Method Details
#call(*arguments, **keywords) { ... } ⇒ Cuprum::Result
35 36 37 38 39 |
# File 'lib/cuprum/operation.rb', line 35 def call *args, &block reset! if called? # Clear reference to most recent result. @result = super end |
#called? ⇒ Boolean
Returns true if the operation has been called and has a reference to the most recent result; otherwise false.
43 44 45 |
# File 'lib/cuprum/operation.rb', line 43 def called? !result.nil? end |
#errors ⇒ Array
Returns the errors from the most recent result, or nil if the operation has not been called.
49 50 51 |
# File 'lib/cuprum/operation.rb', line 49 def errors super || (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.
56 57 58 |
# File 'lib/cuprum/operation.rb', line 56 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.
68 69 70 |
# File 'lib/cuprum/operation.rb', line 68 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.
75 76 77 |
# File 'lib/cuprum/operation.rb', line 75 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.
81 82 83 |
# File 'lib/cuprum/operation.rb', line 81 def value called? ? result.value : nil end |