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

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

Overview

Since:

  • 0.0.1

Instance Method Summary collapse

Instance Method Details

#after_context_validation(*args, &block) ⇒ Object

Define a callback to call after ‘#valid?` has been invoked on an

interactor's context

Examples:

Implement an after_context_validation callback

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

  def ensure_name_is_aaron
    context.name = 'Aaron'
  end
end

context = MyInteractor.perform(name: 'Bob')
#=> <MyInteractor::Context name='Bob'>

context.valid?
#=> false

context.name
#=> 'Aaron'

context.valid?
#=> true

Since:

  • 0.0.1



53
54
55
56
# File 'lib/active_interactor/interactor/callbacks.rb', line 53

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 Base.perform has been invoked

Examples:

class MyInteractor < ActiveInteractor::Base
  after_perform :print_done

  def perform
    puts 'Performing'
  end

  def print_done
    puts 'Done'
  end
end

MyInteractor.perform(name: 'Aaron')
"Performing"
"Done"
#=> <MyInteractor::Context name='Aaron'>

Since:

  • 0.0.1



77
78
79
# File 'lib/active_interactor/interactor/callbacks.rb', line 77

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

#after_rollback(*filters, &block) ⇒ Object

Define a callback to call after Base#rollback has been invoked

Examples:

class MyInteractor < ActiveInteractor::Base
  after_rollback :print_done

  def rollback
    puts 'Rolling back'
  end

  def print_done
    puts 'Done'
  end
end

context = MyInteractor.perform(name: 'Aaron')
#=> <MyInteractor::Context name='Aaron'>

context.rollback!
"Rolling back"
"Done"

Since:

  • 0.0.1



102
103
104
# File 'lib/active_interactor/interactor/callbacks.rb', line 102

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

#allow_context_to_be_invalidObject

By default an interactor context will fail if it is deemed

invalid before or after the {ActiveInteractor::Base.perform} method
is invoked.  Calling this method on an interactor class
will not invoke {ActiveInteractor::Context::Base#fail!} if the
context is invalid.

Since:

  • 0.0.1



111
112
113
# File 'lib/active_interactor/interactor/callbacks.rb', line 111

def allow_context_to_be_invalid
  self.__fail_on_invalid_context = false
end

#around_perform(*filters, &block) ⇒ Object

Define a callback to call around Base.perform invokation

Examples:

class MyInteractor < ActiveInteractor::Base
  around_perform :track_time

  def perform
    sleep(1)
  end

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

context = MyInteractor.perform(name: 'Aaron')
#=> <MyInteractor::Context name='Aaron'>

context.start_time
#=> 2019-01-01 00:00:00 UTC

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

Since:

  • 0.0.1



140
141
142
# File 'lib/active_interactor/interactor/callbacks.rb', line 140

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

#around_rollback(*filters, &block) ⇒ Object

Define a callback to call around Base#rollback invokation

Examples:

class MyInteractor < ActiveInteractor::Base
  around_rollback :track_time

  def rollback
    sleep(1)
  end

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

context = MyInteractor.perform(name: 'Aaron')
#=> <MyInteractor::Context name='Aaron'>

context.rollback!
#=> true

context.start_time
#=> 2019-01-01 00:00:00 UTC

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

Since:

  • 0.0.1



172
173
174
# File 'lib/active_interactor/interactor/callbacks.rb', line 172

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

#before_context_validation(*args, &block) ⇒ Object

Define a callback to call before ‘#valid?` has been invoked on an

interactor's context

Examples:

Implement an after_context_validation callback

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

  def set_name_aaron
    context.name = 'Aaron'
  end
end

context = MyInteractor.perform(name: 'Bob')
#=> <MyInteractor::Context name='Bob'>

context.valid?
#=> true

context.name
#=> 'Aaron'

Since:

  • 0.0.1



197
198
199
200
# File 'lib/active_interactor/interactor/callbacks.rb', line 197

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 Base.perform has been invoked

Examples:

class MyInteractor < ActiveInteractor::Base
  before_perform :print_start

  def perform
    puts 'Performing'
  end

  def print_start
    puts 'Start'
  end
end

MyInteractor.perform(name: 'Aaron')
"Start"
"Performing"
#=> <MyInteractor::Context name='Aaron'>

Since:

  • 0.0.1



221
222
223
# File 'lib/active_interactor/interactor/callbacks.rb', line 221

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

#before_rollback(*filters, &block) ⇒ Object

Define a callback to call before Base#rollback has been invoked

Examples:

class MyInteractor < ActiveInteractor::Base
  before_rollback :print_start

  def rollback
    puts 'Rolling Back'
  end

  def print_start
    puts 'Start'
  end
end

context = MyInteractor.perform(name: 'Aaron')
#=> <MyInteractor::Context name='Aaron'>

context.rollback!
"Start"
"Rolling Back"
#=> true

Since:

  • 0.0.1



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

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

#clean_context_on_completionObject

Calling this method on an interactor class will invoke

{ActiveInteractor::Context::Base#clean!} on the interactor's
context instance after {ActiveInteractor::Base.perform}
is invoked.

Since:

  • 0.0.1



255
256
257
# File 'lib/active_interactor/interactor/callbacks.rb', line 255

def clean_context_on_completion
  self.__clean_after_perform = true
end