Class: Volt::GenericCountingPool

Inherits:
GenericPool show all
Defined in:
lib/volt/utils/generic_counting_pool.rb

Overview

A counting pool behaves like a normal GenericPool, except for each time lookup is called, remove should be called when complete. The item will be completely removed from the GenericCountingPool only when it has been removed an equal number of times it has been looked up.

Direct Known Subclasses

ModelIdentityMap

Instance Attribute Summary

Attributes inherited from GenericPool

#pool

Instance Method Summary collapse

Methods inherited from GenericPool

#create_new_item, #initialize, #lookup_all

Constructor Details

This class inherits a constructor from Volt::GenericPool

Instance Method Details

#find(*args, &block) ⇒ Object

Finds an item and tracks that it was checked out. Use #remove when the item is no longer needed.



17
18
19
20
21
22
23
# File 'lib/volt/utils/generic_counting_pool.rb', line 17

def find(*args, &block)
  item = __lookup(*args, &block)

  item[0] += 1

  item[1]
end

#generate_new(*args) ⇒ Object

return a created item with a count



11
12
13
# File 'lib/volt/utils/generic_counting_pool.rb', line 11

def generate_new(*args)
  [0, create(*args)]
end

#lookup(*args, &block) ⇒ Object

Lookups an item



26
27
28
29
30
# File 'lib/volt/utils/generic_counting_pool.rb', line 26

def lookup(*args, &block)
  item = super(*args, &block)

  item[1]
end

#remove(*args) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/volt/utils/generic_counting_pool.rb', line 36

def remove(*args)
  item    = __lookup(*args)
  item[0] -= 1

  if item[0] == 0
    # Last one using this item has removed it.
    super(*args)
  end
end

#transform_item(item) ⇒ Object



32
33
34
# File 'lib/volt/utils/generic_counting_pool.rb', line 32

def transform_item(item)
  [0, item]
end