module AyeCommander::Commander

Commander is a special command that lets you run several command in a succession. At the end it returns its own result containing a hash with the commands run.

Public Class Methods

new(**args) click to toggle source

A commander works with the following instance variables: command: The last executed command. Will be an anonymous empty command at

the beginning

executed: An array containing the executed commands

Calls superclass method
# File lib/aye_commander/commander.rb, line 87
def initialize(**args)
  super(command: self.class.command.new(args), executed: [])
end

Public Instance Methods

call() click to toggle source

This is the default call for a commander It basically just executes the commands saved in the executes array. This however can be overwritten by the user and define their own logic to execute different commands

# File lib/aye_commander/commander.rb, line 95
def call
  execute(*self.class.executes, abort_on_failure: true)
end

Private Instance Methods

execute(*commands, abort_on_failure: self.class.abort_on_failure?) click to toggle source

Execute will run the commands received, save the last executed command in @command instance variable and push it to the executed array.

It also comes with an option to to abort the Commander in case one of the command run fails.

# File lib/aye_commander/commander.rb, line 106
def execute(*commands, abort_on_failure: self.class.abort_on_failure?)
  commands.each do |command_class|
    args = command.to_hash
    options = { skip_cleanup: :command, skip_validations: :receives }
    @command = command_class.call(**args, **options)
    executed.push(command)

    if command.failure? && abort_on_failure
      fail!
      abort!
    end
  end
end