Module: ActiveInteractor::Context::Status

Included in:
Base
Defined in:
lib/active_interactor/context/status.rb

Overview

Context status methods. Because Status is a module classes should include Status rather than inherit from it.

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#called!(interactor) ⇒ Array<Class>

Add an instance of interactor to the list of interactors called on the context. This list is used when #rollback! is called on a context instance.

Parameters:

Returns:

Since:

  • 0.1.0



17
18
19
# File 'lib/active_interactor/context/status.rb', line 17

def called!(interactor)
  _called << interactor
end

#fail!(errors = nil) ⇒ Object

Fail the context instance. Failing an instance raises an error that may be rescued by the calling interactor. The instance is also flagged as having failed.

Examples:

Fail an interactor context

class MyInteractor < ActiveInteractor::Base
  def perform
    context.fail!
  end
end

MyInteractor.perform!
ActiveInteractor::Error::ContextFailure "<#MyInteractor::Context>"

Parameters:

  • errors (ActiveModel::Errors, String) (defaults to: nil)

    error messages for the failure

Raises:

See Also:

Since:

  • 0.1.0



39
40
41
42
43
# File 'lib/active_interactor/context/status.rb', line 39

def fail!(errors = nil)
  merge_errors!(errors) if errors
  @_failed = true
  raise ActiveInteractor::Error::ContextFailure, self
end

#failure?Boolean Also known as: fail?

Note:

The #failure? method is the inverse of the #success? method

Whether the context instance has failed. By default, a new instance is successful and only changes when explicitly failed.

Examples:

Check if a context has failed

result = MyInteractor.perform
result.failure?
#=> false

Returns:

  • (Boolean)

    false by default or true if failed.

Since:

  • 0.1.0



57
58
59
# File 'lib/active_interactor/context/status.rb', line 57

def failure?
  @_failed || false
end

#rollback!Boolean

Rollback an instance of context. Any interactors the instance has been passed via the #called! method are asked to roll themselves back by invoking their #rollback methods. The instance is also flagged as rolled back.

Returns:

Since:

  • 0.1.0



70
71
72
73
74
75
# File 'lib/active_interactor/context/status.rb', line 70

def rollback!
  return false if @_rolled_back

  _called.reverse_each(&:rollback)
  @_rolled_back = true
end

#success?Boolean Also known as: successful?

Note:

The #success? method is the inverse of the #failure? method

Whether the context instance is successful. By default, a new instance is successful and only changes when explicitly failed.

Examples:

Check if a context has failed

result = MyInteractor.perform
result.success?
#=> true

Returns:

  • (Boolean)

    true by default or false if failed

Since:

  • 0.1.0



89
90
91
# File 'lib/active_interactor/context/status.rb', line 89

def success?
  !failure?
end