Module: AyeCommander::Commander::ClassMethods
- Included in:
- AyeCommander::Commander
- Defined in:
- lib/aye_commander/commander.rb
Overview
Eventhough the Commander is basically a Command, it does come with some minor tweaking to keep it simple to understand and consistant
Instance Method Summary collapse
-
#abort_on_failure(value = true) ⇒ Object
Can be used to set a default behavior of a Commander that overwrites call.
-
#abort_on_failure? ⇒ Boolean
Returns the abort_on_failure.
-
#call(skip_cleanup: false, **args) ⇒ Object
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.
-
#command ⇒ Object
Returns an anonymous command class to be used to initialize the received commander args.
-
#execute(*args) ⇒ Object
Adds the received arguments to the executes array.
-
#executes ⇒ Object
Returns the executes array.
-
#included(includer) ⇒ Object
This ensure that Commander specific class methods and commander specific class instance variables are included when the Commander is included.
-
#inherited(inheriter) ⇒ Object
This ensures that Commander specific instance variables become available for any class inheriting from another that includes the command.
-
#prepare_commander_result(commander) ⇒ Object
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.
Instance Method Details
#abort_on_failure(value = true) ⇒ Object
Can be used to set a default behavior of a Commander that overwrites call.
70 71 72 |
# File 'lib/aye_commander/commander.rb', line 70 def abort_on_failure(value = true) @abort_on_failure = value end |
#abort_on_failure? ⇒ Boolean
Returns the abort_on_failure
75 76 77 |
# File 'lib/aye_commander/commander.rb', line 75 def abort_on_failure? @abort_on_failure end |
#call(skip_cleanup: false, **args) ⇒ Object
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
33 34 35 36 37 |
# 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 ⇒ Object
Returns an anonymous command class to be used to initialize the received commander args.
54 55 56 |
# File 'lib/aye_commander/commander.rb', line 54 def command @command ||= Class.new.send(:include, Command) end |
#execute(*args) ⇒ Object
Adds the received arguments to the executes array
59 60 61 |
# File 'lib/aye_commander/commander.rb', line 59 def execute(*args) executes.concat(args) end |
#executes ⇒ Object
Returns the executes array
64 65 66 |
# File 'lib/aye_commander/commander.rb', line 64 def executes @executes ||= [] end |
#included(includer) ⇒ Object
This ensure that Commander specific class methods and commander specific class instance variables are included when the Commander is included
11 12 13 14 15 16 |
# 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) ⇒ Object
This ensures that Commander specific instance variables become available for any class inheriting from another that includes the command
20 21 22 23 24 |
# 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) ⇒ Object
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.
43 44 45 46 47 48 49 50 |
# 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 |