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.



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)


95
96
97
# File 'lib/identity_cache/cached/belongs_to.rb', line 95

def embedded_by_reference?
  false
end

#embedded_recursively?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/identity_cache/cached/belongs_to.rb', line 91

def embedded_recursively?
  false
end

#fetch(records) ⇒ Object



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

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

#fetch_async(load_strategy, records) ⇒ Object



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
83
84
85
86
87
88
89
# File 'lib/identity_cache/cached/belongs_to.rb', line 38

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

    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
  end
end

#write(owner_record, associated_record) ⇒ Object



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

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