Class: Ruco::History

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ History

Returns a new instance of History.



5
6
7
8
9
10
11
# File 'lib/ruco/history.rb', line 5

def initialize(options)
  @options = options
  @stack = [@options.delete(:state)]
  @timeout = options.delete(:timeout) || 0
  timeout!
  @position = 0
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



3
4
5
# File 'lib/ruco/history.rb', line 3

def timeout
  @timeout
end

Instance Method Details

#add(state) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ruco/history.rb', line 17

def add(state)
  return unless tracked_field_changes?(state)
  remove_undone_states
  if merge_timeout?
    @position += 1
    @last_merge = Time.now.to_f
  end
  @stack[@position] = state
  limit_stack
end

#redoObject



33
34
35
36
# File 'lib/ruco/history.rb', line 33

def redo
  timeout!
  @position = [@position + 1, @stack.size - 1].min
end

#stateObject



13
14
15
# File 'lib/ruco/history.rb', line 13

def state
  @stack[@position]
end

#undoObject



28
29
30
31
# File 'lib/ruco/history.rb', line 28

def undo
  timeout!
  @position = [@position - 1, 0].max
end