Module: ActiveInteractor::Organizer::Callbacks::ClassMethods

Included in:
Base
Defined in:
lib/active_interactor/organizer/callbacks.rb

Overview

Organizer callback class methods. Because ClassMethods is a module classes should extend ClassMethods rather than inherit from it.

Author:

  • Aaron Allen

Since:

  • 1.0.0

Instance Method Summary collapse

Instance Method Details

#after_all_perform(*filters, &block) ⇒ Object

Define a callback to call after all organized interactors' #perform methods have been called.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  after_all_perform :print_done

  organized MyInteractor1, MyInteractor2

  private

  def print_done
    puts 'Done'
  end
end

MyOrganizer.perform
"MyInteractor1"
"MyInteractor2"
"Done"
#=> <MyOrganizer::Context>

Since:

  • v1.2.0



180
181
182
# File 'lib/active_interactor/organizer/callbacks.rb', line 180

def after_all_perform(*filters, &block)
  set_callback(:all_perform, :after, *filters, &block)
end

#after_each_perform(*filters, &block) ⇒ Object

Define a callback to call after each organized interactor's #perform method has been called.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  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
"MyInteractor1"
"Done"
"MyInteractor2"
"Done"
#=> <MyOrganizer::Context>

Since:

  • 0.1.3



55
56
57
# File 'lib/active_interactor/organizer/callbacks.rb', line 55

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

#around_all_perform(*filters, &block) ⇒ Object

Define a callback to call around all organized interactors' #perform method calls.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
    sleep(1)
  end
end

class MyInteractor2 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor2'
    sleep(1)
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  around_all_perform :print_time

  organized MyInteractor1, MyInteractor2

  private

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

MyOrganizer.perform
"2019-04-01 00:00:00 UTC"
"MyInteractor1"
"MyInteractor2"
"2019-04-01 00:00:02 UTC"
#=> <MyOrganizer::Context>

Since:

  • v1.2.0



224
225
226
# File 'lib/active_interactor/organizer/callbacks.rb', line 224

def around_all_perform(*filters, &block)
  set_callback(:all_perform, :around, *filters, &block)
end

#around_each_perform(*filters, &block) ⇒ Object

Define a callback to call around each organized interactor's #perform method call.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
    sleep(1)
  end
end

class MyInteractor2 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor2'
    sleep(1)
  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
"2019-04-01 00:00:00 UTC"
"MyInteractor1"
"2019-04-01 00:00:01 UTC"
"2019-04-01 00:00:01 UTC"
"MyInteractor2"
"2019-04-01 00:00:02 UTC"
#=> <MyOrganizer::Context>

Since:

  • 0.1.3



101
102
103
# File 'lib/active_interactor/organizer/callbacks.rb', line 101

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

#before_all_perform(*filters, &block) ⇒ Object

Define a callback to call before all organized interactors' #perform methods have been called.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  before_all_perform :print_starting

  organized MyInteractor1, MyInteractor2

  private

  def print_starting
    puts 'Starting'
  end
end

MyOrganizer.perform
"Starting"
"MyInteractor1"
"MyInteractor2"
#=> <MyOrganizer::Context>

Since:

  • v1.2.0



263
264
265
# File 'lib/active_interactor/organizer/callbacks.rb', line 263

def before_all_perform(*filters, &block)
  set_callback(:all_perform, :before, *filters, &block)
end

#before_each_perform(*filters, &block) ⇒ Object

Define a callback to call before each organized interactor's #perform method has been called.

Examples:

class MyInteractor1 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor1'
  end
end

class MyInteractor2 < ActiveInteractor::Base
  def perform
    puts 'MyInteractor2'
  end
end

class MyOrganizer < ActiveInteractor::Organizer
  before_each_perform :print_starting

  organized MyInteractor1, MyInteractor2

  private

  def print_starting
    puts 'Starting'
  end
end

MyOrganizer.perform
"Starting"
"MyInteractor1"
"Starting"
"MyInteractor2"
#=> <MyOrganizer::Context>

Since:

  • 0.1.3



141
142
143
# File 'lib/active_interactor/organizer/callbacks.rb', line 141

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