Class: ActiveInteractor::Base

Inherits:
Object
  • Object
show all
Extended by:
Interactor::Callbacks::ClassMethods, Interactor::Context::ClassMethods, Interactor::Perform::ClassMethods
Includes:
Interactor::Callbacks, Interactor::Context, Interactor::Perform, ActiveSupport::Callbacks
Defined in:
lib/active_interactor/base.rb

Overview

The base class all interactors should inherit from.

When Base is loaded by your application an ActiveSupport load hook is called with :active_interactor and Base.

Examples:

a basic interactor

class MyInteractor < ActiveInteractor::Base
  def perform
    # TODO: implement the perform method
  end

  def rollback
    # TODO: implement the rollback method
  end
end

Author:

Since:

  • 0.1.0

Direct Known Subclasses

Organizer::Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_context_validation(*args, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.after_perform(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.after_rollback(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.around_perform(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.around_rollback(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.before_context_validation(*args, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.before_perform(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.before_rollback(*filters, &block) ⇒ Object Originally defined in module Interactor::Callbacks::ClassMethods

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

.context_attribute(name, type = Type::Value.new, **options) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Call .attribute on the interactor class' context class

Since:

  • 1.0.1

.context_attribute_method?(attribute) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_attribute_missing(match, *args, &block) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 1.0.1

.context_attribute_namesObject Originally defined in module Interactor::Context::ClassMethods

Since:

  • 1.0.1

.context_attributes(*attributes) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Call .attributes on the interactor class' context class

Examples:

class MyInteractor < ActiveInteractor::Base
  context_attributes :first_name, :last_name
end

MyInteractor.context_class.attributes
#=> [:first_name, :last_name]

Since:

  • 0.1.0

.context_classConst Originally defined in module Interactor::Context::ClassMethods

Get the interactor class' context class. If no class is found or no class is set via the #contextualize_with method a new class is created.

Returns:

See Also:

Since:

  • 0.1.0

.context_clear_validators!(attribute) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_respond_to_without_attributes?(method, include_private_methods = false) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 1.0.1

.context_validate(*args, &block) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Call .validate on the interactor class' context class

Since:

  • 0.1.0

.context_validates(*attributes) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Call .validates on the interactor class' context class

Since:

  • 0.1.0

.context_validates!(*attributes) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Call .validates! on the interactor class' context class

Since:

  • 0.1.0

.context_validates_absence_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_acceptance_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_confirmation_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_each(*attr_names, &block) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_exclusion_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_format_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_inclusion_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_length_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_numericality_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_presence_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_size_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validates_with(*args, &block) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.context_validatorsObject Originally defined in module Interactor::Context::ClassMethods

Call .validators on the interactor class' context class

Since:

  • 0.1.0

.context_validators_on(*attributes) ⇒ Object Originally defined in module Interactor::Context::ClassMethods

Since:

  • 0.1.0

.contextualize_with(klass) ⇒ Const Originally defined in module Interactor::Context::ClassMethods

Set the interactor class' context class

Examples:

class User < ActiveRecord::Base
end

class MyInteractor < ActiveInteractor::Base
  contextualize_with :user
end

MyInteractor.context_class
#=> User

Parameters:

  • klass (Const, Symbol, String)

    the class to use as context

Returns:

Raises:

Since:

  • 1.0.0

.defer_after_callbacks_when_organizedObject Originally defined in module Interactor::Callbacks::ClassMethods

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

.perform(context = {}, options = {}) ⇒ Class Originally defined in module Interactor::Perform::ClassMethods

Initialize a new interactor instance and call its #perform method. This is the default api to interact with all interactors.

Examples:

class MyInteractor < ActiveInteractor::Base
  def perform
    puts "I performed"
  end
end

MyInteractor.perform
"I performed"
#=> <#MyInteractor::Context>

Parameters:

Returns:

Since:

  • 0.1.0

.perform!(context = {}, options = {}) ⇒ Class Originally defined in module Interactor::Perform::ClassMethods

Initialize a new interactor instance and call its #perform method without rescuing Error::ContextFailure.

Examples:

Calling .perform! without failure

class MyInteractor < ActiveInteractor::Base
  def perform
    puts "I performed"
  end
end

MyInteractor.perform!
"I performed"
#=> <#MyInteractor::Context>

Calling .perform! with failure

class MyInteractor < ActiveInteractor::Base
  def perform
    context.fail!
  end
end

MyInteractor.perform!
ActiveInteractor::Error::ContextFailure "<#MyInteractor::Context>"

Parameters:

Returns:

Raises:

Since:

  • 0.1.0

Instance Method Details

#context_attribute_missing(match, *args, &block) ⇒ Object Originally defined in module Interactor::Context

Call #attribute_missing on the interactor instance's context instance

Since:

  • 1.0.1

#context_attribute_namesObject Originally defined in module Interactor::Context

Call #attribute_names on the interactor instance's context instance

Since:

  • 1.0.1

#context_errorsObject Originally defined in module Interactor::Context

Call #errors on the interactor instance's context instance

Since:

  • 0.1.0

#context_fail!(errors = nil) ⇒ Object Originally defined in module Interactor::Context

Call #fail! on the interactor instance's context instance

Since:

  • 1.0.0

#context_invalid?(context = nil) ⇒ Object Originally defined in module Interactor::Context

Call #invalid? on the interactor instance's context instance

Since:

  • 0.1.0

#context_respond_to_without_attributes?(method, include_private_methods = false) ⇒ Object Originally defined in module Interactor::Context

Call #respond_to_without_attributes? on the interactor instance's context instance

Since:

  • 1.0.1

#context_rollback!Object Originally defined in module Interactor::Context

Call #rollback! on the interactor instance's context instance

Since:

  • 1.0.0

#context_valid?(context = nil) ⇒ Object Originally defined in module Interactor::Context

Call #valid? on the interactor instance's context instance

Since:

  • 0.1.0

#context_validate(context = nil) ⇒ Object Originally defined in module Interactor::Context

Call #validate on the interactor instance's context instance

Since:

  • 0.1.0

#context_validate!(context = nil) ⇒ Object Originally defined in module Interactor::Context

Call #validate! on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_absence_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_absence_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_acceptance_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_acceptance_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_confirmation_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_confirmation_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_exclusion_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_exclusion_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_format_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_format_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_inclusion_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_inclusion_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_length_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_length_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_numericality_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_numericality_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_presence_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_presence_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_size_of(*attr_names) ⇒ Object Originally defined in module Interactor::Context

Call #validates_size_of on the interactor instance's context instance

Since:

  • 0.1.0

#context_validates_with(*args, &block) ⇒ Object Originally defined in module Interactor::Context

Call #validates_with on the interactor instance's context instance

Since:

  • 0.1.0

#deep_dupBase Originally defined in module Interactor::Perform

Duplicate an interactor instance as well as it's #options and context instances.

Returns:

Since:

  • 1.0.0

#execute_performObject Originally defined in module Interactor::Perform

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the interactor instance's #perform with callbacks and validation.

Since:

  • 0.1.0

#execute_perform!Object Originally defined in module Interactor::Perform

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Run the interactor instance's #perform with callbacks and validation without rescuing Error::ContextFailure.

Since:

  • 0.1.0

#finalize_context!Class Originally defined in module Interactor::Context

Mark the interactor instance as called on the instance's context instance and return the context instance.

Returns:

Since:

  • 1.0.0

#initialize(context = {}) ⇒ Base Originally defined in module Interactor::Context

Initialize a new instance of Base

Parameters:

  • context (Hash, Class) (defaults to: {})

    attributes to assign to the interactor instance's context instance

Returns:

Since:

  • 0.1.0

#optionsOptions Originally defined in module Interactor::Perform

Options for the interactor #perform

Returns:

Since:

  • 1.0.0

#performObject Originally defined in module Interactor::Perform

This method is abstract.

interactors should override #perform with the appropriate steps and context mutations required for the interactor to do its work.

The steps to run when an interactor is called. An interactor's #perform method should never be called directly on an interactor instance and should instead be invoked by the interactor's class method .perform.

Examples:

class MyInteractor < ActiveInteractor::Base
  def perform
    context.first_name = 'Aaron'
  end
end

MyInteractor.perform
#=> <#MyInteractor::Context first_name='Aaron'>

Since:

  • 0.1.0

#rollbackObject Originally defined in module Interactor::Perform

This method is abstract.

interactors should override #rollback with the appropriate steps and context mutations required for the interactor to roll back its work.

The steps to run when an interactor fails. An interactor's #rollback method should never be called directly and is instead called by the interactor's context when #fail is called.

Examples:

class MyInteractor < ActiveInteractor::Base
  def perform
    context.first_name = 'Aaron'
    context.fail!
  end

  def rollback
    context.first_name = 'Bob'
  end
end

MyInteractor.perform
#=> <#MyInteractor::Context first_name='Bob'>

Since:

  • 0.1.0

#with_options(options) ⇒ self Originally defined in module Interactor::Perform

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Set options for an interactor's #perform

Parameters:

Returns:

Since:

  • 1.0.0