Class: Fidgit::History
Overview
Manages a history of actions, along with doing, undoing and redoing those actions.
Defined Under Namespace
Classes: Action
Constant Summary
- DEFAULT_MAX_SIZE =
Maximum number of actions in the History before Actions are deleted.
250
Instance Method Summary (collapse)
-
- (Boolean) can_redo?
Is there an action that has been undone that can now be redone?.
-
- (Boolean) can_undo?
Is there an action that can be undone?.
-
- (Object) do(action)
Perform a History::Action, adding it to the history.
-
- (History) initialize(max_size = DEFAULT_MAX_SIZE)
constructor
A new instance of History.
-
- (Object) redo
Redo the last action that was undone.
-
- (Object) replace_last(action)
Perform a History::Action, replacing the last action that was performed.
-
- (Object) undo
Undo the last action that was performed.
Constructor Details
- (History) initialize(max_size = DEFAULT_MAX_SIZE)
A new instance of History
24 25 26 27 28 |
# 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 |
Instance Method Details
- (Boolean) can_redo?
Is there an action that has been undone that can now be redone?
22 |
# File 'lib/fidgit/history.rb', line 22 def can_redo?; @last_done < (@actions.size - 1); end |
- (Boolean) can_undo?
Is there an action that can be undone?
19 |
# File 'lib/fidgit/history.rb', line 19 def can_undo?; @last_done >= 0; end |
- (Object) do(action)
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.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# 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 |
- (Object) redo
Redo the last action that was undone.
82 83 84 85 86 87 88 89 |
# 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 |
- (Object) replace_last(action)
Perform a History::Action, replacing the last action that was performed.
61 62 63 64 65 66 67 68 69 |
# 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 |
- (Object) undo
Undo the last action that was performed.
72 73 74 75 76 77 78 79 |
# 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 |