class ActiveInteractor::Interactor::Worker

A worker class to call {Base interactor} {Interactor::Perform#perform perform} and {Interactor::Perform#rollback rollback} methods in a thread safe manner.

@api private @author Aaron Allen <hello@aaronmallen.me> @since 0.1.0

Attributes

context[R]
interactor[R]

Public Class Methods

new(interactor) click to toggle source

Initialize a new instance of {Worker}

@param interactor [Base] an {Base interactor} instance @return [Worker] a new instance of {Worker}

# File lib/active_interactor/interactor/worker.rb, line 16
def initialize(interactor)
  @interactor = interactor.deep_dup
end

Public Instance Methods

execute_perform() click to toggle source

Run the {Base interactor} instance's {Interactor::Perform#perform perform} with callbacks and validation.

@return [Class] a {ActiveInteractor::Context::Base context} instance

# File lib/active_interactor/interactor/worker.rb, line 23
def execute_perform
  execute_perform!
rescue Error::ContextFailure => e
  ActiveInteractor.logger.error("ActiveInteractor: #{e}")
  context
end
execute_perform!() click to toggle source

Run the {Base interactor} instance's {Interactor::Perform#perform perform} with callbacks and validation without rescuing {Error::ContextFailure}.

@raise [Error::ContextFailure] if the {Base interactor} fails it's {ActiveInteractor::Context::Base context} @return [Class] a {ActiveInteractor::Context::Base context} instance

# File lib/active_interactor/interactor/worker.rb, line 35
def execute_perform!
  execute_context!
rescue StandardError => e
  handle_error(e)
end
execute_rollback() click to toggle source

Run the {Base interactor} instance's {Interactor::Perform#rollback rollback} with callbacks

@return [Boolean] `true` if context was successfully rolled back

# File lib/active_interactor/interactor/worker.rb, line 44
def execute_rollback
  return if interactor.options.skip_rollback

  execute_interactor_rollback!
end

Private Instance Methods

execute_context!() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 54
def execute_context!
  if interactor.options.skip_perform_callbacks
    execute_context_with_validation_check!
  else
    execute_context_with_callbacks!
  end
end
execute_context_with_callbacks!() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 62
def execute_context_with_callbacks!
  interactor.run_callbacks :perform do
    execute_context_with_validation_check!
    @context = interactor.finalize_context!
  end
end
execute_context_with_validation!() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 69
def execute_context_with_validation!
  validate_on_calling
  interactor.perform
  validate_on_called
end
execute_context_with_validation_check!() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 75
def execute_context_with_validation_check!
  return interactor.perform unless interactor.options.validate

  execute_context_with_validation!
end
execute_interactor_rollback!() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 81
def execute_interactor_rollback!
  return interactor.context_rollback! if interactor.options.skip_rollback_callbacks

  interactor.run_callbacks :rollback do
    interactor.context_rollback!
  end
end
handle_error(exception) click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 89
def handle_error(exception)
  @context = interactor.finalize_context!
  execute_rollback
  raise exception
end
validate_context(validation_context = nil) click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 95
def validate_context(validation_context = nil)
  interactor.run_callbacks :validation do
    interactor.context_valid?(validation_context)
  end
end
validate_on_called() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 107
def validate_on_called
  return unless interactor.options.validate_on_called

  interactor.context_fail! unless validate_context(:called)
end
validate_on_calling() click to toggle source
# File lib/active_interactor/interactor/worker.rb, line 101
def validate_on_calling
  return unless interactor.options.validate_on_calling

  interactor.context_fail! unless validate_context(:calling)
end