undo/redo support
[booh] / lib / booh / UndoHandler.rb
1 #!/usr/bin/ruby
2 #
3 #                         *  BOOH  *
4 #
5 # A.k.a `Best web-album Of the world, Or your money back, Humerus'.
6 #
7 # The acronyn sucks, however this is a tribute to Dragon Ball by
8 # Akira Toriyama, where the last enemy beaten by heroes of Dragon
9 # Ball is named "Boo". But there was already a free software project
10 # called Boo, so this one will be it "Booh". Or whatever.
11 #
12 #
13 # Copyright (c) 2004 Guillaume Cottenceau <gc3 at bluewin.ch>
14 #
15 # This software may be freely redistributed under the terms of the GNU
16 # public license version 2.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 module UndoHandler
23
24     @undo_actions = []
25     @redo_actions = []
26
27     module_function
28
29     def save_undo(closure, params)
30         @undo_actions << { :closure => closure, :params => params }
31     end
32
33     def undo
34         todo = @undo_actions.pop
35         redo_closure = todo[:closure].call(*todo[:params])
36         @redo_actions << { :redo => redo_closure, :undo => todo }
37         return !@undo_actions.empty?
38     end
39
40     def redo
41         redo_item = @redo_actions.pop
42         redo_item[:redo].call
43         @undo_actions << redo_item[:undo]
44         return !@redo_actions.empty?
45     end
46
47     def cleanup
48         @undo_actions.clear
49         @redo_actions.clear
50     end
51
52 end