Class: Hanami::Interactor::Result

Inherits:
Utils::BasicObject
Defined in:
lib/hanami/interactor.rb

Overview

Result of an operation

Since:

  • 0.3.5

Constant Summary collapse

METHODS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Concrete methods

See Also:

  • #respond_to_missing?

Since:

  • 0.3.5

::Hash[initialize: true,
success?: true,
successful?: true,
failure?: true,
fail!: true,
prepare!: true,
errors: true,
error: true].freeze

Instance Method Summary collapse

Methods inherited from Utils::BasicObject

#class, const_missing, #inspect, #instance_of?, #is_a?, #kind_of?, #object_id, #pretty_print, #respond_to?

Constructor Details

#initialize(payload = {}) ⇒ Hanami::Interactor::Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new result

Parameters:

  • payload (Hash) (defaults to: {})

    a payload to carry on

Since:

  • 0.3.5


39
40
41
42
43
# File 'lib/hanami/interactor.rb', line 39

def initialize(payload = {})
  @payload = payload
  @errors  = []
  @success = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.3.5


124
125
126
# File 'lib/hanami/interactor.rb', line 124

def method_missing(method_name, *)
  @payload.fetch(method_name) { super }
end

Instance Method Details

#add_error(*errors) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0


89
90
91
92
93
# File 'lib/hanami/interactor.rb', line 89

def add_error(*errors)
  @errors << errors
  @errors.flatten!
  nil
end

#errornil, String

Returns the first errors collected during an operation

Returns:

  • (nil, String)

    the error, if present

See Also:

  • #errors
  • Hanami::Interactor#call
  • Hanami::Interactor#error
  • Hanami::Interactor#error!

Since:

  • 0.3.5


105
106
107
# File 'lib/hanami/interactor.rb', line 105

def error
  errors.first
end

#errorsArray

Returns all the errors collected during an operation

Returns:

  • (Array)

    the errors

See Also:

  • #error
  • Hanami::Interactor#call
  • Hanami::Interactor#error
  • Hanami::Interactor#error!

Since:

  • 0.3.5


83
84
85
# File 'lib/hanami/interactor.rb', line 83

def errors
  @errors.dup
end

#fail!Object

Forces the status to be a failure

Since:

  • 0.3.5


69
70
71
# File 'lib/hanami/interactor.rb', line 69

def fail!
  @success = false
end

#failure?TrueClass, FalseClass

Checks if the current status is not successful

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.9.2


62
63
64
# File 'lib/hanami/interactor.rb', line 62

def failure?
  !successful?
end

#prepare!(payload) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Prepares the result before to be returned

Parameters:

  • payload (Hash)

    an updated payload

Since:

  • 0.3.5


115
116
117
118
# File 'lib/hanami/interactor.rb', line 115

def prepare!(payload)
  @payload.merge!(payload)
  self
end

#successful?TrueClass, FalseClass Also known as: success?

Checks if the current status is successful

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.8.1


50
51
52
# File 'lib/hanami/interactor.rb', line 50

def successful?
  @success && errors.empty?
end