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, parent_span: nil) ⇒ Object?
(also: #[])
Returns the element at
index. -
#clear(parent_span: nil) ⇒ Object
Removes all elements from the list.
-
#delete_at(index, parent_span: nil) ⇒ CouchbaseList
Deletes the element at the specified
index, returning that element, or nil. -
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseList, Enumerable
Calls the given block once for each element in the list, passing that element as a parameter.
-
#empty?(parent_span: nil) ⇒ 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
idincollection. -
#insert(index, *obj, parent_span: nil) ⇒ CouchbaseList
Inserts the given values before the element with the given
index. -
#length(parent_span: nil) ⇒ Integer
(also: #size)
Returns the number of elements in the list.
-
#push(*obj, parent_span: nil) ⇒ CouchbaseList
(also: #append)
Appends the given object(s) on to the end of this error.
-
#unshift(*obj, parent_span: nil) ⇒ 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 41 42 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 35 def initialize(id, collection, = Options::CouchbaseList.new) @id = id @collection = collection @options = @cas = 0 @observability = @collection.instance_variable_get(:@observability) @observability = Observability::Wrapper.new if @observability.nil? end |
Instance Method Details
#at(index, parent_span: nil) ⇒ 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.
145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 145 def at(index, parent_span: nil) @observability.record_operation(Observability::OP_LIST_AT, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.get("[#{index.to_i}]"), ], ) result.exists?(0) ? result.content(0) : nil rescue Error::DocumentNotFound nil end end |
#clear(parent_span: nil) ⇒ Object
Removes all elements from the list
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 177 def clear(parent_span: nil) @observability.record_operation(Observability::OP_LIST_CLEAR, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.remove(@id, ) nil rescue Error::DocumentNotFound nil end end |
#delete_at(index, parent_span: nil) ⇒ CouchbaseList
Deletes the element at the specified index, returning that element, or nil
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 164 def delete_at(index, parent_span: nil) @observability.record_operation(Observability::OP_LIST_DELETE_AT, parent_span, self) do |obs_handler| = Options::MutateIn.new(parent_span: obs_handler.op_span) @collection.mutate_in(@id, [ MutateInSpec.remove("[#{index.to_i}]"), ], ) self rescue Error::DocumentNotFound self end end |
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseList, Enumerable
Calls the given block once for each element in the list, passing that element as a parameter.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 49 def each(parent_span: nil, &) if block_given? current = @observability.record_operation(Observability::OP_LIST_EACH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.get(@id, ) @cas = result.cas result.content rescue Error::DocumentNotFound @cas = 0 [] end current.each(&) self else enum_for(:each, parent_span: parent_span) end end |
#empty?(parent_span: nil) ⇒ Boolean
Returns true if list is empty
86 87 88 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 86 def empty?(parent_span: nil) size(parent_span: parent_span).zero? end |
#insert(index, *obj, parent_span: nil) ⇒ CouchbaseList
Inserts the given values before the element with the given index.
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 130 def insert(index, *obj, parent_span: nil) @observability.record_operation(Observability::OP_LIST_INSERT, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.array_insert("[#{index.to_i}]", obj), ]) end self end |
#length(parent_span: nil) ⇒ Integer Also known as: size
Returns the number of elements in the list.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 70 def length(parent_span: nil) @observability.record_operation(Observability::OP_LIST_LENGTH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.count(""), ], ) result.content(0) rescue Error::DocumentNotFound 0 end end |
#push(*obj, parent_span: nil) ⇒ 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.
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 95 def push(*obj, parent_span: nil) @observability.record_operation(Observability::OP_LIST_PUSH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.array_append("", obj), ], ) end self end |
#unshift(*obj, parent_span: nil) ⇒ CouchbaseList Also known as: prepend
Prepends objects to the front of the list, moving other elements upwards
112 113 114 115 116 117 118 119 120 121 |
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 112 def unshift(*obj, parent_span: nil) @observability.record_operation(Observability::OP_LIST_UNSHIFT, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.array_prepend("", obj), ], ) end self end |