Class: Volt::GenericCountingPool
- Inherits:
-
GenericPool
- Object
- GenericPool
- Volt::GenericCountingPool
- 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
Instance Attribute Summary
Attributes inherited from GenericPool
Instance Method Summary collapse
-
#find(*args, &block) ⇒ Object
Finds an item and tracks that it was checked out.
-
#generate_new(*args) ⇒ Object
return a created item with a count.
-
#lookup(*args, &block) ⇒ Object
Lookups an item.
- #remove(*args) ⇒ Object
- #transform_item(item) ⇒ Object
Methods inherited from GenericPool
#clear, #create_new_item, #initialize, #inspect, #lookup_all, #lookup_without_generate, #print
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 31 |
# File 'lib/volt/utils/generic_counting_pool.rb', line 26 def lookup(*args, &block) # Note: must call without args because of https://github.com/opal/opal/issues/500 item = super item[1] end |
#remove(*args) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/volt/utils/generic_counting_pool.rb', line 37 def remove(*args) item = lookup_without_generate(*args) if item item[0] -= 1 if item[0] == 0 # Last one using this item has removed it. super(*args) end end end |
#transform_item(item) ⇒ Object
33 34 35 |
# File 'lib/volt/utils/generic_counting_pool.rb', line 33 def transform_item(item) [0, item] end |