Sha256: 144ccf3855f4ddacf0b2f5a1d4d0a030bf9110394b0dbe5871eb7f7d8610778e

Contents?: true

Size: 446 Bytes

Versions: 3

Compression:

Stored size: 446 Bytes

Contents

require 'weakref'

# http://endofline.wordpress.com/2011/01/09/getting-to-know-the-ruby-standard-library-weakref/
class PoroRepository::WeakHash < Hash

  class AmbivalentRef < WeakRef
    def __getobj__
      super rescue nil
    end

    alias actual __getobj__
  end

  def []= key, object
    super(key, AmbivalentRef.new(object))
  end

  def [] key
    ref = super(key)
    self.delete(key) if ref && !ref.weakref_alive?
    ref
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
poro_repository-0.2.0 lib/poro_repository/weak_hash.rb
poro_repository-0.1.2 lib/poro_repository/weak_hash.rb
poro_repository-0.0.2 lib/poro_repository/weak_hash.rb