Sha256: f8ba01f9ade9a0bbed249b9264b2da42f09c7e300161d4f08d9f09cbdca7d259

Contents?: true

Size: 721 Bytes

Versions: 13

Compression:

Stored size: 721 Bytes

Contents

require 'n1_loader'

require_relative 'context/service'

# Loader that will be shared between multiple classes
class SharedLoader < N1Loader::Loader
  def perform(objects)
    data = Service.receive(objects)

    objects.each_with_index do |user, index|
      fulfill(user, data[index])
    end
  end
end

class User
  include N1Loader::Loadable

  n1_optimized :optimized_call, SharedLoader
end

class Payment
  include N1Loader::Loadable

  n1_optimized :optimized_call, SharedLoader
end

objects = [User.new, Payment.new, User.new, Payment.new]

N1Loader::Preloader.new(objects).preload(:optimized_call)

# First time loading for all objects
objects.map(&:optimized_call)
p "Loaded for all once: #{Service.count == 1}"

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
n1_loader-1.7.2 examples/shared_loader.rb
n1_loader-1.7.1 examples/shared_loader.rb
n1_loader-1.7.0 examples/shared_loader.rb
n1_loader-1.6.6 examples/shared_loader.rb
n1_loader-1.6.5 examples/shared_loader.rb
n1_loader-1.6.4 examples/shared_loader.rb
n1_loader-1.6.3 examples/shared_loader.rb
n1_loader-1.6.2 examples/shared_loader.rb
n1_loader-1.6.1 examples/shared_loader.rb
n1_loader-1.6.0 examples/shared_loader.rb
n1_loader-1.5.1 examples/shared_loader.rb
n1_loader-1.5.0 examples/shared_loader.rb
n1_loader-1.4.4 examples/shared_loader.rb