Class: PureMVC::MacroCommand

Inherits:
Notifier show all
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 executeis called, the MacroCommand instantiates and calls executeon each of its SubCommands in turn. Each SubCommand will be passed a reference to the original INotification that was passed to the MacroCommand’s executemethod.

Unlike SimpleCommand, your subclass should not override execute but instead override initialize_macro_command calling add_sub_commandonce for each SubCommand to be executed.

Constant Summary

Constants inherited from Notifier

Notifier::MULTITON_MSG

Instance Attribute Summary

Attributes inherited from Notifier

#multiton_key

Instance Method Summary collapse

Methods inherited from Notifier

#facade, #initialize_notifier, #send_notification

Constructor Details

#initializeMacroCommand

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.

Parameters:

  • factory (^() -)

    _ICommand] A block or callable that returns an instance of ICommand when called.



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.

Parameters:

  • notification (INotification)

    the notification object to be passed to each SubCommand.



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_commandObject

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.

Examples:

def initialize_macro_command
  add_sub_command  { FirstCommand.new }
  add_sub_command  { SecondCommand.new }
  add_sub_command  { ThirdCommand.new }
end


54
# File 'src/patterns/command/macro_command.rb', line 54

def initialize_macro_command; end