class BigRecord::BrAssociations::CachedItemProxyFactory

Public Instance Methods

create(id, owner, reflection) click to toggle source
# 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