Class: Command::UndoStack

Inherits:
Object
  • Object
show all
Defined in:
lib/command-set/command.rb

Overview

A thin wrapper on Array to maintain undo/redo state.

Instance Method Summary collapse

Constructor Details

#initializeUndoStack

Returns a new instance of UndoStack.



7
8
9
10
# File 'lib/command-set/command.rb', line 7

def initialize()
  @stack = []
  @now = 0
end

Instance Method Details

#add(cmd) ⇒ Object



12
13
14
15
16
# File 'lib/command-set/command.rb', line 12

def add(cmd)
  @stack.slice!(0,@now)
  @now=0
  @stack.unshift(cmd)
end

#get_redoObject



27
28
29
30
31
32
33
# File 'lib/command-set/command.rb', line 27

def get_redo
  if @now <= 0
    raise CommandException, "Can't redo"
  end
  @now-=1
  return @stack[@now]
end

#get_undoObject



18
19
20
21
22
23
24
25
# File 'lib/command-set/command.rb', line 18

def get_undo
  if @now > (@stack.length - 1) or @stack.length == 0
    raise CommandException, "No more commands to undo" 
  end
  cmd = @stack[@now]
  @now+=1
  return cmd
end