Class: Couchbase::Datastructures::CouchbaseList
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseList
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_list.rb,
/code/couchbase-ruby-client/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 = Options::CouchbaseList.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 = Options::CouchbaseList.new) ⇒ CouchbaseList
Create a new List, backed by the document identified by id
in collection
.
35 36 37 38 39 40 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 35 def initialize(id, collection, = Options::CouchbaseList.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.
124 125 126 127 128 129 130 131 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 124 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
149 150 151 152 153 154 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 149 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
139 140 141 142 143 144 145 146 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 139 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 47 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(&) self else enum_for(:each) end end |
#empty? ⇒ Boolean
Returns true if list is empty
77 78 79 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 77 def empty? size.zero? end |
#insert(index, *obj) ⇒ CouchbaseList
Inserts the given values before the element with the given index
.
113 114 115 116 117 118 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 113 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.
65 66 67 68 69 70 71 72 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 65 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.
86 87 88 89 90 91 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 86 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
99 100 101 102 103 104 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 99 def unshift(*obj) @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", obj), ], @options.) self end |