Class: Lifesaver::Notification::EagerLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/lifesaver/notification/eager_loader.rb

Instance Method Summary collapse

Constructor Details

#initializeEagerLoader

Returns a new instance of EagerLoader.



4
5
6
7
# File 'lib/lifesaver/notification/eager_loader.rb', line 4

def initialize
  @models_to_load = {}
  @loaded_models = {}
end

Instance Method Details

#add_model(class_name, id) ⇒ Object



9
10
11
12
13
14
# File 'lib/lifesaver/notification/eager_loader.rb', line 9

def add_model(class_name, id)
  return if model_previously_added?(class_name, id)
  models_to_load[class_name] ||= []
  models_to_load[class_name] << id
  mark_model_added(class_name, id)
end

#empty?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lifesaver/notification/eager_loader.rb', line 26

def empty?
  @models_to_load.empty?
end

#loadObject



16
17
18
19
20
21
22
23
24
# File 'lib/lifesaver/notification/eager_loader.rb', line 16

def load
  models = []
  models_to_load.each do |class_name, ids|
    klass = class_name.classify.constantize
    models |= load_associations(klass, ids)
    models_to_load.delete(class_name)
  end
  models
end