Sha256: 0cd2531cf6952c7ff814fc14648f6d7f6eff82559eae1f724ed7b12944c9cb87

Contents?: true

Size: 1.04 KB

Versions: 26

Compression:

Stored size: 1.04 KB

Contents

require 'volt/utils/generic_pool'

module Volt
  # 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

      item[1]
    end

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

      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
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
volt-0.9.0.pre4 lib/volt/utils/generic_counting_pool.rb
volt-0.9.0.pre3 lib/volt/utils/generic_counting_pool.rb
volt-0.9.0.pre2 lib/volt/utils/generic_counting_pool.rb
volt-0.9.0.pre1 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta9 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta8 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta7 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta6 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta5 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta4 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta3 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta2 lib/volt/utils/generic_counting_pool.rb
volt-0.8.27.beta1 lib/volt/utils/generic_counting_pool.rb
volt-0.8.26.beta1 lib/volt/utils/generic_counting_pool.rb
volt-0.8.26 lib/volt/utils/generic_counting_pool.rb
volt-0.8.24 lib/volt/utils/generic_counting_pool.rb
volt-0.8.23 lib/volt/utils/generic_counting_pool.rb
volt-0.8.22 lib/volt/utils/generic_counting_pool.rb
volt-0.8.22.beta2 lib/volt/utils/generic_counting_pool.rb
volt-0.8.22.beta1 lib/volt/utils/generic_counting_pool.rb