module ActsAsMethodCacheable::Internal

Public Instance Methods

before_cache_method(method, class_level=false) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 59
def before_cache_method(method, class_level=false)
  raise "#{method} not defined in class #{self.class.to_s}" unless self.class.method_defined?(method)
  raise "method with params is not supported by acts_as_method_cacheable yet!" unless method(method.to_sym).arity === 0
  self.instance_level_cached_methods ||= []
  return false if class_level_cached_methods.include?(method)
  return false if self.instance_level_cached_methods.include?(method)
  (class_level ? class_level_cached_methods : self.instance_level_cached_methods).push(method)
  true
end
cache_method(params) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 51
def cache_method(params)
  normalize_cachable_params(params) do |self_methods, association_methods|
    self_methods.each { |method| _cache_method(method) }
    association_methods.each{ |pair| cache_association_method(*pair.shift) }
  end
  self
end
reset_cache(method) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 47
def reset_cache(method)
  remove_instance_variable(cache_var_name(method)) if cache_var_defined?(method)
end

Private Instance Methods

_cache_method(method) click to toggle source

method which only accept a symbol as parameter

# File lib/acts_as_method_cacheable.rb, line 86
    def _cache_method(method)
      return unless before_cache_method(method)

      instance_eval <<-CACHEEND, __FILE__, __LINE__ + 1
        class << self
          def #{method}
            unless cache_var_defined?(:#{method})
              cache_var_set(:#{method}, super)
            end
            cache_var_get(:#{method})
          end
        end
      CACHEEND
    end
cache_association_method(association, association_params) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 78
def cache_association_method(association, association_params)
  instances = send(association)
  (instances.respond_to?(:each) ? instances : [instances]).each do |instance|
    instance.cache_method(association_params)
  end
end
cache_var_defined?(method) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 113
def cache_var_defined?(method)
  instance_variable_defined?(cache_var_name(method))
end
cache_var_get(method) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 109
def cache_var_get(method)
  instance_variable_get(cache_var_name(method))
end
cache_var_name(method) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 101
def cache_var_name(method)
  "@cached_var_for_method_#{method}".to_sym
end
cache_var_set(method, value) click to toggle source
# File lib/acts_as_method_cacheable.rb, line 105
def cache_var_set(method, value)
  instance_variable_set(cache_var_name(method), value)
end
normalize_cachable_params(params) { |self_methods, association_methods| ... } click to toggle source
# File lib/acts_as_method_cacheable.rb, line 71
def normalize_cachable_params(params, &block)
  normalized_params = params.is_a?(Array) ? params : [params]
  self_methods = normalized_params.select{ |item| item.is_a?(Symbol) }
  association_methods = normalized_params.select{ |item| item.is_a?(Hash) }
  yield self_methods, association_methods
end