Class: IdentityCache::Cached::Recursive::Association
- Inherits:
-
Cached::Association
- Object
- Cached::Association
- IdentityCache::Cached::Recursive::Association
- Defined in:
- lib/identity_cache/cached/recursive/association.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#dehydrated_variable_name ⇒ Object
readonly
Returns the value of attribute dehydrated_variable_name.
Instance Method Summary collapse
- #build ⇒ Object
- #clear(record) ⇒ Object
- #embedded_by_reference? ⇒ Boolean
- #embedded_recursively? ⇒ Boolean
- #fetch(records) ⇒ Object
- #fetch_async(load_strategy, records) ⇒ Object
-
#initialize(name, reflection:) ⇒ Association
constructor
A new instance of Association.
- #read(record) ⇒ Object
- #set_with_inverse(record, association_target) ⇒ Object
- #write(record, association_target) ⇒ Object
Constructor Details
#initialize(name, reflection:) ⇒ Association
Returns a new instance of Association.
7 8 9 10 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 7 def initialize(name, reflection:) super @dehydrated_variable_name = :"@dehydrated_#{name}" end |
Instance Attribute Details
#dehydrated_variable_name ⇒ Object (readonly)
Returns the value of attribute dehydrated_variable_name.
12 13 14 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 12 def dehydrated_variable_name @dehydrated_variable_name end |
Instance Method Details
#build ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 14 def build cached_association = self model = reflection.active_record model.define_method(cached_accessor_name) do cached_association.read(self) end ParentModelExpiration.add_parent_expiry_hook(self) end |
#clear(record) ⇒ Object
53 54 55 56 57 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 53 def clear(record) if record.instance_variable_defined?(records_variable_name) record.remove_instance_variable(records_variable_name) end end |
#embedded_by_reference? ⇒ Boolean
69 70 71 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 69 def false end |
#embedded_recursively? ⇒ Boolean
73 74 75 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 73 def true end |
#fetch(records) ⇒ Object
59 60 61 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 59 def fetch(records) fetch_async(LoadStrategy::Eager, records) { |child_records| child_records } end |
#fetch_async(load_strategy, records) ⇒ Object
63 64 65 66 67 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 63 def fetch_async(load_strategy, records) (load_strategy, records) do yield records.flat_map(&cached_accessor_name).tap(&:compact!) end end |
#read(record) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 25 def read(record) assoc = record.association(name) if !assoc.loaded? && assoc.target.blank? && (record.send(:loaded_by_idc?) || assoc.klass.should_use_cache?) if record.instance_variable_defined?(records_variable_name) record.instance_variable_get(records_variable_name) elsif record.instance_variable_defined?(dehydrated_variable_name) dehydrated_target = record.instance_variable_get(dehydrated_variable_name) association_target = hydrate_association_target(assoc.klass, dehydrated_target) record.remove_instance_variable(dehydrated_variable_name) set_with_inverse(record, association_target) else assoc.load_target end else assoc.load_target end end |
#set_with_inverse(record, association_target) ⇒ Object
48 49 50 51 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 48 def set_with_inverse(record, association_target) set_inverse(record, association_target) write(record, association_target) end |
#write(record, association_target) ⇒ Object
44 45 46 |
# File 'lib/identity_cache/cached/recursive/association.rb', line 44 def write(record, association_target) record.instance_variable_set(records_variable_name, association_target) end |