Method: NoBrainer::Criteria::Cache#each

Defined in:
lib/no_brainer/criteria/cache.rb

#each(options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/no_brainer/criteria/cache.rb', line 39

def each(options={}, &block)
  return super unless with_cache? && !options[:no_cache] && block && !@cache_too_small
  return @cache.each(&block) if @cache

  cache = []
  super(options.merge(:no_cache => true)) do |instance|
    block.call(instance)
    cache << instance unless @cache_too_small

    if cache.size > NoBrainer::Config.criteria_cache_max_entries
      cache = []
      @cache_too_small = true
    end
  end
  @cache = cache unless @cache_too_small
  self
end