Class: Moneta::WeakEachKey

Inherits:
Wrapper show all
Defined in:
lib/moneta/weak_each_key.rb

Overview

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

Adds weak key enumeration support to the underlying store

Instance Attribute Summary

Attributes inherited from Proxy

#adapter

Instance Method Summary collapse

Methods inherited from Wrapper

#clear, #close, #create, #delete, #features, #fetch_values, #increment, #key?, #load, #merge!, #slice, #store, #values_at

Methods inherited from Proxy

#clear, #close, #create, #delete, #features, features_mask, #fetch_values, #increment, #key?, #load, #merge!, not_supports, #slice, #store, #values_at

Methods included from Defaults

#[], #[]=, #close, #create, #decrement, #features, #fetch, #fetch_values, included, #increment, #key?, #merge!, #slice, #supports?, #update, #values_at

Methods included from OptionSupport

#expires, #prefix, #raw, #with

Constructor Details

#initialize(adapter, options = {}) ⇒ WeakEachKey

Returns a new instance of WeakEachKey.

Parameters:

  • adapter (Moneta store)

    The underlying store

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


19
20
21
22
23
# 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

Instance Method Details

#each_keyEnumerator #each_key {|key| ... } ⇒ self

Note:

Not every Moneta store implements this method, a NotImplementedError is raised if it is not supported.

Calls block once for each key in store, passing the key as a parameter. If no block is given, an enumerator is returned instead.

Overloads:

  • #each_keyEnumerator

    Returns An all-the-keys enumerator.

    Returns:

    • (Enumerator)

      An all-the-keys enumerator

  • #each_key {|key| ... } ⇒ self

    Yield Parameters:

    • key (Object)

      Each key is yielded to the supplied block

    Returns:

    • (self)


26
27
28
29
30
# 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