1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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.
12 undo_op.execute(:undoable => false) if undo_op
16 # Add new moves at the end of a history
21 def initialize(history, *items)
26 def execute(opts = { })
27 index = @history.add_items(opts[:extra] || { }, *@items)
29 if opts.fetch(:undoable, true)
30 @undo_op = TruncateOperation.new(@history, index)
35 # Truncate the history from the given index to
38 class TruncateOperation
41 def initialize(history, index)
47 undo_op.execute(:undoable => false,
48 :extra => { :go_to_end => false })
51 def execute(opts = { })
52 items = @history.remove_items_at(@index, opts[:extra] || { })
53 if opts.fetch(:undoable, true)
54 @undo_op = MoveOperation.new(@history, *items)
59 class CompositeOperation
60 def initialize(*actions)
65 @actions.reverse.each do |action|
70 def execute(opts = { })
71 @actions.each do |action|
77 module OperationInterface
78 class OperationBuilder
80 def initialize(history)
85 def method_missing(m, *args)
86 klass = eval(m.to_s.capitalize + "Operation")
87 @ops << klass.new(@history, *args)
91 def operation(opts = { })
92 builder = OperationBuilder.new(self)
101 CompositeOperation.new(*ops)
103 operations << op if opts.fetch(:save, true)