class Cuprum::Operation
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).
@example
def create operation = CreateBookOperation.new.call(book_params) if operation.success? redirect_to(operation.value) else @book = operation.value render :new end end
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.
@see Cuprum::Command