class Moneta::WeakEachKey

Adds weak key enumeration support to the underlying store

@note This class wraps methods that store and retrieve entries in order to

track which keys are in the store, and uses this list when doing key
traversal.  This means that {#each_key each_key} will only yield keys
which have been accessed previously via the present store object.  This
wrapper is therefore best suited to adapters which are not persistent, and
which cannot be shared.

@api public

Attributes

all_keys[R]

Public Class Methods

new(adapter, options = {}) click to toggle source

@param [Moneta store] adapter The underlying store @param [Hash] options

Calls superclass method
# File lib/moneta/weak_each_key.rb, line 19
def initialize(adapter, options = {})
  raise 'Store already supports feature :each_key' if adapter.supports?(:each_key)
  @all_keys = Set.new
  super
end

Public Instance Methods

each_key() { |key| ... } click to toggle source

(see Proxy#each_key)

# File lib/moneta/weak_each_key.rb, line 26
def each_key
  return enum_for(:each_key) { all_keys.size } unless block_given?
  all_keys.each { |key| yield key }
  self
end

Protected Instance Methods

each_key_save(key) click to toggle source
# File lib/moneta/weak_each_key.rb, line 68
def each_key_save(key)
  @all_keys = Set.new(@all_keys).add(key)
end
wrap(name, *args) { || ... } click to toggle source
# File lib/moneta/weak_each_key.rb, line 36
def wrap(name, *args)
  case name
  when :create, :store, :increment, :create
    each_key_save(args[0])
    yield
  when :key?
    if found = yield
      each_key_save(args[0])
    else
      all_keys.delete(args[0])
    end
    found
  when :load
    key?(*args)
    yield
  when :delete
    all_keys.delete(args[0])
    yield
  when :clear, :close
    all_keys.clear
    yield
  when :values_at, :fetch_values, :slice
    args[0].each { |key| key?(key) }
    yield
  when :merge!
    args[0].each { |key, _| each_key_save(key) }
    yield
  else
    yield
  end
end