Module: Cuprum::ResultHelpers

Included in:
Command
Defined in:
lib/cuprum/result_helpers.rb

Overview

Helper methods that delegate result methods to the currently processed result.

Examples:

class LogCommand
  include Cuprum::Processing
  include Cuprum::ResultHelpers

  private

  def process log
    case log[:level]
    when 'fatal'
      halt!

      'error'
    when 'error' && log[:message]
      errors << message

      'error'
    when 'error'
      failure!

      'error'
    else
      'ok'
    end # case
  end # method process
end # class

result = LogCommand.new.call(:level => 'info')
result.success? #=> true

string = 'something went wrong'
result = LogCommand.new.call(:level => 'error', :message => string)
result.success? #=> false
result.errors   #=> ['something went wrong']

result = LogCommand.new.call(:level => 'error')
result.success? #=> false
result.errors   #=> []

result = LogCommand.new.call(:level => 'fatal')
result.halted? #=> true

See Also:

Instance Method Summary collapse

Instance Method Details

#errorsArray, Object

Note:

This is a private method, and only available when executing the command implementation as defined in the constructor block or the #process method.

Provides a reference to the current result’s errors object. Messages or error objects added to this will be included in the #errors method of the returned result object.

Returns:

  • (Array, Object)

    The errors object.

See Also:

  • Cuprum::Result#errors.


66
67
68
# File 'lib/cuprum/result_helpers.rb', line 66

def errors
  result&.errors
end

#failure!Object

Note:

This is a private method, and only available when executing the command implementation as defined in the constructor block or the #process method.

Marks the current result as failed. Calling #failure? on the returned result object will evaluate to true, whether or not the result has any errors.

See Also:

  • Cuprum::Result#failure!.


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

def failure!
  result&.failure!
end

#halt!Object

Note:

This is a private method, and only available when executing the command implementation as defined in the constructor block or the #process method.

Marks the current result as halted.

See Also:

  • Cuprum::Result#halt!.


94
95
96
# File 'lib/cuprum/result_helpers.rb', line 94

def halt!
  result&.halt!
end

#success!Object

Note:

This is a private method, and only available when executing the command implementation as defined in the constructor block or the #process method.

Marks the current result as passing. Calling #success? on the returned result object will evaluate to true, whether or not the result has any errors.

See Also:

  • Cuprum::Result#success!.


109
110
111
# File 'lib/cuprum/result_helpers.rb', line 109

def success!
  result&.success!
end