Class: Puppet::Util::CacheAccumulator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/puppet/util/rails/cache_accumulator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, attribute) ⇒ Base

Returns a new instance of Base.



11
12
13
14
15
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 11

def initialize(klass, attribute)
  @klass = klass
  @attribute = attribute
  @find_or_create = "find_or_create_by_#{@attribute.to_s}".intern
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



9
10
11
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 9

def attribute
  @attribute
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 9

def klass
  @klass
end

Instance Method Details

#do_multi_find(keys) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 37

def do_multi_find(keys)
  result = 0
  @klass.find(:all, :conditions => {@attribute => keys}).each do |obj|
    store[obj.send(@attribute)] = obj
    result += 1
  end
  result
end

#find(*keys) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 25

def find(*keys)
  result = nil
  if keys.length == 1
    result = store[keys[0]] ||= @klass.send(@find_or_create, *keys)
  else
    found, missing = keys.partition {|k| store.include? k}
    result = found.length
    result += do_multi_find(missing) if missing.length > 0
  end
  result
end

#resetObject



21
22
23
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 21

def reset
  @store = {}
end

#storeObject



17
18
19
# File 'lib/vendor/puppet/util/rails/cache_accumulator.rb', line 17

def store
  @store || reset
end