module MethodCaching::ClassMethods

Public Instance Methods

cache_method(method, options = {}) click to toggle source
# File lib/method_caching.rb, line 8
def cache_method(method, options = {})
  old = "_#{method}".to_sym
  alias_method old, method
  define_method method do |*args|
    Rails.cache.fetch(cache_key_name(method)) do
      send(old, *args)
    end
  end

  clear_methods = [options[:clear_on]].flatten.compact
  clear_methods.each do |clear_method|
    cache_method_clear_on(clear_method, method)
  end
end
cache_method_clear_on(clear_method, cache_method) click to toggle source
# File lib/method_caching.rb, line 23
def cache_method_clear_on(clear_method, cache_method)
  original_method = "_cache_method_clear_on_#{clear_method}"
  alias_method original_method, clear_method

  define_method clear_method do |*args, &blk|
    self.clear_cache_on cache_method
    send(original_method, *args, &blk)
  end
end
identifier() click to toggle source
# File lib/method_caching.rb, line 33
def identifier
  @identifier
end
method_caching(identifier_attr = :id) click to toggle source
# File lib/method_caching.rb, line 3
def method_caching(identifier_attr = :id)
  @identifier = identifier_attr.to_sym
  self.send(:include, InstanceMethods)
end