class Patterns::CommandList
CommandList -> Patterns::Command
Class that stores a list of commands and provides a way to add and remove them, get status and description, and execute and undo them.
Public Class Methods
public: Initializes the command list
Examples
=> command_list = CommandList.new
# File app/models/command/command_list.rb, line 13 def initialize @commands = [] @next = 0 end
Public Instance Methods
public: Adds a command to the list
-
cmd
- The command to be added to the list
Examples
=> command = Command.new('A new base command') => command_list.add_command(command)
# File app/models/command/command_list.rb, line 25 def add_command(cmd) @commands << cmd if @current == nil @current = cmd end end
public: Adds a list of commands to the list
-
cmds
- The list of commands to add to the command list
Examples
=> list_of_commands = [Command.new('A new base command'), Command.new('Another new base command')] => command_list.add_command(list_of_commands)
# File app/models/command/command_list.rb, line 39 def add_commands(cmds) @commands = @commands + cmds end
public: Returns the string of the descriptions of all commands in the list
Examples
=> command_list.description
# File app/models/command/command_list.rb, line 146 def description description = '' @commands.each { |cmd| description += cmd.description + "\n" } description end
not practical unless you want to run every command in the list; for demonstration only
# File app/models/command/command_list.rb, line 134 def execute @commands.each { |cmd| cmd.execute } end
public: Executes the next command in the list
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => command_list.execute_next
# File app/models/command/command_list.rb, line 49 def execute_next if @next == 0 @commands[@next].execute @next = @next + 1 else if @commands[@next] != nil @commands[@next].execute @next = @next + 1 else raise CommandList, 'No next command in list; index out of bounds' end end end
public: Returns the description of the last command executed
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => puts command_list.last_description
# File app/models/command/command_list.rb, line 97 def last_description if @next != 0 @commands[@next - 1].description else 'No commands have been executed yet' end end
public: Returns the status of the last command executed
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => puts command_list.last_status
# File app/models/command/command_list.rb, line 125 def last_status if @next != 0 @commands[@next - 1].status else 'No commands have been executed yet' end end
public: Returns the description of the next command in the list
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => puts command_list.next_description
# File app/models/command/command_list.rb, line 83 def next_description if @commands[@next] != nil @commands[@next].description else 'No more commands in list' end end
public: Returns the status of the next command in the list
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => puts command_list.next_status
# File app/models/command/command_list.rb, line 111 def next_status if @commands[@next] != nil @commands[@next].status else 'No more commands in list' end end
public: Returns the string of the status of all commands in the list
Examples
=> command_list.status
# File app/models/command/command_list.rb, line 156 def status status = '' @commands.each { |cmd| status += cmd.status + "\n"} status end
also only practical if you want to reverse every command in the list
# File app/models/command/command_list.rb, line 138 def undo @commands.reverse.each { |cmd| cmd.undo } end
public: Undoes the last command executed
Examples
=> command = Command.new('A new base command') => command_list.add_command(command) => command_list.execute_next => command_list.undo_last
# File app/models/command/command_list.rb, line 70 def undo_last if @next > 0 @commands[@next - 1].undo @next = @next - 1 end end