Class: Lotus::Interactor::Result

Inherits:
Utils::BasicObject
Defined in:
lib/lotus/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

{initialize: true, success?: true, fail!: true, prepare!: true, errors: true, error: true}.freeze

Instance Method Summary collapse

Methods inherited from Utils::BasicObject

#class, #inspect, #respond_to?

Constructor Details

#initialize(payload = {}) ⇒ Lotus::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



30
31
32
33
34
# File 'lib/lotus/interactor.rb', line 30

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ 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



102
103
104
# File 'lib/lotus/interactor.rb', line 102

def method_missing(m, *)
  @payload.fetch(m) { 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



68
69
70
71
72
# File 'lib/lotus/interactor.rb', line 68

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
  • Lotus::Interactor#call
  • Lotus::Interactor#error
  • Lotus::Interactor#error!

Since:

  • 0.3.5



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

def error
  errors.first
end

#errorsArray

Returns all the errors collected during an operation

Returns:

  • (Array)

    the errors

See Also:

  • #error
  • Lotus::Interactor#call
  • Lotus::Interactor#error
  • Lotus::Interactor#error!

Since:

  • 0.3.5



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

def errors
  @errors.dup
end

#fail!Object

Force the status to be a failure

Since:

  • 0.3.5



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

def fail!
  @success = false
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.

Prepare the result before to be returned

Parameters:

  • payload (Hash)

    an updated payload

Since:

  • 0.3.5



94
95
96
97
# File 'lib/lotus/interactor.rb', line 94

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

#success?TrueClass, FalseClass

Check if the current status is successful

Returns:

  • (TrueClass, FalseClass)

    the result of the check

Since:

  • 0.3.5



41
42
43
# File 'lib/lotus/interactor.rb', line 41

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