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

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.



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

def records_variable_name
  @records_variable_name
end

Instance Method Details

#buildObject



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

def build
  reflection.active_record.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
    def #{cached_accessor_name}
      association_klass = association(:#{name}).klass
      if #{reflection.foreign_key}.present? && !association(:#{name}).loaded? && (loaded_by_idc? || association_klass.should_use_cache?)
        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



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

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)


102
103
104
# File 'lib/identity_cache/cached/belongs_to.rb', line 102

def embedded_by_reference?
  false
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/identity_cache/cached/belongs_to.rb', line 98

def embedded_recursively?
  false
end

#fetch(records) ⇒ Object



35
36
37
# File 'lib/identity_cache/cached/belongs_to.rb', line 35

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

#fetch_async(load_strategy, records) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/identity_cache/cached/belongs_to.rb', line 39

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

    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)

      foreign_type_fetcher = Object.const_get(
        owner_record.send(reflection.foreign_type)
      ).cached_model.cached_primary_index
      db_ids = type_fetcher_to_db_ids_hash[foreign_type_fetcher] ||= []
      db_ids << associated_id
    end

    load_strategy.load_batch(type_fetcher_to_db_ids_hash) do |batch_load_result|
      batch_records = []

      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)

        foreign_type_fetcher = Object.const_get(
          owner_record.send(reflection.foreign_type)
        ).cached_model.cached_primary_index

        associated_record = batch_load_result.fetch(foreign_type_fetcher).fetch(associated_id)
        batch_records << owner_record
        write(owner_record, associated_record)
      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

    if ids_to_owner_record.any?
      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)
          write(owner_record, associated_record)
        end

        yield associated_records_by_id.values.compact
      end
    else
      yield records.filter_map { |record| record.instance_variable_get(records_variable_name) }
    end
  end
end

#write(owner_record, associated_record) ⇒ Object



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

def write(owner_record, associated_record)
  owner_record.instance_variable_set(records_variable_name, associated_record)
end