lib/sup/undo.rb (901B) - raw
1 module Redwood
2
3 ## Implements a single undo list for the Sup instance
4 ##
5 ## The basic idea is to keep a list of lambdas to undo
6 ## things. When an action is called (such as 'archive'),
7 ## a lambda is registered with UndoManager that will
8 ## undo the archival action
9
10 class UndoManager
11 include Redwood::Singleton
12
13 def initialize
14 @@actionlist = []
15 end
16
17 def register desc, *actions, &b
18 actions = [*actions.flatten]
19 actions << b if b
20 raise ArgumentError, "need at least one action" unless actions.length > 0
21 @@actionlist.push :desc => desc, :actions => actions
22 end
23
24 def undo
25 unless @@actionlist.empty?
26 actionset = @@actionlist.pop
27 actionset[:actions].each { |action| action.call }
28 BufferManager.flash "undid #{actionset[:desc]}"
29 else
30 BufferManager.flash "nothing more to undo!"
31 end
32 end
33
34 def clear
35 @@actionlist = []
36 end
37 end
38 end