Sha256: 4b97522e935a5a39c3e947a5b87c63e55d3440679760b1d3de5d6e01ea119e56
Contents?: true
Size: 667 Bytes
Versions: 5
Compression:
Stored size: 667 Bytes
Contents
# frozen_string_literal: true require 'delegate' module Icasework ## # A hash which will attempt to lazy load a value from given block when the key # is missing. # class LazyHash < SimpleDelegator def initialize(hash, key = nil, &block) @hash = hash @key = key @block = block @hash.default_proc = proc do |h, k| new_hash = @block.call new_hash = new_hash[@key] if @key h[k] = new_hash.fetch(k) end super(@hash) end def [](key) value = @hash[key] case value when Hash LazyHash.new(value, key, &@block) else value end end end end
Version data entries
5 entries across 5 versions & 1 rubygems