Sha256: 5b89fb582bb9c6f7ad94ec4dcc85daafd90f6207c10adc20aa0a0fd0ddc0aa5b

Contents?: true

Size: 509 Bytes

Versions: 31

Compression:

Stored size: 509 Bytes

Contents

# A very simple cache. Works just like a Hash, but can optionally store values
# during the fetch process.
#
# Example:
#   hc = HashCache.new
#   hc.fetch("foo")
#   => nil
#   hc.fetch("foo"){:bar} # Block gets called because 'foo' is nil
#   => :bar
#   hc.fetch("foo"){raise "Block won't be called because 'foo' is cached"}
#   => :bar
class HashCache < Hash
  def fetch(key, &block)
    if has_key?(key) 
      self[key] 
    elsif block
      self[key] = block.call
    else
      nil
    end
  end
end

Version data entries

31 entries across 31 versions & 2 rubygems

Version Path
automateit-0.71220 lib/hashcache.rb
automateit-0.71104 lib/hashcache.rb
automateit-0.71221 lib/hashcache.rb
automateit-0.71111 lib/hashcache.rb
automateit-0.71219 lib/hashcache.rb
automateit-0.71112 lib/hashcache.rb
automateit-0.71226.1 lib/hashcache.rb
automateit-0.80116 lib/hashcache.rb
automateit-0.71226 lib/hashcache.rb
automateit-0.71230 lib/hashcache.rb
automateit-0.80624 lib/hashcache.rb