module AyeCommander::Commander::ClassMethods

Eventhough the Commander is basically a Command, it does come with some minor tweaking to keep it simple to understand and consistant

Public Instance Methods

abort_on_failure(value = true) click to toggle source

Can be used to set a default behavior of a Commander that overwrites call.

# File lib/aye_commander/commander.rb, line 70
def abort_on_failure(value = true)
  @abort_on_failure = value
end
abort_on_failure?() click to toggle source

Returns the abort_on_failure

# File lib/aye_commander/commander.rb, line 75
def abort_on_failure?
  @abort_on_failure
end
call(skip_cleanup: false, **args) click to toggle source

Override of Command.call It's almost identical to a normal command call, the only difference being that it has to prepare the commander result before sending it.

This was previously done through hooks, but the idea was scrapped to avoid inconsistencies with the command instance variable during after and aborted hooks

Calls superclass method
# File lib/aye_commander/commander.rb, line 33
def call(skip_cleanup: false, **args)
  commander = super(skip_cleanup: :command, **args)
  prepare_commander_result(commander)
  result(commander, skip_cleanup)
end
command() click to toggle source

Returns an anonymous command class to be used to initialize the received commander args.

# File lib/aye_commander/commander.rb, line 54
def command
  @command ||= Class.new.send(:include, Command)
end
execute(*args) click to toggle source

Adds the received arguments to the executes array

# File lib/aye_commander/commander.rb, line 59
def execute(*args)
  executes.concat(args)
end
executes() click to toggle source

Returns the executes array

# File lib/aye_commander/commander.rb, line 64
def executes
  @executes ||= []
end
included(includer) click to toggle source

This ensure that Commander specific class methods and commander specific class instance variables are included when the Commander is included

Calls superclass method
# File lib/aye_commander/commander.rb, line 11
def included(includer)
  super
  includer.extend ClassMethods
  includer.instance_variable_set :@executes, @executes
  includer.instance_variable_set :@abort_on_failure, @abort_on_failure
end
inherited(inheriter) click to toggle source

This ensures that Commander specific instance variables become available for any class inheriting from another that includes the command

Calls superclass method
# File lib/aye_commander/commander.rb, line 20
def inherited(inheriter)
  super
  inheriter.instance_variable_set :@executes, @executes
  inheriter.instance_variable_set :@abort_on_failure, @abort_on_failure
end
prepare_commander_result(commander) click to toggle source

This method is always run before retuning the result of the commander It basically removes command instance variable since it's only relevant during the execution of the commander itself. It also assigns the ivars of the last executed command to itself.

# File lib/aye_commander/commander.rb, line 43
def prepare_commander_result(commander)
  commander.instance_exec do
    command.to_hash.each do |name, value|
      instance_variable_set to_ivar(name), value
    end
    remove!(:command)
  end
end