Class: Cuprum::Operation

Inherits:
Command show all
Includes:
Mixin
Defined in:
lib/cuprum/operation.rb

Overview

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).

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.

Examples:

def create
  operation = CreateBookOperation.new.call(book_params)

  if operation.success?
    redirect_to(operation.value)
  else
    @book = operation.value

    render :new
  end
end

See Also:

Defined Under Namespace

Modules: Mixin

Instance Method Summary collapse

Methods included from Mixin

#status

Methods inherited from Command

#initialize, #to_proc

Methods included from Steps

#step, #steps

Methods included from Currying

#curry

Methods included from Processing

#arity, #process

Constructor Details

This class inherits a constructor from Cuprum::Command

Instance Method Details

#call(*arguments, **keywords) { ... } ⇒ Cuprum::Operation

Calls the command implementation and stores the result.

Parameters:

  • arguments (Array)

    Arguments to be passed to the implementation.

  • keywords (Hash)

    Keywords to be passed to the implementation.

Yields:

  • If a block argument is given, it will be passed to the implementation.

Returns:

See Also:



# File 'lib/cuprum/operation.rb', line 137

#called?Boolean

Returns true if the operation has been called and has a reference to the most recent result; otherwise false.

Returns:

  • (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 140

#errorObject

Returns the error (if any) from the most recent result, or nil if the operation has not been called.

Returns:

  • (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 143

#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.

Returns:

  • (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 146

#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.



# File 'lib/cuprum/operation.rb', line 149

#resultCuprum::Result

Returns The result from the most recent call of the operation.

Returns:

  • (Cuprum::Result)

    The result from the most recent call of the operation.



# File 'lib/cuprum/operation.rb', line 152

#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.

Returns:

  • (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 155

#to_cuprum_resultCuprum::Result

Returns the most recent result if the operation was previously called; otherwise, returns a failing result with a Cuprum::Errors::OperationNotCalled error.

Returns:

  • (Cuprum::Result)

    the most recent result if the operation was previously called; otherwise, returns a failing result with a Cuprum::Errors::OperationNotCalled error.



# File 'lib/cuprum/operation.rb', line 158

#valueObject

Returns the value of the most recent result, or nil if the operation has not been called.

Returns:

  • (Object)

    the value of the most recent result, or nil if the operation has not been called.



# File 'lib/cuprum/operation.rb', line 161