class UndoManager
Responsibilities
-
Manage undo stack
-
Manage redo stack
-
clear redo stack when new undo command is added
Collaborators
Attributes
redo_stack[R]
undo_stack[R]
Public Class Methods
new(undo_stack = [])
click to toggle source
Initializes a new UndoManager
. @param undo_stack
[Array, optional]
# File lib/undo_manager.rb, line 13 def initialize(undo_stack = []) @undo_stack = undo_stack @redo_stack = [] @total_ops_counter = 0 end
Public Instance Methods
can_redo?()
click to toggle source
Returns true if there is at least one command to be redone.
# File lib/undo_manager.rb, line 20 def can_redo? @redo_stack.any? end
can_undo?()
click to toggle source
Returns true if there is at least one command to be undone.
# File lib/undo_manager.rb, line 25 def can_undo? @undo_stack.any? end
record_new_command(command)
click to toggle source
Pushes a new command onto the undo_stack. @param command [#do, undo] a command object that responds to do and undo @return [Integer] number of total commands
# File lib/undo_manager.rb, line 32 def record_new_command(command) @redo_stack = [] # clear redo stack @undo_stack.push(command) end
redo_command()
click to toggle source
Redoes command at top of redo_stack. @return [Command, nil] the redone command or nil if nothing was redone
# File lib/undo_manager.rb, line 39 def redo_command o = @redo_stack.pop if o o.do # redo the command @undo_stack.push(o) end o end
undo_command()
click to toggle source
Undoes command at top of undo_stack. @return [Command, nil] the undone command or nil if nothing was undone
# File lib/undo_manager.rb, line 50 def undo_command o = @undo_stack.pop if o o.undo # undo the command @redo_stack.push(o) end o end