Class: Couchbase::Datastructures::CouchbaseMap
- Inherits:
-
Object
- Object
- Couchbase::Datastructures::CouchbaseMap
- Includes:
- Enumerable
- Defined in:
- lib/couchbase/datastructures/couchbase_map.rb
Overview
A CouchbaseMap is implements Enumerable interface and backed by Collection document (more specifically
a JSON array).
Note that as such, a CouchbaseMap is restricted to the types that JSON array can contain.
Instance Method Summary collapse
-
#[](key) ⇒ Object?
Returns a value from the map for the given key.
-
#[]=(key, value) ⇒ void
Associate the value given by
valuewith the key given bykey. -
#clear(parent_span: nil) ⇒ Object
Removes all elements from the map.
-
#delete(key, parent_span: nil) ⇒ Object
Deletes the key-value pair from the map.
-
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseMap, Enumerable
Calls the given block once for each element in the map, passing that element as a parameter.
-
#empty?(parent_span: nil) ⇒ Boolean
Returns true if map is empty.
-
#fetch(key, *rest, parent_span: nil) ⇒ Object
Returns a value from the map for the given key.
-
#initialize(id, collection, options = Options::CouchbaseMap.new) ⇒ CouchbaseMap
constructor
Create a new Map, backed by the document identified by
idincollection. -
#key?(key, parent_span: nil) ⇒ Boolean
(also: #member?, #include?)
Returns
trueif the given key is present. -
#keys ⇒ Array
Returns a new array populated with the keys from the map.
-
#length(parent_span: nil) ⇒ Integer
(also: #size)
Returns the number of elements in the map.
-
#store(key, value, parent_span: nil) ⇒ void
Associate the value given by
valuewith the key given bykey. -
#values ⇒ Array
Returns a new array populated with the values from the map.
Constructor Details
#initialize(id, collection, options = Options::CouchbaseMap.new) ⇒ CouchbaseMap
Create a new Map, backed by the document identified by id in collection.
35 36 37 38 39 40 41 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 35 def initialize(id, collection, = Options::CouchbaseMap.new) @id = id @collection = collection @options = @cas = 0 @observability = collection.instance_variable_get(:@observability) end |
Instance Method Details
#[](key) ⇒ Object?
Returns a value from the map for the given key.
If the key cannot be found, nil will be returned.
147 148 149 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 147 def [](key) fetch(key, nil) end |
#[]=(key, value) ⇒ void
This method returns an undefined value.
Associate the value given by value with the key given by key.
173 174 175 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 173 def []=(key, value) store(key, value) end |
#clear(parent_span: nil) ⇒ Object
Removes all elements from the map
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 90 def clear(parent_span: nil) @observability.record_operation(Observability::OP_MAP_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(key, parent_span: nil) ⇒ Object
Deletes the key-value pair from the map.
182 183 184 185 186 187 188 189 190 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 182 def delete(key, parent_span: nil) @observability.record_operation(Observability::OP_MAP_DELETE, parent_span, self) do |obs_handler| @collection.mutate_in(@id, [ MutateInSpec.remove(key), ], Options::MutateIn.new(parent_span: obs_handler.op_span)) rescue Error::DocumentNotFound, Error::PathNotFound nil end end |
#each(parent_span: nil) {|item| ... } ⇒ CouchbaseMap, Enumerable
Calls the given block once for each element in the map, passing that element as a parameter.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 48 def each(parent_span: nil, &) if block_given? current = @observability.record_operation(Observability::OP_MAP_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 map is empty
85 86 87 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 85 def empty?(parent_span: nil) size(parent_span: parent_span).zero? end |
#fetch(key) ⇒ Object #fetch(key, default) ⇒ Object #fetch(key, &block) ⇒ Object
Returns a value from the map for the given key.
If the key cannot be found, there are several options:
- with no other arguments, it will raise a KeyError exception
- if
defaultis given, then that will be returned - if the optional code
blockis specified, then that will be run and its result returned
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 124 def fetch(key, *rest, parent_span: nil) @observability.record_operation(Observability::OP_MAP_FETCH, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.get(key), ], ) result.content(0) rescue Error::DocumentNotFound, Error::PathNotFound return yield if block_given? return rest.first unless rest.empty? raise KeyError, "key not found: #{key}" end end |
#key?(key, parent_span: nil) ⇒ Boolean Also known as: member?, include?
Returns true if the given key is present
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 196 def key?(key, parent_span: nil) @observability.record_operation(Observability::OP_MAP_KEY_EXISTS, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span result = @collection.lookup_in(@id, [ LookupInSpec.exists(key), ], ) result.exists?(0) rescue Error::DocumentNotFound, Error::PathNotFound false end end |
#keys ⇒ Array
Returns a new array populated with the keys from the map.
215 216 217 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 215 def keys map { |key, _value| key } end |
#length(parent_span: nil) ⇒ Integer Also known as: size
Returns the number of elements in the map.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 69 def length(parent_span: nil) @observability.record_operation(Observability::OP_MAP_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 |
#store(key, value, parent_span: nil) ⇒ void
This method returns an undefined value.
Associate the value given by value with the key given by key.
157 158 159 160 161 162 163 164 165 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 157 def store(key, value, parent_span: nil) @observability.record_operation(Observability::OP_MAP_STORE, parent_span, self) do |obs_handler| = @options..clone .parent_span = obs_handler.op_span @collection.mutate_in(@id, [ MutateInSpec.upsert(key, value), ], ) end end |
#values ⇒ Array
Returns a new array populated with the values from the map.
222 223 224 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 222 def values map { |_key, value| value } end |