Sha256: 77449a2f716f45896639bd131ca873d55c46b97f5f6f5f76219f56fdc937c281

Contents?: true

Size: 780 Bytes

Versions: 10

Compression:

Stored size: 780 Bytes

Contents

require 'facets/core/hash/having_fallback'

class Hash

  # Define a fallback object for #fetch and #[].
  #
  #   f = Hash[:b=>2]
  #   h = Hash[:a=>1].having_aquisition(f)
  #   h[:b] => 2

  def with_aquisition(fallback)
    @fallback = fallback
    unless @fallsback
      def self.[](key)
        begin
          return @fallback[key] if @fallback and not key?(key)
        rescue
        end
        val = super
        val.fallback(self) if Hash == val
        val
      end
      def self.fetch(key, *args, &blk)
        begin
          return @fallback.fetch(key, *args, &blk) if @fallback and not key?(key)
        rescue
        end
        val = super
        val.fallback(self) if Hash == val
        val
      end
      @fallsback = true
    end
    self
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-1.7.0 lib/facets/core/hash/having_aquisition.rb
facets-1.7.30 lib/facets/core/hash/having_aquisition.rb
facets-1.7.38 lib/facets/core/hash/having_aquisition.rb
facets-1.7.46 lib/facets/core/hash/having_aquisition.rb
facets-1.8.0 lib/facets/core/hash/having_aquisition.rb
facets-1.8.20 lib/facets/core/hash/having_aquisition.rb
facets-1.8.49 lib/facets/core/hash/having_aquisition.rb
facets-1.8.51 lib/facets/core/hash/having_aquisition.rb
facets-1.8.54 lib/facets/core/hash/having_aquisition.rb
facets-1.8.8 lib/facets/core/hash/having_aquisition.rb