Class: GrandCentral::VersionedStore

Inherits:
Store
  • Object
show all
Defined in:
lib/grand_central/versioned_store.rb

Instance Attribute Summary collapse

Attributes inherited from Store

#state

Instance Method Summary collapse

Methods inherited from Store

#on_dispatch, #run_callbacks

Constructor Details

#initialize(*args) ⇒ VersionedStore

Returns a new instance of VersionedStore.



7
8
9
10
11
12
13
# File 'lib/grand_central/versioned_store.rb', line 7

def initialize *args
  super

  @current_version = 0
  @rollback_versions = []
  @redo_versions = []
end

Instance Attribute Details

#current_versionObject (readonly)

Returns the value of attribute current_version.



5
6
7
# File 'lib/grand_central/versioned_store.rb', line 5

def current_version
  @current_version
end

Instance Method Details

#commit!Object



60
61
62
# File 'lib/grand_central/versioned_store.rb', line 60

def commit!
  initialize state
end

#dispatch(*args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/grand_central/versioned_store.rb', line 15

def dispatch *args
  old_state = state

  result = super

  @rollback_versions << old_state
  @redo_versions.clear
  @current_version += 1

  result
end

#go_to_version(version) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/grand_central/versioned_store.rb', line 51

def go_to_version version
  while version > current_version
    self.redo
  end
  while version < current_version
    rollback
  end
end

#redoObject



37
38
39
40
41
42
43
44
45
# File 'lib/grand_central/versioned_store.rb', line 37

def redo
  unless @redo_versions.empty?
    @rollback_versions << (old_state = state)
    @state = @redo_versions.pop
    @current_version += 1
  end

  run_callbacks old_state, state
end

#rollbackObject



27
28
29
30
31
32
33
34
35
# File 'lib/grand_central/versioned_store.rb', line 27

def rollback
  unless @rollback_versions.empty?
    @redo_versions << (old_state = state)
    @state = @rollback_versions.pop
    @current_version -= 1
  end

  run_callbacks old_state, state
end

#total_versionsObject



47
48
49
# File 'lib/grand_central/versioned_store.rb', line 47

def total_versions
  @rollback_versions.size + @redo_versions.size + 1
end