Sha256: 041067022ca267e1cf169d74d5339b7aec0c7580342a97216ecd6cf1ff176a14

Contents?: true

Size: 990 Bytes

Versions: 37

Compression:

Stored size: 990 Bytes

Contents

require 'volt/utils/generic_pool'

# 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.
class GenericCountingPool < GenericPool
  # return a created item with a count
  def generate_new(*args)
    [0, create(*args)]
  end

  # Finds an item and tracks that it was checked out.  Use
  # #remove when the item is no longer needed.
  def find(*args, &block)
    item = __lookup(*args, &block)

    item[0] += 1

    return item[1]
  end

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

    return item[1]
  end

  def transform_item(item)
    [0, item]
  end

  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
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
volt-0.8.14 lib/volt/utils/generic_counting_pool.rb
volt-0.8.13 lib/volt/utils/generic_counting_pool.rb
volt-0.8.11 lib/volt/utils/generic_counting_pool.rb
volt-0.8.10 lib/volt/utils/generic_counting_pool.rb
volt-0.8.9 lib/volt/utils/generic_counting_pool.rb
volt-0.8.8 lib/volt/utils/generic_counting_pool.rb
volt-0.8.7 lib/volt/utils/generic_counting_pool.rb
volt-0.8.6 lib/volt/utils/generic_counting_pool.rb
volt-0.8.5 lib/volt/utils/generic_counting_pool.rb
volt-0.8.4 lib/volt/utils/generic_counting_pool.rb
volt-0.8.3 lib/volt/utils/generic_counting_pool.rb
volt-0.8.2 lib/volt/utils/generic_counting_pool.rb
volt-0.8.1 lib/volt/utils/generic_counting_pool.rb
volt-0.8.0 lib/volt/utils/generic_counting_pool.rb
volt-0.7.23 lib/volt/utils/generic_counting_pool.rb
volt-0.7.22 lib/volt/utils/generic_counting_pool.rb
volt-0.7.21 lib/volt/utils/generic_counting_pool.rb
volt-0.7.20 lib/volt/utils/generic_counting_pool.rb
volt-0.7.19 lib/volt/utils/generic_counting_pool.rb
volt-0.7.18 lib/volt/utils/generic_counting_pool.rb