Class: StrokeDB::Document::Versions

Inherits:
Object
  • Object
show all
Defined in:
lib/strokedb/document/versions.rb

Overview

Versions is a helper class that is used to navigate through versions. You should not instantiate it directly, but using Document#versions method

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document) ⇒ Versions

:nodoc:



8
9
10
11
# File 'lib/strokedb/document/versions.rb', line 8

def initialize(document)  #:nodoc:
  @document = document
  @cache = {}
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



7
8
9
# File 'lib/strokedb/document/versions.rb', line 7

def document
  @document
end

Instance Method Details

#[](version) ⇒ Object

Get document by version.

Returns Document instance Returns nil if there is no document with given version



20
21
22
# File 'lib/strokedb/document/versions.rb', line 20

def [](version)
  @cache[version] ||= @document.store.find(document.uuid, version)
end

#allObject

Get all versions of document including currrent one

Returns an Array of Documents



71
72
73
# File 'lib/strokedb/document/versions.rb', line 71

def all
  all_versions.map{|v| self[v]}
end

#all_precedingObject

Find all previous versions of document

Returns an Array of Documents



94
95
96
# File 'lib/strokedb/document/versions.rb', line 94

def all_preceding
  all_preceding_versions.map{|v| self[v]}
end

#all_preceding_versionsObject

Find all previous document versions, treating current one as a head

Returns an Array of version numbers



81
82
83
84
85
86
87
# File 'lib/strokedb/document/versions.rb', line 81

def all_preceding_versions
  if previous_version = document.previous_version
    [previous_version, *self[previous_version].versions.all_preceding_versions]
  else
    []
  end
end

#all_versionsObject

Find all document versions, treating current one as a head

Returns an Array of version numbers



62
63
64
# File 'lib/strokedb/document/versions.rb', line 62

def all_versions
  [document.version, *all_preceding_versions]
end

#currentObject

Get current version of document



27
28
29
# File 'lib/strokedb/document/versions.rb', line 27

def current
  document.new? ? document.clone.extend(VersionedDocument) : self[document.version]
end

#empty?Boolean

Returns true if document has no previous versions

Returns:

  • (Boolean)


101
102
103
# File 'lib/strokedb/document/versions.rb', line 101

def empty?
  document.previous_version.nil?
end

#firstObject

Get first version of document



41
42
43
# File 'lib/strokedb/document/versions.rb', line 41

def first
  document.new? ? document.clone.extend(VersionedDocument) : self[NIL_UUID]
end

#headObject

Get head version of document



34
35
36
# File 'lib/strokedb/document/versions.rb', line 34

def head
  document.new? ? document.clone.extend(VersionedDocument) : document.store.find(document.uuid)
end

#previousObject

Get document with previous version

Returns Document instance Returns nil if there is no previous version



53
54
55
# File 'lib/strokedb/document/versions.rb', line 53

def previous
  document.previous_version ? self[document.previous_version] : nil
end