Class: Reight::History
- Inherits:
-
Object
- Object
- Reight::History
- Defined in:
- lib/reight/history.rb
Class Method Summary collapse
- .dump(xdos, &dump_object) ⇒ Object
- .load(hash, &restore_object) ⇒ Object
- .restore(xdos, &restore_object) ⇒ Object
Instance Method Summary collapse
- #append(*actions) ⇒ Object
- #begin_grouping(*args, **kwargs, &block) ⇒ Object (also: #group)
- #can_redo? ⇒ Boolean
- #can_undo? ⇒ Boolean
- #disable(*args, **kwargs, &block) ⇒ Object
- #disabled ⇒ Object
- #disabled? ⇒ Boolean
- #enable(state = true) ⇒ Object
- #enabled ⇒ Object
- #enabled? ⇒ Boolean
- #end_grouping ⇒ Object
-
#initialize(undos = [], redos = []) ⇒ History
constructor
A new instance of History.
- #redo(&block) ⇒ Object
- #to_h(&dump_object) ⇒ Object
- #undo(&block) ⇒ Object
- #updated(&block) ⇒ Object
Constructor Details
#initialize(undos = [], redos = []) ⇒ History
Returns a new instance of History.
3 4 5 6 7 |
# File 'lib/reight/history.rb', line 3 def initialize(undos = [], redos = []) super() @undos, @redos = undos, redos @group, @enabled = nil, true end |
Class Method Details
.dump(xdos, &dump_object) ⇒ Object
123 124 125 126 127 128 129 |
# File 'lib/reight/history.rb', line 123 def self.dump(xdos, &dump_object) xdos.map do |actions| actions.map do |action, *args| [action.to_s, *args.map {|obj| dump_object.call(obj) || obj}] end end end |
.load(hash, &restore_object) ⇒ Object
111 112 113 114 115 |
# File 'lib/reight/history.rb', line 111 def self.load(hash, &restore_object) undos = restore hash['undos'], &restore_object redos = restore hash['redos'], &restore_object self.new undos, redos end |
.restore(xdos, &restore_object) ⇒ Object
131 132 133 134 135 136 137 |
# File 'lib/reight/history.rb', line 131 def self.restore(xdos, &restore_object) xdos.map do |actions| actions.map do |action, *args| [action.intern, *args.map {|obj| restore_object.call(obj) || obj}] end end end |
Instance Method Details
#append(*actions) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/reight/history.rb', line 9 def append(*actions) return false if actions.empty? || disabled? if @group @group.push(*actions) else @undos.push actions @redos.clear update end true end |
#begin_grouping(*args, **kwargs, &block) ⇒ Object Also known as: group
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/reight/history.rb', line 21 def begin_grouping(*args, **kwargs, &block) raise "Grouping cannot be nested" if @group @group = [] if block begin block.call(*args, **kwargs) ensure end_grouping end end end |
#can_redo? ⇒ Boolean
81 82 83 |
# File 'lib/reight/history.rb', line 81 def can_redo?() !@redos.empty? end |
#can_undo? ⇒ Boolean
77 78 79 |
# File 'lib/reight/history.rb', line 77 def can_undo?() !@undos.empty? end |
#disable(*args, **kwargs, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/reight/history.rb', line 65 def disable(*args, **kwargs, &block) old = enabled? enable false if block begin block.call(*args, **kwargs) ensure enable old end end end |
#disabled ⇒ Object
100 101 |
# File 'lib/reight/history.rb', line 100 def disabled() end |
#disabled? ⇒ Boolean
93 94 95 |
# File 'lib/reight/history.rb', line 93 def disabled?() !enabled? end |
#enable(state = true) ⇒ Object
59 60 61 62 63 |
# File 'lib/reight/history.rb', line 59 def enable(state = true) return if state == @enabled @enabled = state @enabled ? enabled : disabled end |
#enabled ⇒ Object
97 98 |
# File 'lib/reight/history.rb', line 97 def enabled() end |
#enabled? ⇒ Boolean
89 90 91 |
# File 'lib/reight/history.rb', line 89 def enabled?() @enabled end |
#end_grouping ⇒ Object
35 36 37 38 39 |
# File 'lib/reight/history.rb', line 35 def end_grouping() raise "'begin_grouping' is missing" unless @group actions, @group = @group, nil append(*actions) end |
#redo(&block) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/reight/history.rb', line 50 def redo(&block) actions = @redos.pop || return disable do actions.each {|action| block.call action} end @undos.push actions update end |
#to_h(&dump_object) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/reight/history.rb', line 103 def to_h(&dump_object) { version: 1, undos: self.class.dump(@undos, &dump_object), redos: self.class.dump(@redos, &dump_object) } end |
#undo(&block) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/reight/history.rb', line 41 def undo(&block) actions = @undos.pop || return disable do actions.reverse.each {|action| block.call action} end @redos.push actions update end |
#updated(&block) ⇒ Object
85 86 87 |
# File 'lib/reight/history.rb', line 85 def updated(&block) @updated = block end |