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



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

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



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

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



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

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



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

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



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

def errors
  @errors.dup
end

#fail!Object

Forces the status to be a failure

Since:

  • 0.3.5



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

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



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

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



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

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



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

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