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.



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

def initialize(options)
  @options = options
  @options[:entries] ||= 100
  @timeout = options.delete(:timeout) || 0

  @stack = [{:mutable => false, :created_at => 0, :type => :initial, :state => @options.delete(:state)}]
  @position = 0
end

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

#stackObject (readonly)

Returns the value of attribute stack.



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

def stack
  @stack
end

#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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruco/history.rb', line 19

def add(state)
  return unless tracked_field_changes?(state)
  remove_undone_states
  unless merge? state
    # can no longer modify previous states
    @stack[@position][:mutable] = false

    state_type = type(state)
    @position += 1
    @stack[@position] = {:mutable => true, :type => state_type, :created_at => Time.now.to_f}
  end
  @stack[@position][:state] = state
  limit_stack
end

#redoObject



38
39
40
# File 'lib/ruco/history.rb', line 38

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

#stateObject



15
16
17
# File 'lib/ruco/history.rb', line 15

def state
  @stack[@position][:state]
end

#undoObject



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

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