Class: Couchbase::Datastructures::CouchbaseSet
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseSet
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_set.rb,
/code/couchbase-ruby-client/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
-
#add(obj) ⇒ CouchbaseSet
Adds the given value to the set.
-
#clear ⇒ Object
Removes all elements from the set.
-
#delete(obj) ⇒ Boolean
Deletes the given object from the set.
-
#each {|item| ... } ⇒ CouchbaseSet, Enumerable
Calls the given block once for each element in the set, passing that element as a parameter.
-
#empty? ⇒ Boolean
Returns true if set is empty.
-
#initialize(id, collection, options = Options::CouchbaseSet.new) ⇒ CouchbaseSet
constructor
Create a new List, backed by the document identified by
id
incollection
. -
#length ⇒ Integer
(also: #size)
Returns the number of elements in the set.
Constructor Details
#initialize(id, collection, options = Options::CouchbaseSet.new) ⇒ CouchbaseSet
Create a new List, backed by the document identified by id
in collection
.
35 36 37 38 39 40 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 35 def initialize(id, collection, = Options::CouchbaseSet.new) @id = id @collection = collection @options = @cas = 0 end |
Instance Method Details
#add(obj) ⇒ CouchbaseSet
Adds the given value to the set
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.) rescue Error::PathExists # ignore end self end |
#clear ⇒ Object
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.) nil rescue Error::DocumentNotFound nil end |
#delete(obj) ⇒ Boolean
Deletes the given object from the set.
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 = Collection::MutateInOptions.new .cas = result.cas @collection.mutate_in(@id, [ MutateInSpec.remove("[#{idx}]"), ], ) 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.
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.) 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
77 78 79 |
# File 'lib/couchbase/datastructures/couchbase_set.rb', line 77 def empty? size.zero? end |
#length ⇒ Integer Also known as: size
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.) result.content(0) rescue Error::DocumentNotFound 0 end |