Class: BigRecord::BrAssociations::CachedItemProxyFactory

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/big_record/br_associations/cached_item_proxy_factory.rb

Instance Method Summary

Instance Method Details

- (Object) create(id, owner, reflection)



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/big_record/br_associations/cached_item_proxy_factory.rb', line 7

def create(id, owner, reflection)
  cache = owner[CachedItemProxy::CACHE_ATTRIBUTE]
  cached_attributes = cache["#{reflection.klass.name}:#{id}"] if cache
  cached_attributes ||= {}
  cached_attributes["id"] = id
  proxy = reflection.klass.instantiate(cached_attributes)
  proxy.extend CachedItemProxy
  proxy.instance_variable_set(:@owner, owner)
  proxy.instance_variable_set(:@reflection, reflection)
  proxy.reset

  # Overload the cached methods
  reflection.options[:cache].each do |attribute_name|
    eval "def proxy.#{attribute_name}\n"+
         "  proxy_cache[\"#{attribute_name}\"] ||= super\n"+
         "end"
  end

  proxy
end