Sha256: cf5d23d340e6dfe6e1d9112ae2bc9efdeb3950b8ec419dec3ca6cf7671ef1484

Contents?: true

Size: 706 Bytes

Versions: 13

Compression:

Stored size: 706 Bytes

Contents

require 'n1_loader'

require_relative 'context/service'

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

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

  def single(object)
    Service.single(object)
  end
end

class User
  include N1Loader::Loadable

  n1_optimized :optimized_call, OptimizedLoader
end

objects = [User.new, User.new]

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

objects.map(&:optimized_call)
p "Used multi-case perform: #{Service.count == 1}"

User.new.optimized_call
p "Used single-case perform: #{Service.count == 1}"

Version data entries

13 entries across 13 versions & 1 rubygems

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