Module: Interactor

Defined in:
lib/interactor.rb,
lib/interactor/error.rb,
lib/interactor/hooks.rb,
lib/interactor/context.rb,
lib/interactor/organizer.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: ClassMethods, Hooks, Organizer Classes: Context, Failure

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Internal: Install Interactor's behavior in the given class.


20
21
22
23
24
25
26
27
28
# File 'lib/interactor.rb', line 20

def self.included(base)
  base.class_eval do
    extend ClassMethods
    include Hooks

    # Public: Gets the Interactor::Context of the Interactor instance.
    attr_reader :context
  end
end

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>>

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

def initialize(context = {})
  @context = Context.build(context)
end

#rollbackObject

Public: Reverse prior invocation of an Interactor instance. Any interactor class that requires undoing upon downstream failure is expected to overwrite the “rollback” instance method.

Returns nothing.


164
165
# File 'lib/interactor.rb', line 164

def rollback
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.


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

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.


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

def run!
  with_hooks do
    call
    context.called!(self)
  end
rescue
  context.rollback!
  raise
end