Module: MethodCachable::ClassMethods

Defined in:
lib/method_cachable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_cachableObject



24
25
26
27
# File 'lib/method_cachable.rb', line 24

def acts_as_cachable
  klass = self
  [:after_touch, :before_destroy, :after_save].each { |callback| klass.send(callback, :clear_cache) }
end

#cached_methods(*methods) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/method_cachable.rb', line 29

def cached_methods(*methods)
  name = self.name
  eval_string = ""
  methods.each do |method|
    method = method.to_s
    eval_string += "
      alias_method :old_#{method}, :#{method}

      def #{method}(*args)
        sig = createsig(args || self.id)
        with_env \"#{name}:#{method}:\#{self.id}:\#{sig}\", cache_list_key do
          old_#{method}(*args)
        end
      end
    "
  end
  class_eval(eval_string)
end