Class: Couchbase::Datastructures::CouchbaseMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/couchbase/datastructures/couchbase_map.rb,
/code/couchbase-ruby-client/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

Constructor Details

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

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

Parameters:

  • id (String)

    the id of the document to back the map.

  • collection (Collection)

    the Couchbase collection through which to interact with the document.

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

    customization of the datastructure



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

def initialize(id, collection, options = Options::CouchbaseMap.new)
  @id = id
  @collection = collection
  @options = 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.

Parameters:

  • key (String)

Returns:

  • (Object, nil)

    value, associated with the key or nil



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.

Parameters:

  • key (String)
  • value (Object)


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

#clearObject

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

#delete(key) ⇒ Object

Deletes the key-value pair from the map.

Parameters:

  • key (String)

Returns:

  • void



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.

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_map.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 map is empty

Returns:

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

Overloads:

  • #fetch(key) ⇒ Object

    Gets the value, associated with the key, or raise KeyError if key could not be found

    Parameters:

    • key (String)

      key

  • #fetch(key, default) ⇒ Object

    Gets the value, associated with the key, or return default if key could not be found

    Parameters:

    • key (String)

      key

  • #fetch(key, &block) ⇒ Object

    Gets the value, associated with the key, or invoke specified block and propagate its return value if key could not be found

    Parameters:

    • key (String)

      key

    Yield Returns:

    • (Object)

      the default value to return in case the key could not be found

Returns:

  • (Object)


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

Parameters:

  • key (String)

Returns:

  • (Boolean)


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

#keysArray

Returns a new array populated with the keys from the map.

Returns:

  • (Array)


179
180
181
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 179

def keys
  map { |key, _value| key }
end

#lengthInteger Also known as: size

Returns the number of elements in the map.

Returns:

  • (Integer)

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

#valuesArray

Returns a new array populated with the values from the map.

Returns:

  • (Array)


186
187
188
# File 'lib/couchbase/datastructures/couchbase_map.rb', line 186

def values
  map { |_key, value| value }
end