13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/gen_cache/cache_types/association_cache.rb', line 13
def cached_assoc_methods(name)
method_name = "cached_#{name}"
define_method(method_name) do
cache_key = GenCache.association_key(self, name)
if instance_variable_get("@#{method_name}").nil?
instance_keys = GenCache.fetch(cache_key) do
Array.wrap(self.send(name)).map { |obj| GenCache.instance_key(obj.class, obj.id) }
end
association = GenCache.fetch(instance_keys) do |key_blobs|
result = {}
instance_keys.each do |ik|
key_parts = ik[:key].scan(/(^.*)\/(.*)\/(.*$)/).flatten
result[ik] = Object.const_get(key_parts.first.singularize.capitalize).send(:find, key_parts.last.to_i)
end
result
end
association = if association.size == 0
name.to_s.pluralize == name.to_s ? [] : nil
elsif association.size == 1
name.to_s.pluralize == name.to_s ? association : association.first
else
association
end
instance_variable_set("@#{method_name}", association)
end
instance_variable_get("@#{method_name}")
end
end
|