Class: Reight::History

Inherits:
Object
  • Object
show all
Defined in:
lib/reight/history.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.load(hash, &restore_object) ⇒ Object



107
108
109
110
111
# File 'lib/reight/history.rb', line 107

def self.load(hash, &restore_object)
  undos = restore hash['undos'], &restore_object
  redos = restore hash['redos'], &restore_object
  self.new undos, redos
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(&block) ⇒ Object Also known as: group



21
22
23
24
25
26
27
# File 'lib/reight/history.rb', line 21

def begin_grouping(&block)
  raise "Grouping cannot be nested" if @group
  @group = []
  block.call if block
ensure
  end_grouping if block
end

#can_redo?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/reight/history.rb', line 77

def can_redo?()
  !@redos.empty?
end

#can_undo?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/reight/history.rb', line 73

def can_undo?()
  !@undos.empty?
end

#disable(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/reight/history.rb', line 61

def disable(&block)
  old = enabled?
  enable false
  if block
    begin
      block.call
    ensure
      enable old
    end
  end
end

#disabledObject



96
97
# File 'lib/reight/history.rb', line 96

def disabled()
end

#disabled?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/reight/history.rb', line 89

def disabled?()
  !enabled?
end

#enable(state = true) ⇒ Object



55
56
57
58
59
# File 'lib/reight/history.rb', line 55

def enable(state = true)
  return if state == @enabled
  @enabled = state
  @enabled ? enabled : disabled
end

#enabledObject



93
94
# File 'lib/reight/history.rb', line 93

def enabled()
end

#enabled?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/reight/history.rb', line 85

def enabled?()
  @enabled
end

#end_groupingObject



31
32
33
34
35
# File 'lib/reight/history.rb', line 31

def end_grouping()
  raise "'begin_grouping' is missing" unless @group
  actions, @group = @group, nil
  append(*actions)
end

#redo(&block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/reight/history.rb', line 46

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



99
100
101
102
103
104
105
# File 'lib/reight/history.rb', line 99

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



37
38
39
40
41
42
43
44
# File 'lib/reight/history.rb', line 37

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



81
82
83
# File 'lib/reight/history.rb', line 81

def updated(&block)
  @updated = block
end