Module: Moneta::DBMAdapter Private

Includes:
HashAdapter
Included in:
Adapters::DBM, Adapters::Daybreak, Adapters::GDBM, Adapters::SDBM
Defined in:
lib/moneta/dbm_adapter.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

This is for adapters that conform to the DBM interface

Instance Attribute Summary

Attributes included from HashAdapter

#backend

Instance Method Summary collapse

Methods included from HashAdapter

#clear, #delete, #fetch_values, #key?, #load, #slice, #store, #values_at

Instance Method Details

#closeObject

Explicitly close the store

Returns:

  • nil



8
9
10
11
# File 'lib/moneta/dbm_adapter.rb', line 8

def close
  @backend.close
  nil
end

#merge!(pairs, options = {}) ⇒ self #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

Note:

Some adapters may implement this method atomically, or in two passes when a block is provided. The default implmentation uses HashAdapter#key?, HashAdapter#load and HashAdapter#store.

Stores the pairs in the key-value store, and returns itself. When a block is provided, it will be called before overwriting any existing values with the key, old value and supplied value, and the return value of the block will be used in place of the supplied value.

Overloads:

  • #merge!(pairs, options = {}) ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Returns:

    • (self)
  • #merge!(pairs, options = {}) {|key, old_value, new_value| ... } ⇒ self

    Parameters:

    • pairs (<(Object, Object)>)

      A collection of key-value pairs to store

    • options (Hash) (defaults to: {})

    Yield Parameters:

    • key (Object)

      A key that whose value is being overwritten

    • old_value (Object)

      The existing value which is being overwritten

    • new_value (Object)

      The value supplied in the method call

    Yield Returns:

    • (Object)

      The value to use for overwriting

    Returns:

    • (self)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/moneta/dbm_adapter.rb', line 14

def merge!(pairs, options = {})
  hash =
    if block_given?
      keys = pairs.map { |k, _| k }
      old_pairs = Hash[slice(*keys)]
      Hash[pairs.map do |key, new_value|
        new_value = yield(key, old_pairs[key], new_value) if old_pairs.key?(key)
        [key, new_value]
      end.to_a]
    else
      Hash === pairs ? pairs : Hash[pairs.to_a]
    end

  @backend.update(hash)
  self
end