Class: Volt::GenericPool

Inherits:
Object show all
Defined in:
lib/volt/utils/generic_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGenericPool

Returns a new instance of GenericPool.



17
18
19
# File 'lib/volt/utils/generic_pool.rb', line 17

def initialize
  @pool = {}
end

Instance Attribute Details

#poolObject (readonly)

Returns the value of attribute pool.



15
16
17
# File 'lib/volt/utils/generic_pool.rb', line 15

def pool
  @pool
end

Instance Method Details

#clearObject



21
22
23
# File 'lib/volt/utils/generic_pool.rb', line 21

def clear
  @pool = {}
end

#create_new_item(*args) ⇒ Object

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



59
60
61
62
63
64
65
66
67
# 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

#inspectObject



114
115
116
# File 'lib/volt/utils/generic_pool.rb', line 114

def inspect
  "<#{self.class}:#{object_id} #{@pool.inspect}>"
end

#lookup(*args, &block) ⇒ Object Also known as: __lookup



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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

#lookup_all(*args) ⇒ Object



78
79
80
81
82
83
84
85
86
# 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) ⇒ Object

Looks up the path without generating a new one



46
47
48
49
50
51
52
53
54
55
# 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


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/volt/utils/generic_pool.rb', line 118

def print
  puts '--- Running Queries ---'

  @pool.each_pair do |table, query_hash|
    query_hash.each_key do |query|
      puts "#{table.inspect}: #{query.inspect}"
    end
  end

  puts '---------------------'
end

#remove(*args) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# 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) ⇒ Object

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



70
71
72
# File 'lib/volt/utils/generic_pool.rb', line 70

def transform_item(item)
  item
end