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.

Examples:

class CustomOperation < CustomCommand
  include Cuprum::Operation::Mixin
end # class

Instance Attribute Summary collapse

Instance Method Summary collapse

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.



48
49
50
# File 'lib/cuprum/operation.rb', line 48

def result
  @result
end

Instance Method Details

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

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

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:

  • Command#call


65
66
67
68
69
70
71
# File 'lib/cuprum/operation.rb', line 65

def call *args, &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.

Returns:

  • (Boolean)

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



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

def called?
  !result.nil?
end

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



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

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.

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.



88
89
90
# File 'lib/cuprum/operation.rb', line 88

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.



100
101
102
# File 'lib/cuprum/operation.rb', line 100

def reset!
  @result = nil
end

#statusSymbol?

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

Returns:

  • (Symbol, nil)

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



106
107
108
# File 'lib/cuprum/operation.rb', line 106

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.

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.



113
114
115
# File 'lib/cuprum/operation.rb', line 113

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

#to_cuprum_resultCuprum::Result

Returns the most result if the operation was previously called. Otherwise, returns a failing result.

Returns:



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

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



131
132
133
# File 'lib/cuprum/operation.rb', line 131

def value
  called? ? result.value : nil
end