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.

Author:

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
44
# File 'lib/active_interactor/context/status.rb', line 39

def fail!(errors = nil)
  handle_errors(errors) if errors
  @_failed = true
  resolve
  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



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

def failure?
  @_failed || false
end

#resolveself

Resolve an instance of context. Called when an interactor is finished with it's context.

Returns:

  • (self)

    the instance of context

Since:

  • 1.0.3



68
69
70
71
# File 'lib/active_interactor/context/status.rb', line 68

def resolve
  resolve_errors
  self
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



81
82
83
84
85
86
# File 'lib/active_interactor/context/status.rb', line 81

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



100
101
102
# File 'lib/active_interactor/context/status.rb', line 100

def success?
  !failure?
end