module Redmemo::Cache::ClassMethods

start of class methods

Constants

DEFAULT_OPTIONS

Public Instance Methods

cache_method(method_name, options = {}) click to toggle source

usage: cache_method :m1, :cache_key => :your_custom_cache_key :cache_key is set to :cache_key by default, this is the activerecord cache key method

Calls superclass method
# File lib/redmemo.rb, line 46
def cache_method(method_name, options = {})
  options = DEFAULT_OPTIONS.merge(options || {})
  cache_module = const_get("#{self.name}Cache")
  cache_key_method = options[:cache_key] || options["cache_key"]
  cache_module.class_eval do
    define_method(method_name) do
      key = "#{self.class.name}/#{method_name}/#{self.send(cache_key_method)}"
      cached_value_for(key, ->{super()})
    end
  end
end
cache_methods(args) click to toggle source

usage: cache_methods :m1, :m2, :m3, :cache_key => :your_custom_cache_key :cache_key is set to :cache_key by default, this is the activerecord cache key method

# File lib/redmemo.rb, line 62
def cache_methods(args)
  options = args.pop if args.last.is_a?(Hash)
  methods = args

  methods.each do |method|
    self.cache_method(method, options)
  end
end