Sha256: a66a5c794bc136c0f987f053d303882211a5370e17b35ec693dc9fa7729d953c

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

class Wref::Implementations::IdClassUnique
  def initialize(object)
    @id = object.__id__
    @class_name = object.class.name.to_sym
    ObjectSpace.define_finalizer(object, method(:destroy))
    @unique_id = object.__wref_unique_id__ if object.respond_to?(:__wref_unique_id__)
  end

  def get!
    object = get
    raise ::Wref::Recycled unless object
    return object
  end

  def get
    return nil if !@class_name || !@id

    begin
      object = ObjectSpace._id2ref(@id)
    rescue RangeError
      destroy
      return nil
    end

    #Some times this class-name will be nil for some reason - knj
    object_class_name = object.class.name

    if !object_class_name || @class_name != object_class_name.to_sym || @id != object.__id__
      destroy
      return nil
    end

    if @unique_id
      destroy
      return nil if !object.respond_to?(:__wref_unique_id__) || object.__wref_unique_id__ != @unique_id
    end

    return object
  end

  def alive?
    if get
      return true
    else
      return false
    end
  end

private

  def destroy(*args)
    @id = nil
    @class_name = nil
    @unique_id = nil
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wref-0.0.10 lib/wref/implementations/id_class_unique.rb
wref-0.0.9 lib/wref/implementations/id_class_unique.rb
wref-0.0.8 lib/wref/implementations/id_class_unique.rb