Sha256: 8c385857d5558b8ffa8df6238267849a98a4453d6522902fb38450c9e96a3961

Contents?: true

Size: 758 Bytes

Versions: 13

Compression:

Stored size: 758 Bytes

Contents

require 'n1_loader'

require_relative 'context/service'

class User
  include N1Loader::Loadable

  n1_optimized :optimized_call do |users|
    data = Service.receive(users)

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

users = [User.new, User.new, User.new]

# Initialized loader but didn't perform it yet
N1Loader::Preloader.new(users).preload(:optimized_call)
p "No calls yet: #{Service.count == 0}"

# First time loading
users.map(&:optimized_call)
p "First time loaded: #{Service.count == 1}"

users.first.optimized_call(reload: true)
p "Reloaded for this object only: #{Service.count == 2}"

users.first.n1_clear_cache
users.first.optimized_call
p "Reloaded for this object only: #{Service.count == 3}"

Version data entries

13 entries across 13 versions & 1 rubygems

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