class Puppet::Util::CacheAccumulator::Base

Attributes

attribute[R]
klass[R]

Public Class Methods

new(klass, attribute) click to toggle source
# File lib/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

Public Instance Methods

do_multi_find(keys) click to toggle source
# File lib/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) click to toggle source
# File lib/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
reset() click to toggle source
# File lib/puppet/util/rails/cache_accumulator.rb, line 21
def reset
  @store = {}
end
store() click to toggle source
# File lib/puppet/util/rails/cache_accumulator.rb, line 17
def store
  @store || reset
end