Implement ActionList::Entry#clear.
[kaya.git] / lib / interaction / operation_history.rb
blobd64e6673e5bae8ee1f03f19878d216e32c7711d2
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 class OperationHistory
9   attr_reader :current
10   
11   def initialize
12     @operations = []
13     @current = -1
14   end
15   
16   def <<(op)
17     @operations = if @current >= 0
18       @operations[0..@current]
19     else
20       []
21     end
22     @operations << op
23     @current += 1
24   end
25   
26   def undo_operation
27     if @current >= 0
28       op = @operations[@current]
29       @current -= 1
30       op
31     end
32   end
33   
34   def redo_operation
35     if @current < @operations.size
36       @current += 1
37       @operations[@current]
38     end
39   end
40   
41   def size
42     @operations.size
43   end
44 end