Module: Moneta::DBMAdapter Private

Includes:
HashAdapter
Included in:
Adapters::DBM, Adapters::Daybreak, Adapters::GDBM, Adapters::SDBM
Defined in:
lib/moneta/mixins.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



539
540
541
542
# File 'lib/moneta/mixins.rb', line 539

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)


545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/moneta/mixins.rb', line 545

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