Module: Adama::Invoker::InstanceMethods

Defined in:
lib/adama/invoker.rb

Instance Method Summary collapse

Instance Method Details

#_calledObject

Maintain an array of commands that have been called.



89
90
91
# File 'lib/adama/invoker.rb', line 89

def _called
  @called ||= []
end

#callObject

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



109
110
111
112
113
114
115
116
# 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

#rollbackObject

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.



97
98
99
100
101
102
103
104
105
# 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

#runObject

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.



76
77
78
79
80
81
82
83
84
85
86
# 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