Sha256: 90b45a0ab139d9903075e8d2fbcb2ea2cd18e168b4918b1b6d798f68bc3dccb9

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

# Raised when a single object without ArLazyPreload context was passed to an isolated loader.
N1Loader::Loader::MissingArLazyPreloadContext = Class.new(StandardError)

# Defines a singleton method method that allows isolated loaders
# to use ArLazyPreload context without passing sibling records.
N1Loader::Loader.define_singleton_method(:for) do |element, **args|
  # It is required to have an ArLazyPreload context defined
  if !element.respond_to?(:lazy_preload_context) || element.lazy_preload_context.nil?
    raise N1Loader::Loader::MissingArLazyPreloadContext
  end

  # Fetch or initialize loader from ArLazyPreload context
  loader_collection = element.lazy_preload_context.fetch_n1_loader_collection(self) do
    context_setup = lambda { |records|
      N1Loader::ArLazyPreload::AssociatedContextBuilder.prepare(
        parent_context: element.lazy_preload_context,
        association_name: "cached_n1_loader_collection_#{self}".downcase.to_sym,
        records: records
      )
    }

    N1Loader::LoaderCollection.new(self, element.lazy_preload_context.records).tap do |collection|
      collection.context_setup = context_setup
    end
  end

  # Fetch value from loader
  loader_collection.with(**args).for(element)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
n1_loader-1.6.0 lib/n1_loader/ar_lazy_preload/loader.rb