Class: CouchbaseStructures::Stack

Inherits:
Object
  • Object
show all
Includes:
CouchbaseDocStore
Defined in:
lib/couchbase_structures/stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Stack

Returns a new instance of Stack.



7
8
9
10
11
12
# File 'lib/couchbase_structures/stack.rb', line 7

def initialize(key)
  @key = key
  @top_index_key = "#{key}::stack::top"
  initialize_document(@top_index_key, 0)
  self
end

Instance Method Details

#popObject



20
21
22
23
24
25
26
27
# File 'lib/couchbase_structures/stack.rb', line 20

def pop()
  old_top_index = get_document(@top_index_key)
  decrease_atomic_count(@top_index_key)

  doc = get_document("#{key}::stack::#{old_top_index}")
  delete_document("#{key}::stack::#{old_top_index}")
  doc
end

#push(value) ⇒ Object



14
15
16
17
18
# File 'lib/couchbase_structures/stack.rb', line 14

def push(value)
  new_top_index = increase_atomic_count(@top_index_key)
  create_document("#{key}::stack::#{new_top_index}", value)
  self
end

#sizeObject



29
30
31
# File 'lib/couchbase_structures/stack.rb', line 29

def size
  get_document(@top_index_key)
end