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
automate-it-0.9.2 lib/hashcache.rb
automate-it-0.9.1 lib/hashcache.rb
automate-it-0.9.0 lib/hashcache.rb
automateit-0.70923 lib/hashcache.rb
automateit-0.70928 lib/hashcache.rb
automateit-0.70930 lib/hashcache.rb
automateit-0.71003 lib/hashcache.rb
automateit-0.71012 lib/hashcache.rb
automateit-0.71006 lib/hashcache.rb
automateit-0.71021 lib/hashcache.rb
automateit-0.71030 lib/hashcache.rb
automateit-0.71031.1 lib/hashcache.rb
automateit-0.71031.2 lib/hashcache.rb
automateit-0.71017 lib/hashcache.rb
automateit-0.71101.1 lib/hashcache.rb
automateit-0.71101.2 lib/hashcache.rb
automateit-0.71102 lib/hashcache.rb
automateit-0.71103 lib/hashcache.rb
automateit-0.71031 lib/hashcache.rb
automateit-0.71101 lib/hashcache.rb