class Volt::GenericPool

Attributes

pool[R]

Public Class Methods

new() click to toggle source
# File lib/volt/utils/generic_pool.rb, line 17
def initialize
  @pool = {}
end

Public Instance Methods

__lookup(*args, &block)

Make sure we call the pool one from lookup_all and not an overridden one.

Alias for: lookup
clear() click to toggle source
# File lib/volt/utils/generic_pool.rb, line 21
def clear
  @pool = {}
end
create_new_item(*args) { |*args| ... } click to toggle source

Does the actual creating, if a block is not passed in, it calls create on the class.

# File lib/volt/utils/generic_pool.rb, line 59
def create_new_item(*args)
  if block_given?
    new_item = yield(*args)
  else
    new_item = create(*args)
  end

  transform_item(new_item)
end
inspect() click to toggle source
# File lib/volt/utils/generic_pool.rb, line 114
def inspect
  "<#{self.class}:#{object_id} #{@pool.inspect}>"
end
lookup(*args, &block) click to toggle source
# File lib/volt/utils/generic_pool.rb, line 25
def lookup(*args, &block)
  section = @pool

  # TODO: This is to work around opal issue #500
  args.pop if args.last.nil? if RUBY_PLATFORM == 'opal'

  args.each_with_index do |arg, index|
    last = (args.size - 1) == index

    if last
      # return, creating if needed
      return (section[arg] ||= create_new_item(*args, &block))
    else
      next_section = section[arg]
      next_section ||= (section[arg] = {})
      section      = next_section
    end
  end
end
Also aliased as: __lookup
lookup_all(*args) click to toggle source
# File lib/volt/utils/generic_pool.rb, line 78
def lookup_all(*args)
  result = __lookup(*args) { nil }

  if result
    result.values
  else
    []
  end
end
lookup_without_generate(*args) click to toggle source

Looks up the path without generating a new one

# File lib/volt/utils/generic_pool.rb, line 46
def lookup_without_generate(*args)
  section = @pool

  args.each_with_index do |arg, index|
    section = section[arg]
    return nil unless section
  end

  section
end
print() click to toggle source
remove(*args) click to toggle source
# File lib/volt/utils/generic_pool.rb, line 88
def remove(*args)
  stack   = []
  section = @pool

  args.each_with_index do |arg, index|
    stack << section

    if args.size - 1 == index
      unless section
        fail GenericPoolDeleteException, "An attempt was made to delete at #{arg}, full path: #{args.inspect} in #{inspect}"
      end

      section.delete(arg)
    else
      section = section[arg]
    end
  end

  (stack.size - 1).downto(1) do |index|
    node   = stack[index]
    parent = stack[index - 1]

    parent.delete(args[index - 1]) if node.size == 0
  end
end
transform_item(item) click to toggle source

Allow other pools to override how the created item gets stored.

# File lib/volt/utils/generic_pool.rb, line 70
def transform_item(item)
  item
end