Module: BigRecord::BrAssociations::CachedItemProxy

Defined in:
lib/big_record/br_associations/cached_item_proxy.rb

Overview

:nodoc:

Constant Summary

CACHE_ATTRIBUTE =
"attribute:associations_cache"

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

- (Object) reflection (readonly)

Returns the value of attribute reflection



7
8
9
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 7

def reflection
  @reflection
end

Instance Method Details

- (Object) ===(other)

Explicitly proxy === because the instance method removal above doesn’t catch it.



36
37
38
39
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 36

def ===(other)
  load_target
  other === @target
end

- (Object) aliased_table_name



41
42
43
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 41

def aliased_table_name
  @reflection.klass.table_name
end

- (Object) find_target



176
177
178
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 176

def find_target
  @reflection.klass.find(self.id)
end

- (Object) load_target

private

       def method_missing(method_id, *args, &block)
         if !loaded? and @reflection.options[:cache].include?(method_id)
           # FIXME: shouldn't be hard coded
           column = @owner.column_for_attribute("attribute:#{method_id}")
           if column
             if proxy_cache.has_key?(method_id)
               column.type_cast(proxy_cache[method_id])
             elsif load_target
               proxy_cache[method_id] = @target.send(method_id, *args, &block)
             end
           else
             @target.send(method_id, *args, &block)
           end
         elsif load_target
           value = @target.send(method_id, *args, &block)
           proxy_cache[method_id] = value if @reflection.options[:cache].include?(method_id)
           value
         end
       end


163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 163

def load_target
  return nil unless defined?(@loaded)

  if !loaded? and (!@owner.new_record? || foreign_key_present)
    @target = find_target
  end

  @loaded = true
  @target
rescue BigRecord::RecordNotFound
  reset
end

- (Object) loaded



59
60
61
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 59

def loaded
  @loaded = true
end

- (Boolean) loaded?

Returns:

  • (Boolean)


55
56
57
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 55

def loaded?
  @loaded
end

- (Object) proxy_cache

delegate :to_param, :to => :proxy_target

     instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_)/ }


13
14
15
16
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 13

def proxy_cache
  @owner[CACHE_ATTRIBUTE] ||= {}
  @owner[CACHE_ATTRIBUTE]["#{@reflection.klass.name}:#{id}"] ||= {}
end

- (Object) proxy_owner



18
19
20
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 18

def proxy_owner
  @owner
end

- (Object) proxy_reflection



22
23
24
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 22

def proxy_reflection
  @reflection
end

- (Object) proxy_target



26
27
28
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 26

def proxy_target
  @target
end

- (Object) reload



50
51
52
53
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 50

def reload
  reset
  load_target
end

- (Object) reset



45
46
47
48
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 45

def reset
  @loaded = false
  @target = nil
end

- (Boolean) respond_to?(symbol, include_priv = false) Also known as: proxy_respond_to?

Returns:

  • (Boolean)


30
31
32
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 30

def respond_to?(symbol, include_priv = false)
  proxy_respond_to?(symbol, include_priv) || (load_target && @target.respond_to?(symbol, include_priv))
end

- (Object) target



63
64
65
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 63

def target
  @target
end

- (Object) target=(target)



67
68
69
70
# File 'lib/big_record/br_associations/cached_item_proxy.rb', line 67

def target=(target)
  @target = target
  loaded
end