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
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
Returns the abort_on_failure
# File lib/aye_commander/commander.rb, line 75 def abort_on_failure? @abort_on_failure end
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
# 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
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
Adds the received arguments to the executes array
# File lib/aye_commander/commander.rb, line 59 def execute(*args) executes.concat(args) end
Returns the executes array
# File lib/aye_commander/commander.rb, line 64 def executes @executes ||= [] end
This ensure that Commander
specific class methods and commander specific class instance variables are included when the Commander
is included
# 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
This ensures that Commander
specific instance variables become available for any class inheriting from another that includes the command
# 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
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