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
value
with the key given bykey
. -
#clear ⇒ Object
Removes all elements from the map.
-
#delete(key) ⇒ Object
Deletes the key-value pair from the map.
-
#each {|item| ... } ⇒ CouchbaseMap, Enumerable
Calls the given block once for each element in the map, passing that element as a parameter.
-
#empty? ⇒ Boolean
Returns true if map is empty.
-
#fetch(key, *rest) ⇒ 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
id
incollection
. -
#key?(key) ⇒ Boolean
(also: #member?, #include?)
Returns
true
if the given key is present. -
#keys ⇒ Array
Returns a new array populated with the keys from the map.
-
#length ⇒ Integer
(also: #size)
Returns the number of elements in the map.
-
#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 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 35 def initialize(id, collection, = Options::CouchbaseMap.new) @id = id @collection = collection @options = @cas = 0 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.
131 132 133 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 131 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
.
141 142 143 144 145 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 141 def []=(key, value) @collection.mutate_in(@id, [ MutateInSpec.upsert(key, value), ], @options.) end |
#clear ⇒ Object
Removes all elements from the map
82 83 84 85 86 87 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 82 def clear @collection.remove(@id, @options.) nil rescue Error::DocumentNotFound nil end |
#delete(key) ⇒ Object
Deletes the key-value pair from the map.
152 153 154 155 156 157 158 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 152 def delete(key) @collection.mutate_in(@id, [ MutateInSpec.remove(key), ]) rescue Error::DocumentNotFound, Error::PathNotFound nil end |
#each {|item| ... } ⇒ CouchbaseMap, Enumerable
Calls the given block once for each element in the map, 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_map.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 map is empty
77 78 79 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 77 def empty? size.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
default
is given, then that will be returned -
if the optional code
block
is specified, then that will be run and its result returned
112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 112 def fetch(key, *rest) result = @collection.lookup_in(@id, [ LookupInSpec.get(key), ], @options.) 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 |
#key?(key) ⇒ Boolean Also known as: member?, include?
Returns true
if the given key is present
164 165 166 167 168 169 170 171 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 164 def key?(key) result = @collection.lookup_in(@id, [ LookupInSpec.exists(key), ], @options.) result.exists?(0) rescue Error::DocumentNotFound, Error::PathNotFound false end |
#keys ⇒ Array
Returns a new array populated with the keys from the map.
179 180 181 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 179 def keys map { |key, _value| key } end |
#length ⇒ Integer Also known as: size
Returns the number of elements in the map.
65 66 67 68 69 70 71 72 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 65 def length result = @collection.lookup_in(@id, [ LookupInSpec.count(""), ], @options.) result.content(0) rescue Error::DocumentNotFound 0 end |
#values ⇒ Array
Returns a new array populated with the values from the map.
186 187 188 |
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 186 def values map { |_key, value| value } end |