Module: BennyCache::Related

Defined in:
lib/benny_cache/related.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/benny_cache/related.rb', line 3

def self.included(base) #:nodoc:
  base.send :include, BennyCache::Base

  base.extend BennyCache::ClassMethods
  unless(base.class_variable_defined? :@@benny_related_indexes)
    base.class_variable_set(:@@benny_related_indexes, [])
  end

  unless(base.class_variable_defined? :@@benny_related_methods)
    base.class_variable_set(:@@benny_related_methods, [])
  end

  if base.respond_to?(:after_save)
    base.after_save :benny_cache_clear_related
  end

  if base.respond_to?(:after_destroy)
    base.after_destroy :benny_cache_clear_related
  end
end

Instance Method Details



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/benny_cache/related.rb', line 24

def benny_cache_clear_related
  self.class.class_variable_get(:@@benny_related_indexes).each do |key|
    local_field, klass, data_cache = key.split('/')
    local_field = local_field[1, local_field.length]
    const = benny_constantize(klass)
    id = self.send(local_field)
    const.benny_data_cache_delete(id, data_cache) if id
  end

  self.class.class_variable_get(:@@benny_related_methods).each do |key|
    local_field, klass, method_cache = key.split('/')
    local_field = local_field[1, local_field.length]
    const = benny_constantize(klass)
    id = self.send(local_field)
    const.benny_method_cache_delete(id, method_cache) if id
  end

end