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.



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

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

#buildObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/identity_cache/cached/recursive/association.rb', line 13

def build
  reflection.active_record.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def #{cached_accessor_name}
      fetch_recursively_cached_association(
        :#{records_variable_name},
        :#{dehydrated_variable_name},
        :#{name}
      )
    end
  RUBY

  ParentModelExpiration.add_parent_expiry_hook(self)
end

#clear(record) ⇒ Object



35
36
37
38
39
# File 'lib/identity_cache/cached/recursive/association.rb', line 35

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)


51
52
53
# File 'lib/identity_cache/cached/recursive/association.rb', line 51

def embedded_by_reference?
  false
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/identity_cache/cached/recursive/association.rb', line 55

def embedded_recursively?
  true
end

#fetch(records) ⇒ Object



41
42
43
# File 'lib/identity_cache/cached/recursive/association.rb', line 41

def fetch(records)
  fetch_async(LoadStrategy::Eager, records) { |child_records| child_records }
end

#fetch_async(load_strategy, records) ⇒ Object



45
46
47
48
49
# File 'lib/identity_cache/cached/recursive/association.rb', line 45

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



27
28
29
# File 'lib/identity_cache/cached/recursive/association.rb', line 27

def read(record)
  record.public_send(cached_accessor_name)
end

#write(record, records) ⇒ Object



31
32
33
# File 'lib/identity_cache/cached/recursive/association.rb', line 31

def write(record, records)
  record.instance_variable_set(records_variable_name, records)
end