-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
batched_methods.rb
37 lines (30 loc) · 1.06 KB
/
batched_methods.rb
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
33
34
35
36
37
# frozen_string_literal: true
require "active_record/batched_methods/batch"
require "active_record/batched_methods/method"
module ActiveRecord::BatchedMethods
extend ActiveSupport::Concern
class_methods do
# Define a batched method. This method will be available on instances
# of this class and return auto-memoized results.
def batch_method(name, batch_size: nil, &block)
batched_methods[name] = Method.new(block, batch_size: batch_size)
define_method(name) do |*args|
batched_method_batch.result_for(name, args, self)
end
end
def batched_methods # :nodoc:
@batched_methods ||= {}
end
end
# Associate this instance with a batch which is will use for batched loading
def batched_method_batch=(batch) # :nodoc:
@batched_method_batch = batch
batch.add(self)
end
private
# Get the current batch, returning a batch of one element if not set
def batched_method_batch
return @batched_method_batch if @batched_method_batch
self.batched_method_batch = Batch.new(self.class)
end
end