Class: Weakling::IdHash

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/weakling/collections.rb

Defined Under Namespace

Classes: IdWeakRef

Instance Method Summary collapse

Constructor Details

#initializeIdHash

Returns a new instance of IdHash.



7
8
9
10
# File 'lib/weakling/collections.rb', line 7

def initialize
  @hash = Hash.new
  @queue = Weakling::RefQueue.new
end

Instance Method Details

#[](id) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/weakling/collections.rb', line 20

def [](id)
  _cleanup
  if wr = @hash[id]
    return wr.get rescue nil
  end

  return nil
end

#_cleanupObject



38
39
40
41
42
# File 'lib/weakling/collections.rb', line 38

def _cleanup
  while ref = @queue.poll
    @hash.delete(ref.id)
  end
end

#add(object) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/weakling/collections.rb', line 29

def add(object)
  _cleanup
  wr = IdWeakRef.new(object, @queue)

  @hash[wr.id] = wr

  return wr.id
end

#eachObject



44
45
46
# File 'lib/weakling/collections.rb', line 44

def each
  @hash.each {|id, wr| obj = wr.get rescue nil; yield [id,obj] if obj}
end