Class: StrokeDB::Document::Versions
- 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
-
#document ⇒ Object
readonly
Returns the value of attribute document.
Instance Method Summary collapse
-
#[](version) ⇒ Object
Get document by version.
-
#all ⇒ Object
Get all versions of document including currrent one.
-
#all_preceding ⇒ Object
Find all previous versions of document.
-
#all_preceding_versions ⇒ Object
Find all previous document versions, treating current one as a head.
-
#all_versions ⇒ Object
Find all document versions, treating current one as a head.
-
#current ⇒ Object
Get current version of document.
-
#empty? ⇒ Boolean
Returns
true
if document has no previous versions. -
#first ⇒ Object
Get first version of document.
-
#head ⇒ Object
Get head version of document.
-
#initialize(document) ⇒ Versions
constructor
:nodoc:.
-
#previous ⇒ Object
Get document with previous version.
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
#document ⇒ Object (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 |
#all ⇒ Object
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_preceding ⇒ Object
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_versions ⇒ Object
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_versions ⇒ Object
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 |
#current ⇒ Object
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
101 102 103 |
# File 'lib/strokedb/document/versions.rb', line 101 def empty? document.previous_version.nil? end |
#first ⇒ Object
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 |
#head ⇒ Object
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 |
#previous ⇒ Object
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 |