Class: Ki::HashCache

Inherits:
Hash show all
Defined in:
lib/util/hash_cache.rb

Overview

Caching Hash, resolves values at request time

Instance Method Summary collapse

Methods inherited from Hash

#require

Instance Method Details

#cache(key, &block) ⇒ Object

If key has not been defined, uses block to resolve the value. Value is stored and returned

Parameters:

  • key

    Key

  • block (Proc)

    Block which is evaluated if the key does not have value yet. Block’s value is stored to hash

Returns:

  • Existing value or one resolved with the block



24
25
26
27
28
29
# File 'lib/util/hash_cache.rb', line 24

def cache(key, &block)
  if !include?(key)
    store(key, block.call)
  end
  self[key]
end