Module: ActiveInteractor::Interactor::Callbacks::ClassMethods

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

Overview

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

Author:

  • Aaron Allen

Since:

  • 0.1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

Since:

  • 0.1.0



264
265
266
267
268
# File 'lib/active_interactor/interactor/callbacks.rb', line 264

def self.extended(base)
  base.class_eval do
    class_attribute :after_callbacks_deferred_when_organized, instance_writer: false, default: false
  end
end

Instance Method Details

#after_context_validation(*args, &block) ⇒ Object

Define a callback to call after validation has been run on an interactor instance's context instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  after_context_validation :ensure_name_is_aaron
  context_validates :name, presence: true

  private

  def ensure_name_is_aaron
    context.name = 'Aaron'
  end
end

result = MyInteractor.perform(name: 'Bob')
result.name
#=> 'Aaron'

result = MyInteractor.perform({ name: 'Bob' }, { validate: false })
result.name
#=> 'Bob'

Since:

  • 0.1.0



41
42
43
44
# File 'lib/active_interactor/interactor/callbacks.rb', line 41

def after_context_validation(*args, &block)
  options = normalize_options(args.extract_options!.dup.merge(prepend: true))
  set_callback(:validation, :after, *args, options, &block)
end

#after_perform(*filters, &block) ⇒ Object

Define a callback to call after #perform has been called on an interactor instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  after_perform :print_done

  def perform
    puts 'Performing'
  end

  private

  def print_done
    puts 'Done'
  end
end

MyInteractor.perform
"Performing"
"Done"
#=> <#MyInteractor::Context>

Since:

  • 0.1.0



68
69
70
# File 'lib/active_interactor/interactor/callbacks.rb', line 68

def after_perform(*filters, &block)
  set_callback(:perform, :after, *filters, &block)
end

#after_rollback(*filters, &block) ⇒ Object

Define a callback to call after #rollback has been called on an interactor instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  after_rollback :print_done

  def perform
    context.fail!
  end

  def rollback
    puts 'Rolling Back'
  end

  private

  def print_done
    puts 'Done'
  end
end

MyInteractor.perform
"Rolling Back"
"Done"
#=> <MyInteractor::Context>

Since:

  • 0.1.0



98
99
100
# File 'lib/active_interactor/interactor/callbacks.rb', line 98

def after_rollback(*filters, &block)
  set_callback(:rollback, :after, *filters, &block)
end

#around_perform(*filters, &block) ⇒ Object

Define a callback to call around #perform invokation on an interactor instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  around_perform :track_time

  def perform
    sleep(1)
  end

  private

  def track_time
    context.start_time = Time.now.utc
    yield
    context.end_time = Time.now.utc
  end
end

result = MyInteractor.perform
result.start_time
#=> 2019-01-01 00:00:00 UTC

result.end_time
#=> 2019-01-01 00:00:01 UTC

Since:

  • 0.1.0



128
129
130
# File 'lib/active_interactor/interactor/callbacks.rb', line 128

def around_perform(*filters, &block)
  set_callback(:perform, :around, *filters, &block)
end

#around_rollback(*filters, &block) ⇒ Object

Define a callback to call around #rollback invokation on an interactor instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  around_rollback :track_time

  def perform
    context.fail!
  end

  def rollback
    sleep(1)
  end

  private

  def track_time
    context.start_time = Time.now.utc
    yield
    context.end_time = Time.now.utc
  end
end

result = MyInteractor.perform
result.start_time
#=> 2019-01-01 00:00:00 UTC

result.end_time
#=> 2019-01-01 00:00:01 UTC

Since:

  • 0.1.0



162
163
164
# File 'lib/active_interactor/interactor/callbacks.rb', line 162

def around_rollback(*filters, &block)
  set_callback(:rollback, :around, *filters, &block)
end

#before_context_validation(*args, &block) ⇒ Object

Define a callback to call before validation has been run on an interactor instance's context instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  before_context_validation :ensure_name_is_aaron
  context_validates :name, inclusion: { in: %w[Aaron] }

  private

  def ensure_name_is_aaron
    context.name = 'Aaron'
  end
end

result = MyInteractor.perform(name: 'Bob')
result.successful?
#=> true

result.name
#=> 'Aaron'

result = MyInteractor.perform({ name: 'Bob' }, { validate: false })
result.name
#=> 'Bob'

Since:

  • 0.1.0



191
192
193
194
# File 'lib/active_interactor/interactor/callbacks.rb', line 191

def before_context_validation(*args, &block)
  options = normalize_options(args.extract_options!.dup)
  set_callback(:validation, :before, *args, options, &block)
end

#before_perform(*filters, &block) ⇒ Object

Define a callback to call before #perform has been called on an interactor instance

Examples:

class MyInteractor < ActiveInteractor::Base
  before_perform :print_starting

  def perform
    puts 'Performing'
  end

  private

  def print_starting
    puts 'Starting'
  end
end

MyInteractor.perform
"Starting"
"Performing"
#=> <#MyInteractor::Context>

Since:

  • 0.1.0



218
219
220
# File 'lib/active_interactor/interactor/callbacks.rb', line 218

def before_perform(*filters, &block)
  set_callback(:perform, :before, *filters, &block)
end

#before_rollback(*filters, &block) ⇒ Object

Define a callback to call before #rollback has been called on an interactor instance.

Examples:

class MyInteractor < ActiveInteractor::Base
  before_rollback :print_starting

  def perform
    context.fail!
  end

  def rollback
    puts 'Rolling Back'
  end

  private

  def print_starting
    puts 'Starting'
  end
end

MyInteractor.perform
"Starting"
"Rolling Back"
#=> <#MyInteractor::Context>

Since:

  • 0.1.0



248
249
250
# File 'lib/active_interactor/interactor/callbacks.rb', line 248

def before_rollback(*filters, &block)
  set_callback(:rollback, :before, *filters, &block)
end

#defer_after_callbacks_when_organizedObject

Set after_callbacks_deferred_when_organized to true

Examples:

a basic organizer set to defer 'after' callbacks when organized

class MyOrganizer < ActiveInteractor::Organizer::Base
  defer_after_callbacks_when_organized
end

Since:

  • v1.2.0



260
261
262
# File 'lib/active_interactor/interactor/callbacks.rb', line 260

def defer_after_callbacks_when_organized
  self.after_callbacks_deferred_when_organized = true
end