Class: History

Inherits:
Object
  • Object
show all
Includes:
CanDisable
Defined in:
lib/rubysketch/solitaire/common/history.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CanDisable

#disable, #disabled, #disabled?, #enable, #enabled, #enabled?

Constructor Details

#initialize(undos = [], redos = []) ⇒ History

Returns a new instance of History.



5
6
7
8
9
# File 'lib/rubysketch/solitaire/common/history.rb', line 5

def initialize(undos = [], redos = [])
  super()
  @undos, @redos = undos, redos
  @group         = nil
end

Class Method Details

.load(hash, &restoreObject) ⇒ Object



66
67
68
69
70
# File 'lib/rubysketch/solitaire/common/history.rb', line 66

def self.load(hash, &restoreObject)
  undos = restore hash['undos'], &restoreObject
  redos = restore hash['redos'], &restoreObject
  self.new undos, redos
end

Instance Method Details

#canRedo?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubysketch/solitaire/common/history.rb', line 50

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

#canUndo?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rubysketch/solitaire/common/history.rb', line 46

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

#group(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/rubysketch/solitaire/common/history.rb', line 22

def group(&block)
  @group = group = [] unless @group
  block.call
ensure
  if group
    @group = nil
    push *group
  end
end

#push(*actions) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/rubysketch/solitaire/common/history.rb', line 11

def push(*actions)
  return if actions.empty? || disabled?
  if @group
    @group.push *actions
  else
    @undos.push actions
    @redos.clear
    update
  end
end

#redo(&block) ⇒ Object



39
40
41
42
43
44
# File 'lib/rubysketch/solitaire/common/history.rb', line 39

def redo(&block)
  actions = @redos.pop || return
  actions.each {|action| block.call action}
  @undos.push actions
  update
end

#to_h(&dumpObject) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/rubysketch/solitaire/common/history.rb', line 58

def to_h(&dumpObject)
  {
    version: 1,
    undos: self.class.dump(@undos, &dumpObject),
    redos: self.class.dump(@redos, &dumpObject)
  }
end

#undo(&block) ⇒ Object



32
33
34
35
36
37
# File 'lib/rubysketch/solitaire/common/history.rb', line 32

def undo(&block)
  actions = @undos.pop || return
  actions.reverse.each {|action| block.call action}
  @redos.push actions
  update
end

#updated(&block) ⇒ Object



54
55
56
# File 'lib/rubysketch/solitaire/common/history.rb', line 54

def updated(&block)
  @updated = block
end