Class: IdentityCache::Cached::BelongsTo

Inherits:
Association show all
Defined in:
lib/identity_cache/cached/belongs_to.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Association

#cached_accessor_name, #name, #reflection

Instance Method Summary collapse

Methods inherited from Association

#embedded?, #initialize, #inverse_name, #read, #validate, #write

Constructor Details

This class inherits a constructor from IdentityCache::Cached::Association

Instance Attribute Details

#records_variable_nameObject (readonly)

Returns the value of attribute records_variable_name.



5
6
7
# File 'lib/identity_cache/cached/belongs_to.rb', line 5

def records_variable_name
  @records_variable_name
end

Instance Method Details

#buildObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/identity_cache/cached/belongs_to.rb', line 7

def build
  reflection.active_record.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def #{cached_accessor_name}
      association_klass = association(:#{name}).klass
      if association_klass.should_use_cache? && #{reflection.foreign_key}.present? && !association(:#{name}).loaded?
        if defined?(#{records_variable_name})
          #{records_variable_name}
        else
          #{records_variable_name} = association_klass.fetch_by_id(#{reflection.foreign_key})
        end
      else
        #{name}
      end
    end
  RUBY
end

#clear(record) ⇒ Object



24
25
26
27
28
# File 'lib/identity_cache/cached/belongs_to.rb', line 24

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)


88
89
90
# File 'lib/identity_cache/cached/belongs_to.rb', line 88

def embedded_by_reference?
  false
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/identity_cache/cached/belongs_to.rb', line 84

def embedded_recursively?
  false
end

#fetch(records) ⇒ Object



30
31
32
# File 'lib/identity_cache/cached/belongs_to.rb', line 30

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

#fetch_async(load_strategy, records) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/identity_cache/cached/belongs_to.rb', line 34

def fetch_async(load_strategy, records)
  if reflection.polymorphic?
    cache_keys_to_associated_ids = {}

    records.each do |owner_record|
      associated_id = owner_record.send(reflection.foreign_key)
      next unless associated_id && !owner_record.instance_variable_defined?(records_variable_name)
      associated_cache_key = Object.const_get(
        owner_record.send(reflection.foreign_type)
      ).cached_model.cached_primary_index
      unless cache_keys_to_associated_ids[associated_cache_key]
        cache_keys_to_associated_ids[associated_cache_key] = {}
      end
      cache_keys_to_associated_ids[associated_cache_key][associated_id] = owner_record
    end

    load_strategy.load_batch(cache_keys_to_associated_ids) do |associated_records_by_cache_key|
      batch_records = []
      associated_records_by_cache_key.each do |cache_key, associated_records|
        associated_records.keys.each do |id, associated_record|
          owner_record = cache_keys_to_associated_ids.fetch(cache_key).fetch(id)
          batch_records << owner_record
          owner_record.instance_variable_set(records_variable_name, associated_record)
        end
      end

      yield batch_records
    end
  else
    ids_to_owner_record = records.each_with_object({}) do |owner_record, hash|
      associated_id = owner_record.send(reflection.foreign_key)
      if associated_id && !owner_record.instance_variable_defined?(records_variable_name)
        hash[associated_id] = owner_record
      end
    end

    load_strategy.load_multi(
      reflection.klass.cached_primary_index,
      ids_to_owner_record.keys
    ) do |associated_records_by_id|
      associated_records_by_id.each do |id, associated_record|
        owner_record = ids_to_owner_record.fetch(id)
        owner_record.instance_variable_set(records_variable_name, associated_record)
      end

      yield associated_records_by_id.values.compact
    end
  end
end