Class: IdentityCache::Cached::Recursive::Association

Inherits:
Association
  • Object
show all
Defined in:
lib/identity_cache/cached/recursive/association.rb

Overview

:nodoc:

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.



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_nameObject (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

#buildObject



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

Returns:

  • (Boolean)


69
70
71
# File 'lib/identity_cache/cached/recursive/association.rb', line 69

def embedded_by_reference?
  false
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/identity_cache/cached/recursive/association.rb', line 73

def embedded_recursively?
  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)
  fetch_embedded_async(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