Class: Couchbase::Datastructures::CouchbaseList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/couchbase/datastructures/couchbase_list.rb,
/home/runner/work/couchbase-ruby-client/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

Constructor Details

#initialize(id, collection, options = Options::CouchbaseList.new) ⇒ CouchbaseList

Create a new List, backed by the document identified by id in collection.

Parameters:

  • id (String)

    the id of the document to back the list.

  • collection (Collection)

    the Couchbase collection through which to interact with the document.

  • options (Options::CouchbaseList) (defaults to: Options::CouchbaseList.new)

    customization of the datastructure



35
36
37
38
39
40
41
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 35

def initialize(id, collection, options = Options::CouchbaseList.new)
  @id = id
  @collection = collection
  @options = options
  @cas = 0
  @observability = @collection.instance_variable_get(:@observability)
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.

Parameters:

  • index (Integer)

Returns:

  • (Object, nil)


144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 144

def at(index, parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_AT, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.parent_span = obs_handler.op_span
    result = @collection.lookup_in(@id, [
                                     LookupInSpec.get("[#{index.to_i}]"),
                                   ], options)
    result.exists?(0) ? result.content(0) : nil
  rescue Error::DocumentNotFound
    nil
  end
end

#clear(parent_span: nil) ⇒ Object

Removes all elements from the list



176
177
178
179
180
181
182
183
184
185
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 176

def clear(parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_CLEAR, parent_span, self) do |obs_handler|
    options = @options.remove_options.clone
    options.parent_span = obs_handler.op_span
    @collection.remove(@id, options)
    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

Parameters:

  • index (Integer)

Returns:



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 163

def delete_at(index, parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_DELETE_AT, parent_span, self) do |obs_handler|
    options = Options::MutateIn.new(parent_span: obs_handler.op_span)
    @collection.mutate_in(@id, [
                            MutateInSpec.remove("[#{index.to_i}]"),
                          ], options)
    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.

Yield Parameters:

  • item (Object)

Returns:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 48

def each(parent_span: nil, &)
  if block_given?
    current =
      @observability.record_operation(Observability::OP_LIST_EACH, parent_span, self) do |obs_handler|
        options = @options.get_options.clone
        options.parent_span = obs_handler.op_span
        result = @collection.get(@id, options)
        @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

Returns:

  • (Boolean)

    returns true if list is empty



85
86
87
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 85

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.

Parameters:

  • index (Integer)
  • obj (Object...)

    object(s) to insert

Returns:



129
130
131
132
133
134
135
136
137
138
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 129

def insert(index, *obj, parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_INSERT, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.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.

Returns:

  • (Integer)

    returns the number of elements in the list.



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 69

def length(parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_LENGTH, parent_span, self) do |obs_handler|
    options = @options.lookup_in_options.clone
    options.parent_span = obs_handler.op_span
    result = @collection.lookup_in(@id, [
                                     LookupInSpec.count(""),
                                   ], options)
    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.

Parameters:

  • obj (Object...)

    object(s) to append

Returns:



94
95
96
97
98
99
100
101
102
103
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 94

def push(*obj, parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_PUSH, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.parent_span = obs_handler.op_span
    @collection.mutate_in(@id, [
                            MutateInSpec.array_append("", obj),
                          ], options)
  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

Parameters:

  • obj (Object...)

    object(s) to prepend

Returns:



111
112
113
114
115
116
117
118
119
120
# File 'lib/couchbase/datastructures/couchbase_list.rb', line 111

def unshift(*obj, parent_span: nil)
  @observability.record_operation(Observability::OP_LIST_UNSHIFT, parent_span, self) do |obs_handler|
    options = @options.mutate_in_options.clone
    options.parent_span = obs_handler.op_span
    @collection.mutate_in(@id, [
                            MutateInSpec.array_prepend("", obj),
                          ], options)
  end
  self
end