Class: Couchbase::Datastructures::CouchbaseList
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseList
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_list.rb
Overview
A CouchbaseList is implements Enumerable
interface and backed by Collection document (more specifically a JSON array).
Note that as such, a CouchbaseList is restricted to the types that JSON array can contain.
Instance Method Summary collapse
-
#at(index) ⇒ Object?
(also: #[])
Returns the element at
index
. -
#clear ⇒ Object
Removes all elements from the list.
-
#delete_at(index) ⇒ CouchbaseList
Deletes the element at the specified
index
, returning that element, or nil. -
#each {|item| ... } ⇒ CouchbaseList, Enumerable
Calls the given block once for each element in the list, passing that element as a parameter.
-
#empty? ⇒ Boolean
Returns true if list is empty.
-
#initialize(id, collection, options = CouchbaseListOptions.new) ⇒ CouchbaseList
constructor
Create a new List, backed by the document identified by
id
incollection
. -
#insert(index, *obj) ⇒ CouchbaseList
Inserts the given values before the element with the given
index
. -
#length ⇒ Integer
(also: #size)
Returns the number of elements in the list.
-
#push(*obj) ⇒ CouchbaseList
(also: #append)
Appends the given object(s) on to the end of this error.
-
#unshift(*obj) ⇒ CouchbaseList
(also: #prepend)
Prepends objects to the front of the list, moving other elements upwards.
Constructor Details
#initialize(id, collection, options = CouchbaseListOptions.new) ⇒ CouchbaseList
Create a new List, backed by the document identified by id
in collection
.
32 33 34 35 36 37 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 32 def initialize(id, collection, = CouchbaseListOptions.new) @id = id @collection = collection @options = @cas = 0 end |
Instance Method Details
#at(index) ⇒ Object? Also known as: []
Returns the element at index
. A negative index counts from the end. Returns nil
if the index is out of range.
123 124 125 126 127 128 129 130 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 123 def at(index) result = @collection.lookup_in(@id, [ LookupInSpec.get("[#{index.to_i}]") ], @options.) result.exists?(0) ? result.content(0) : nil rescue Error::DocumentNotFound nil end |
#clear ⇒ Object
Removes all elements from the list
148 149 150 151 152 153 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 148 def clear @collection.remove(@id, @options.) nil rescue Error::DocumentNotFound nil end |
#delete_at(index) ⇒ CouchbaseList
Deletes the element at the specified index
, returning that element, or nil
138 139 140 141 142 143 144 145 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 138 def delete_at(index) @collection.mutate_in(@id, [ MutateInSpec.remove("[#{index.to_i}]") ]) self rescue Error::DocumentNotFound self end |
#each {|item| ... } ⇒ CouchbaseList, Enumerable
Calls the given block once for each element in the list, passing that element as a parameter.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 44 def each if block_given? begin result = @collection.get(@id, @options.) current = result.content @cas = result.cas rescue Error::DocumentNotFound current = [] @cas = 0 end current.each do |entry| yield entry end self else enum_for(:each) end end |
#empty? ⇒ Boolean
Returns true if list is empty
76 77 78 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 76 def empty? size.zero? end |
#insert(index, *obj) ⇒ CouchbaseList
Inserts the given values before the element with the given index
.
112 113 114 115 116 117 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 112 def insert(index, *obj) @collection.mutate_in(@id, [ MutateInSpec.array_insert("[#{index.to_i}]", obj) ]) self end |
#length ⇒ Integer Also known as: size
Returns the number of elements in the list.
64 65 66 67 68 69 70 71 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 64 def length result = @collection.lookup_in(@id, [ LookupInSpec.count("") ], @options.) result.content(0) rescue Error::DocumentNotFound 0 end |
#push(*obj) ⇒ CouchbaseList Also known as: append
Appends the given object(s) on to the end of this error. This expression returns the array itself, so several appends may be chained together.
85 86 87 88 89 90 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 85 def push(*obj) @collection.mutate_in(@id, [ MutateInSpec.array_append("", obj) ], @options.) self end |
#unshift(*obj) ⇒ CouchbaseList Also known as: prepend
Prepends objects to the front of the list, moving other elements upwards
98 99 100 101 102 103 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 98 def unshift(*obj) @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", obj) ], @options.) self end |