class Fidgit::History
Manages a history of actions, along with doing, undoing and redoing those actions.
Constants
- DEFAULT_MAX_SIZE
Maximum number of actions in the
History
before Actions are deleted.
Public Class Methods
new(max_size = DEFAULT_MAX_SIZE)
click to toggle source
# File lib/fidgit/history.rb, line 24 def initialize(max_size = DEFAULT_MAX_SIZE) @max_size = max_size @actions = [] @last_done = -1 # Last command that was performed. end
Public Instance Methods
can_redo?()
click to toggle source
Is there an action that has been undone that can now be redone?
# File lib/fidgit/history.rb, line 22 def can_redo?; @last_done < (@actions.size - 1); end
can_undo?()
click to toggle source
Is there an action that can be undone?
# File lib/fidgit/history.rb, line 19 def can_undo?; @last_done >= 0; end
do(action)
click to toggle source
Perform a History::Action
, adding it to the history. If there are currently any actions that have been undone, they will be permanently lost and cannot be redone.
@param [History::Action] action Action
to be performed
# File lib/fidgit/history.rb, line 34 def do(action) raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action # Remove all undone actions when a new one is performed. if can_redo? if @last_done == -1 @actions.clear else @actions = @actions[0..@last_done] end end # If history is too big, remove the oldest action. if @actions.size >= @max_size @actions.shift end @last_done = @actions.size @actions << action action.do nil end
redo()
click to toggle source
Redo the last action that was undone.
# File lib/fidgit/history.rb, line 82 def redo raise "Can't redo if there are no commands in the future" unless can_redo? @last_done += 1 @actions[@last_done].do nil end
replace_last(action)
click to toggle source
Perform a History::Action
, replacing the last action that was performed.
@param [History::Action] action Action
to be performed
# File lib/fidgit/history.rb, line 61 def replace_last(action) raise ArgumentError, "Parameter, 'action', expected to be a #{Action}, but received: #{action}" unless action.is_a? Action @actions[@last_done].undo @actions[@last_done] = action action.do nil end
undo()
click to toggle source
Undo the last action that was performed.
# File lib/fidgit/history.rb, line 72 def undo raise "Can't undo unless there are commands in past" unless can_undo? @actions[@last_done].undo @last_done -= 1 nil end