Class: PureMVC::MacroCommand
- Defined in:
- src/patterns/command/macro_command.rb
Overview
A base ICommand implementation that executes other ICommand instances.
A MacroCommand maintains a list of ICommand class references called SubCommands.
When execute
is called, the MacroCommand instantiates and calls execute
on each of its SubCommands in turn. Each SubCommand will be passed a reference to the original INotification that was passed to the MacroCommand’s execute
method.
Unlike SimpleCommand, your subclass should not override execute
but instead override initialize_macro_command
calling add_sub_command
once for each SubCommand to be executed.
Constant Summary
Constants inherited from Notifier
Instance Attribute Summary
Attributes inherited from Notifier
Instance Method Summary collapse
-
#add_sub_command(&factory) ⇒ Object
Add a SubCommand.
-
#execute(notification) ⇒ Object
Execute this MacroCommand’s SubCommands.
-
#initialize ⇒ MacroCommand
constructor
Constructor.
-
#initialize_macro_command ⇒ Object
Initialize the MacroCommand.
Methods inherited from Notifier
#facade, #initialize_notifier, #send_notification
Constructor Details
#initialize ⇒ MacroCommand
Constructor.
You should not need to define a constructor, instead, override the initialize_macro_command
method.
If your subclass does define a constructor, be sure to call super
33 34 35 36 37 |
# File 'src/patterns/command/macro_command.rb', line 33 def initialize super() @sub_commands = [] initialize_macro_command end |
Instance Method Details
#add_sub_command(&factory) ⇒ Object
Add a SubCommand. SubCommands will be called in First In/First Out (FIFO) order.
60 61 62 |
# File 'src/patterns/command/macro_command.rb', line 60 def add_sub_command(&factory) @sub_commands << factory end |
#execute(notification) ⇒ Object
Execute this MacroCommand’s SubCommands.
The SubCommands will be called in First In/First Out (FIFO) order.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'src/patterns/command/macro_command.rb', line 69 def execute(notification) while @sub_commands.any? # @type factory: [^() -> ICommand] factory = @sub_commands.shift # @type var command: _ICommand command = factory.call command.initialize_notifier(@multiton_key) command.execute(notification) end end |
#initialize_macro_command ⇒ Object
Initialize the MacroCommand.
In your subclass, override this method to initialize the MacroCommand’s SubCommand list with ICommand factory references, like this:
Note that SubCommands may be any ICommand implementor; MacroCommands or SimpleCommands are both acceptable.
54 |
# File 'src/patterns/command/macro_command.rb', line 54 def initialize_macro_command; end |