Class: Cuprum::Operation

Inherits:
Function show all
Defined in:
lib/cuprum/operation.rb

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.

Examples:

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

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

    render :new
  end # if-else
end # create

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#chain, #else, #initialize, #then

Constructor Details

This class inherits a constructor from Cuprum::Function

Instance Attribute Details

#resultCuprum::Result (readonly)

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

Returns:

  • (Cuprum::Result)

    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

Executes the logic encoded in the constructor block, or the #process method if no block was passed to the constructor.

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:

Raises:

  • (NotImplementedError)

    Unless a block was passed to the constructor or the #process method was overriden by a Function subclass.



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.

Returns:

  • (Boolean)

    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

#errorsArray

Returns the errors from the most recent result, or nil if the operation has not been called.

Returns:

  • (Array)

    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.

Returns:

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



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.

Returns:

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



75
76
77
# File 'lib/cuprum/operation.rb', line 75

def success?
  called? ? result.success? : false
end

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



81
82
83
# File 'lib/cuprum/operation.rb', line 81

def value
  called? ? result.value : nil
end