module NoBrainer::Criteria::Cache

Public Class Methods

reload_on(*methods) click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/cache.rb, line 69
def self.reload_on(*methods)
  methods.each do |method|
    define_method(method) do |*args, &block|
      reload
      super(*args, &block).tap { reload }
    end
  end
end
use_cache_for(*methods) click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/cache.rb, line 61
def self.use_cache_for(*methods)
  methods.each do |method|
    define_method(method) do |*args, &block|
      @cache ? @cache.__send__(method, *args, &block) : super(*args, &block)
    end
  end
end

Public Instance Methods

_override_cache(cache) click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 57
def _override_cache(cache)
  @cache = cache
end
cached?() click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 35
def cached?
  !!@cache
end
each(options={}, &block) click to toggle source
Calls superclass method
# 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
inspect() click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/cache.rb, line 14
def inspect
  msg = super
  msg = "#{msg} \e[1;37m# #{@cache.size} results cached\e[0m" if @cache && with_cache?
  msg
end
merge!(criteria, options={}) click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/cache.rb, line 20
def merge!(criteria, options={})
  if options[:copy_cache_from] && options[:copy_cache_from].cached?
    @cache = options[:copy_cache_from].instance_variable_get(:@cache)
  end
  super
end
reload() click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 31
def reload
  @cache = nil
end
with_cache() click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 6
def with_cache
  chain(:with_cache => true)
end
with_cache?() click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 27
def with_cache?
  finalized_criteria.options[:with_cache] != false
end
without_cache() click to toggle source
# File lib/no_brainer/criteria/cache.rb, line 10
def without_cache
  chain(:with_cache => false)
end

Private Instance Methods

apply_named_scope(name, args, block) click to toggle source
Calls superclass method
# File lib/no_brainer/criteria/cache.rb, line 83
def apply_named_scope(name, args, block)
  return super unless with_cache?
  @scope_cache ||= {}
  @scope_cache[[name, args, block]] ||= super
end