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.



83
84
85
# File 'lib/adama/invoker.rb', line 83

def _called
  @called ||= []
end

#callObject

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



103
104
105
106
107
108
109
# File 'lib/adama/invoker.rb', line 103

def call
  self.class.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.



91
92
93
94
95
96
97
98
99
# File 'lib/adama/invoker.rb', line 91

def rollback
  _called.reverse_each do |command|
    begin
      command.rollback
    rescue => error
      raise Errors::InvokerRollbackError.new(error: error, command: command, invoker: self)
    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.



71
72
73
74
75
76
77
78
79
80
# File 'lib/adama/invoker.rb', line 71

def run
  call
rescue => error
  rollback
  raise Errors::InvokerError.new(
    error: error,
    command: error.respond_to?(:command) ? error.command : nil,
    invoker: self
  )
end