module Adama::Invoker::InstanceMethods

Public Instance Methods

_called() click to toggle source

Maintain an array of commands that have been called.

# File lib/adama/invoker.rb, line 89
def _called
  @called ||= []
end
call() click to toggle source

Iterate over the commands array, instantiate each command, run it, and add it to the called list.

# File lib/adama/invoker.rb, line 109
def call
  commands = self.commands.any? ? self.commands : self.class.commands
  commands.each do |command_klass|
    command = command_klass.new(kwargs)
    command.run
    _called << command
  end
end
rollback() click to toggle source

To unwind the invoker, we rollback the _called array in reverse order.

If anything fails in the command's rollback method we should raise and drop out of the process as we'll need to manually remove something.

# File lib/adama/invoker.rb, line 97
def rollback
  _called.reverse_each do |command|
    begin
      command.rollback
    rescue => error
      raise Errors::InvokerRollbackError.new(error: error, command: command, invoker: self, backtrace: error.backtrace)
    end
  end
end
run() click to toggle source

Internal instance method. Called by the included Command module's .call class method. We've overridden command's instance method because we don't want it to have optional rollback.

Always raises Errors::InvokerError and has the invoker and error attribute set. In the case where the error is raised within the invoker “call” instance method, we won't have access to error's command so need to test for it's existence.

# File lib/adama/invoker.rb, line 76
def run
  tap(&:call)
rescue => error
  rollback
  raise Errors::InvokerError.new(
    error: error,
    command: error.respond_to?(:command) ? error.command : nil,
    invoker: self,
    backtrace: error.backtrace
  )
end