Module: MethodCaching::ClassMethods

Defined in:
lib/method_caching.rb

Instance Method Summary collapse

Instance Method Details

#cache_method(method) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/method_caching.rb', line 8

def cache_method(method)
  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
end

#cache_method_clear_on(clear_method, cache_method) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/method_caching.rb', line 18

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

#identifierObject



28
29
30
# File 'lib/method_caching.rb', line 28

def identifier
  @identifier
end

#method_caching(identifier_attr = :id) ⇒ Object



3
4
5
6
# File 'lib/method_caching.rb', line 3

def method_caching(identifier_attr = :id)
  @identifier = identifier_attr.to_sym
  self.send(:include, InstanceMethods)
end