Class: Command::UndoStack
- Inherits:
-
Object
- Object
- Command::UndoStack
- Defined in:
- lib/command-set/command.rb
Overview
A thin wrapper on Array to maintain undo/redo state.
Instance Method Summary collapse
- #add(cmd) ⇒ Object
- #get_redo ⇒ Object
- #get_undo ⇒ Object
-
#initialize ⇒ UndoStack
constructor
A new instance of UndoStack.
Constructor Details
#initialize ⇒ UndoStack
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_redo ⇒ Object
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_undo ⇒ Object
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 |