-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreloading.rb
More file actions
32 lines (23 loc) · 758 Bytes
/
reloading.rb
File metadata and controls
32 lines (23 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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}"