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

new() click to toggle source

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

add_command(cmd) click to toggle source

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
add_commands(cmds) click to toggle source

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
description() click to toggle source

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
execute() click to toggle source

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
execute_next() click to toggle source

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
last_description() click to toggle source

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
last_status() click to toggle source

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
next_description() click to toggle source

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
next_status() click to toggle source

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
status() click to toggle source

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
undo() click to toggle source

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
undo_last() click to toggle source

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