Class: ActiveInteractor::Organizer

Inherits:
Base
  • Object
show all
Defined in:
lib/active_interactor/organizer.rb

Overview

The Base Interactor class inherited by all Interactor::Organizers

Author:

Since:

  • 0.0.1

Version:

  • 0.3

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Interactor

#fail_on_invalid_context?, #rollback, #should_clean_context?, #skip_clean_context!

Constructor Details

This class inherits a constructor from ActiveInteractor::Base

Class Method Details

.after_each_perform(*filters, &block) ⇒ Object

Define a callback to call after each organized interactor’s

{ActiveInteractor::Base.perform} has been invoked

Examples:

class MyInteractor1 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  after_each_perform :print_done

  organized MyInteractor1, MyInteractor2

  private

  def print_done
    puts "done"
  end
end

MyOrganizer.perform(name: 'Aaron')
"MyInteractor1"
"Done"
"MyInteractor2"
"Done"
#=> <MyOrganizer::Context name='Aaron'>

Since:

  • 0.0.1



51
52
53
# File 'lib/active_interactor/organizer.rb', line 51

def after_each_perform(*filters, &block)
  set_callback(:each_perform, :after, *filters, &block)
end

.around_each_perform(*filters, &block) ⇒ Object

Define a callback to call around each organized interactor’s

{ActiveInteractor::Base.perform} has been invokation

Examples:

class MyInteractor1 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  around_each_perform :print_time

  organized MyInteractor1, MyInteractor2

  private

  def print_time
    puts Time.now.utc
    yield
    puts Time.now.utc
  end
end

MyOrganizer.perform(name: 'Aaron')
"2019-04-01 00:00:00 UTC"
"MyInteractor1"
"2019-04-01 00:00:01 UTC"
"2019-04-01 00:00:02 UTC"
"MyInteractor2"
"2019-04-01 00:00:03 UTC"
#=> <MyOrganizer::Context name='Aaron'>

Since:

  • 0.0.1



97
98
99
# File 'lib/active_interactor/organizer.rb', line 97

def around_each_perform(*filters, &block)
  set_callback(:each_perform, :around, *filters, &block)
end

.before_each_perform(*filters, &block) ⇒ Object

Define a callback to call before each organized interactor’s

{ActiveInteractor::Base.perform} has been invoked

Examples:

class MyInteractor1 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  before_perform :print_name

  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  before_each_perform :print_start

  organized MyInteractor1, MyInteractor2

  private

  def print_start
    puts "Start"
  end
end

MyOrganizer.perform(name: 'Aaron')
"Start"
"MyInteractor1"
"Start"
"MyInteractor2"
#=> <MyOrganizer::Context name='Aaron'>

Since:

  • 0.0.1



139
140
141
# File 'lib/active_interactor/organizer.rb', line 139

def before_each_perform(*filters, &block)
  set_callback(:each_perform, :before, *filters, &block)
end

.organize(*interactors) ⇒ Object

Declare Interactors to be invoked as part of the

organizer's invocation. These interactors are invoked in
the order in which they are declared

Examples:

class MyFirstOrganizer < ActiveInteractor::Organizer
  organize InteractorOne, InteractorTwo
end

class MySecondOrganizer < ActiveInteractor::Organizer
  organize [InteractorThree, InteractorFour]
end

Parameters:

Since:

  • 0.0.1



157
158
159
# File 'lib/active_interactor/organizer.rb', line 157

def organize(*interactors)
  @organized = interactors.flatten
end

.organizedArray<ActiveInteractor::Base] the organized interactors

An Array of declared Interactors to be invoked

Returns:

Since:

  • 0.0.1



163
164
165
# File 'lib/active_interactor/organizer.rb', line 163

def organized
  @organized ||= []
end

Instance Method Details

#performObject

Invoke the organized interactors. An organizer is

expected not to define its own {ActiveInteractor::Interactor#perform #perform} method
in favor of this default implementation.

Since:

  • 0.0.1



171
172
173
174
175
176
177
178
179
# File 'lib/active_interactor/organizer.rb', line 171

def perform
  self.class.organized.each do |interactor|
    run_callbacks :each_perform do
      self.context = interactor.new(context)
                               .tap(&:skip_clean_context!)
                               .execute_perform!
    end
  end
end