Class: CacheQL::AssociationLoader

Inherits:
GraphQL::Batch::Loader
  • Object
show all
Defined in:
lib/cacheql/association_loader.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, association_name) ⇒ AssociationLoader

Returns a new instance of AssociationLoader.



9
10
11
12
13
# File 'lib/cacheql/association_loader.rb', line 9

def initialize(model, association_name)
  @model = model
  @association_name = association_name
  validate
end

Class Method Details

.validate(model, association_name) ⇒ Object



4
5
6
7
# File 'lib/cacheql/association_loader.rb', line 4

def self.validate(model, association_name)
  new(model, association_name)
  nil
end

Instance Method Details

#cache_key(record) ⇒ Object

We want to load the associations on all records, even if they have the same id



22
23
24
# File 'lib/cacheql/association_loader.rb', line 22

def cache_key(record)
  record.object_id
end

#load(record) ⇒ Object

Raises:

  • (TypeError)


15
16
17
18
19
# File 'lib/cacheql/association_loader.rb', line 15

def load(record)
  raise TypeError, "#{@model} loader can't load association for #{record.class}" unless record.is_a?(@model)
  return Promise.resolve(read_association(record)) if association_loaded?(record)
  super
end

#perform(records) ⇒ Object



26
27
28
29
# File 'lib/cacheql/association_loader.rb', line 26

def perform(records)
  preload_association(records)
  records.each { |record| fulfill(record, read_association(record)) }
end