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


37
38
39
40
41
# File 'lib/hanami/interactor.rb', line 37

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


122
123
124
# File 'lib/hanami/interactor.rb', line 122

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


87
88
89
90
91
# File 'lib/hanami/interactor.rb', line 87

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


103
104
105
# File 'lib/hanami/interactor.rb', line 103

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


81
82
83
# File 'lib/hanami/interactor.rb', line 81

def errors
  @errors.dup
end

#fail!Object

Forces the status to be a failure

Since:

  • 0.3.5


67
68
69
# File 'lib/hanami/interactor.rb', line 67

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


60
61
62
# File 'lib/hanami/interactor.rb', line 60

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


113
114
115
116
# File 'lib/hanami/interactor.rb', line 113

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


48
49
50
# File 'lib/hanami/interactor.rb', line 48

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