Class: AmsLazyRelationships::Loaders::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ams_lazy_relationships/loaders/base.rb

Overview

A base class for all the loaders. A correctly defined loader requires the ‘load_data` and `batch_key` methods.

Direct Known Subclasses

Association, Direct, SimpleBelongsTo, SimpleHasMany

Instance Method Summary collapse

Instance Method Details

#load(record, &block) ⇒ Object

Lazy loads and yields the data when evaluating

Parameters:

  • record (Object)

    an object for which we’re loading the data

  • block (Proc)

    a block to execute when data is evaluated. Loaded data is yielded as a block argument.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ams_lazy_relationships/loaders/base.rb', line 12

def load(record, &block)
  BatchLoader.for(record).batch(
    key: batch_key(record),
    # Replacing methods can be costly, especially on objects with lots
    # of methods (like AR methods). Let's disable it.
    # More info:
    # https://github.com/exAspArk/batch-loader/tree/v1.4.1#replacing-methods
    replace_methods: false
  ) do |records, loader|
    data = load_data(records, loader)

    block&.call(data)
  end
end