Module: Interactor

Extended by:
ActiveSupport::Concern
Includes:
Declaration, Hooks
Included in:
Organizer
Defined in:
lib/interactor.rb,
lib/interactor/error.rb,
lib/interactor/hooks.rb,
lib/interactor/context.rb,
lib/interactor/organizer.rb,
lib/interactor/declaration.rb

Overview

Public: Interactor methods. Because Interactor is a module, custom Interactor classes should include Interactor rather than inherit from it.

Examples

class MyInteractor
  include Interactor

  def call
    puts context.foo
  end
end

Defined Under Namespace

Modules: Declaration, Hooks, Organizer Classes: Context, Failure

Instance Method Summary collapse

Instance Method Details

#callObject

Public: Invoke an Interactor instance without any hooks, tracking, or rollback. It is expected that the “call” instance method is overwritten for each interactor class.

Returns nothing.



156
157
# File 'lib/interactor.rb', line 156

def call
end

#initialize(context = {}) ⇒ Object

Internal: Initialize an Interactor.

context - A Hash whose key/value pairs are used in initializing the

interactor's context. An existing Interactor::Context may also be
given. (default: {})

Examples

MyInteractor.new(foo: "bar")
# => #<MyInteractor @context=#<Interactor::Context foo="bar">>

MyInteractor.new
# => #<MyInteractor @context=#<Interactor::Context>>


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

def initialize(context = {})
  @context = self.context_class.build(**context.to_h)
end

#runObject

Internal: Invoke an interactor instance along with all defined hooks. The “run” method is used internally by the “call” class method. The following are equivalent:

MyInteractor.call(foo: "bar")
# => #<Interactor::Context foo="bar">

interactor = MyInteractor.new(foo: "bar")
interactor.run
interactor.context
# => #<Interactor::Context foo="bar">

After successful invocation of the interactor, the instance is tracked within the context. If the context is failed or any error is raised, the context is rolled back.

Returns nothing.



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

def run
  run!
rescue Failure
end

#run!Object

Internal: Invoke an Interactor instance along with all defined hooks. The “run!” method is used internally by the “call!” class method. The following are equivalent:

MyInteractor.call!(foo: "bar")
# => #<Interactor::Context foo="bar">

interactor = MyInteractor.new(foo: "bar")
interactor.run!
interactor.context
# => #<Interactor::Context foo="bar">

After successful invocation of the interactor, the instance is tracked within the context. If the context is failed or any error is raised, the context is rolled back.

The “run!” method behaves identically to the “run” method with one notable exception. If the context is failed during invocation of the interactor, the Interactor::Failure is raised.

Returns nothing. Raises Interactor::Failure if the context is failed.



142
143
144
145
146
147
148
149
# File 'lib/interactor.rb', line 142

def run!
  with_hooks do
    call
  end
rescue Failure => e
  # Make sure we fail the current context when a call! to another interactor fails
  context.fail!(error: e.context&.error, error_cause: e.cause)
end