Class: IdentityCache::Cached::Recursive::Association
- Inherits:
-
Association
- Object
- Association
- IdentityCache::Cached::Recursive::Association
show all
- Defined in:
- lib/identity_cache/cached/recursive/association.rb
Overview
Instance Attribute Summary collapse
Attributes inherited from Association
#cached_accessor_name, #name, #records_variable_name, #reflection
Instance Method Summary
collapse
Methods inherited from Association
#embedded?, #inverse_name, #validate
Constructor Details
#initialize(name, reflection:) ⇒ Association
Returns a new instance of Association.
6
7
8
9
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 6
def initialize(name, reflection:)
super
@dehydrated_variable_name = :"@dehydrated_#{name}"
end
|
Instance Attribute Details
#dehydrated_variable_name ⇒ Object
Returns the value of attribute dehydrated_variable_name.
11
12
13
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 11
def dehydrated_variable_name
@dehydrated_variable_name
end
|
Instance Method Details
#build ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 13
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
52
53
54
55
56
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 52
def clear(record)
if record.instance_variable_defined?(records_variable_name)
record.remove_instance_variable(records_variable_name)
end
end
|
#embedded_by_reference? ⇒ Boolean
68
69
70
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 68
def embedded_by_reference?
false
end
|
#embedded_recursively? ⇒ Boolean
72
73
74
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 72
def embedded_recursively?
true
end
|
#fetch(records) ⇒ Object
58
59
60
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 58
def fetch(records)
fetch_async(LoadStrategy::Eager, records) { |child_records| child_records }
end
|
#fetch_async(load_strategy, records) ⇒ Object
62
63
64
65
66
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 62
def fetch_async(load_strategy, records)
fetch_embedded_async(load_strategy, records) do
yield records.flat_map(&cached_accessor_name).tap(&:compact!)
end
end
|
#read(record) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 24
def read(record)
assoc = record.association(name)
if assoc.klass.should_use_cache? && !assoc.loaded? && assoc.target.blank?
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
47
48
49
50
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 47
def set_with_inverse(record, association_target)
set_inverse(record, association_target)
write(record, association_target)
end
|
#write(record, association_target) ⇒ Object
43
44
45
|
# File 'lib/identity_cache/cached/recursive/association.rb', line 43
def write(record, association_target)
record.instance_variable_set(records_variable_name, association_target)
end
|