Module: GenCache::ClassMethodCache

Included in:
Caches
Defined in:
lib/gen_cache/cache_types/class_method_cache.rb

Instance Method Summary collapse

Instance Method Details

#with_class_method(*methods) ⇒ Object

Cached class method Should expire on any instance save



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gen_cache/cache_types/class_method_cache.rb', line 5

def with_class_method(*methods)
  self.cached_class_methods ||= []
  self.cached_class_methods += methods

  methods.each do |meth|
    define_singleton_method("cached_#{meth}") do |*args|
      cache_key = GenCache.class_method_key(self, meth)
      GenCache.fetch(cache_key, args: args) do
        unless args.empty?
          self.send(meth, *args)
        else
          self.send(meth)
        end
      end
    end
  end
end