Class: PredictiveLoad::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/predictive_load/loader.rb

Overview

Predictive loader

Usage: ActiveRecord::Relation.collection_observer = LazyLoader

Direct Known Subclasses

Watcher

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ Loader

Returns a new instance of Loader.



13
14
15
# File 'lib/predictive_load/loader.rb', line 13

def initialize(records)
  @records = records
end

Class Method Details

.observe(records) ⇒ Object



9
10
11
# File 'lib/predictive_load/loader.rb', line 9

def self.observe(records)
  new(records).observe
end

Instance Method Details

#all_records_will_likely_load_association?(association_name) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/predictive_load/loader.rb', line 29

def all_records_will_likely_load_association?(association_name)
  if defined?(Mocha) && association_name.to_s.index('_stub_')
    false
  else
    true
  end
end

#loading_association(record, association_name) ⇒ Object



23
24
25
26
27
# File 'lib/predictive_load/loader.rb', line 23

def loading_association(record, association_name)
  if all_records_will_likely_load_association?(association_name)
    preload(association_name)
  end
end

#observeObject



17
18
19
20
21
# File 'lib/predictive_load/loader.rb', line 17

def observe
  records.each do |record|
    record.collection_observer = self
  end
end