Class: Couchbase::Datastructures::CouchbaseSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/couchbase/datastructures/couchbase_set.rb

Overview

A CouchbaseSet is implements Enumerable interface and backed by Collection document (more specifically a JSON array).

Note: sets are restricted to containing primitive types only due to server-side comparison restrictions.

Instance Method Summary collapse

Constructor Details

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

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

Parameters:

  • id (String)

    the id of the document to back the set.

  • collection (Collection)

    the collection through which to interact with the document.

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

    customization of the datastructure



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

def initialize(id, collection, options = Options::CouchbaseSet.new)
  @id = id
  @collection = collection
  @options = options
  @cas = 0
end

Instance Method Details

#add(obj) ⇒ CouchbaseSet

Adds the given value to the set

Parameters:

  • obj (Object)

Returns:



85
86
87
88
89
90
91
92
93
94
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 85

def add(obj)
  begin
    @collection.mutate_in(@id, [
                            MutateInSpec.array_add_unique("", obj),
                          ], @options.mutate_in_options)
  rescue Error::PathExists
    # ignore
  end
  self
end

#clearObject

Removes all elements from the set



97
98
99
100
101
102
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 97

def clear
  @collection.remove(@id, @options.remove_options)
  nil
rescue Error::DocumentNotFound
  nil
end

#delete(obj) ⇒ Boolean

Deletes the given object from the set.

Returns:

  • (Boolean)

    true if the value has been removed



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 107

def delete(obj)
  result = @collection.get(@id)
  idx = result.content.index(obj)
  return false unless idx

  options = Collection::MutateInOptions.new
  options.cas = result.cas
  @collection.mutate_in(@id, [
                          MutateInSpec.remove("[#{idx}]"),
                        ], options)
  true
rescue Error::CasMismatch
  retry
rescue Error::DocumentNotFound
  false
end

#each {|item| ... } ⇒ CouchbaseSet, Enumerable

Calls the given block once for each element in the set, 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_set.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 set is empty

Returns:

  • (Boolean)

    returns true if set is empty



77
78
79
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 77

def empty?
  size.zero?
end

#lengthInteger Also known as: size

Returns the number of elements in the set.

Returns:

  • (Integer)

    returns the number of elements in the set.



65
66
67
68
69
70
71
72
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 65

def length
  result = @collection.lookup_in(@id, [
                                   LookupInSpec.count(""),
                                 ], @options.lookup_in_options)
  result.content(0)
rescue Error::DocumentNotFound
  0
end