Class: Couchbase::Datastructures::CouchbaseList

Inherits:
Object
  • Object
show all
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

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
# 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
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.

Parameters:

  • index (Integer)

Returns:

  • (Object, nil)


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.lookup_in_options)
  result.exists?(0) ? result.content(0) : nil
rescue Error::DocumentNotFound
  nil
end

#clearObject

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.remove_options)
  nil
rescue Error::DocumentNotFound
  nil
end

#delete_at(index) ⇒ CouchbaseList

Deletes the element at the specified index, returning that element, or nil

Parameters:

  • index (Integer)

Returns:



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.

Yield Parameters:

  • item (Object)

Returns:



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.get_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

Returns:

  • (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.

Parameters:

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

    object(s) to insert

Returns:



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

#lengthInteger Also known as: size

Returns the number of elements in the list.

Returns:

  • (Integer)

    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.lookup_in_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.

Parameters:

  • obj (Object...)

    object(s) to append

Returns:



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.mutate_in_options)
  self
end

#unshift(*obj) ⇒ 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:



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.mutate_in_options)
  self
end