Module: Cacheable::MethodCache

Included in:
Caches
Defined in:
lib/cacheable/types/method_cache.rb

Instance Method Summary collapse

Instance Method Details

#with_method(*methods) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cacheable/types/method_cache.rb', line 3

def with_method(*methods)
  self.cached_methods ||= []
  self.cached_methods += methods

  class_eval do
    after_commit :expire_method_cache, :on => :update
  end

  methods.each do |meth|
    method_name = "cached_#{meth}"
    define_method(method_name) do
      iv = Cacheable.escape_punctuation("@#{method_name}")
      if instance_variable_get(iv).nil?
        instance_variable_set(iv,
          (Cacheable.fetch method_cache_key(meth) do
            send(meth)
          end)
        )
      end
      instance_variable_get(iv)
    end
  end
end