Module: ActiveRecord::Collections::Batching

Included in:
ActiveRecord::Collection
Defined in:
lib/active_record/collections/batching.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/active_record/collections/batching.rb', line 4

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#as_batchObject



104
105
106
# File 'lib/active_record/collections/batching.rb', line 104

def as_batch
  dup.is_batch!
end

#as_batches {|batches.first| ... } ⇒ Object Also known as: in_batches

Yields:

  • (batches.first)


122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record/collections/batching.rb', line 122

def as_batches(&block)
  total_count # init count once before duping
  batched = dup.batch!
  batches = [batched.first_batch!.as_batch]
  yield batches.first if block_given?
  while batched.next_batch? do
    b = batched.as_next_batch
    yield b if block_given?
    batches << b
  end
  batches
end

#as_next_batchObject



108
109
110
# File 'lib/active_record/collections/batching.rb', line 108

def as_next_batch
  next_batch!.as_batch
end

#batch(batch_size: nil) ⇒ Object



60
61
62
# File 'lib/active_record/collections/batching.rb', line 60

def batch(batch_size: nil)
  dup.batch!(batch_size: batch_size)
end

#batch!(batch_size: nil) ⇒ Object



64
65
66
# File 'lib/active_record/collections/batching.rb', line 64

def batch!(batch_size: nil)
  batchify!(1, (batch_size || limit_value || default_batch_size))
end

#batch_by_default?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/active_record/collections/batching.rb', line 44

def batch_by_default?
  self.class.batch_by_default? ||
  ( batching_threshold > 0 &&
    total_count >= batching_threshold )
end

#batch_map(&block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/active_record/collections/batching.rb', line 153

def batch_map(&block)
  if total_batches <= 1
    return (block_given? ? yield(dup.as_batch) : dup.as_batch)
  end

  batched = []
  first_batch!
  total_batches.times do
    batched << (block_given? ? yield(dup.as_batch) : dup.as_batch)
    next_batch!
  end
  first_batch!
  batched
end

#batch_sizeObject



90
91
92
# File 'lib/active_record/collections/batching.rb', line 90

def batch_size
  limit_value || total_count
end

#batchify!(btch, bs) ⇒ Object



76
77
78
79
80
# File 'lib/active_record/collections/batching.rb', line 76

def batchify!(btch, bs)
  @is_batched = true
  limit!(bs.to_i)
  offset!((btch.to_i - 1) * bs.to_i)
end

#batching_thresholdObject



40
41
42
# File 'lib/active_record/collections/batching.rb', line 40

def batching_threshold
  self.class.batching_threshold
end

#current_batchObject



86
87
88
# File 'lib/active_record/collections/batching.rb', line 86

def current_batch
  (offset_value.to_i / (limit_value || 1)) + 1
end

#default_batch_sizeObject



36
37
38
# File 'lib/active_record/collections/batching.rb', line 36

def default_batch_size
  self.class.default_batch_size
end

#each_batch(&block) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/active_record/collections/batching.rb', line 136

def each_batch(&block)
  if total_batches <= 1
    yield dup.as_batch if block_given?
    return [dup.as_batch]
  end

  batched = []
  first_batch!
  total_batches.times do
    batched << dup.as_batch
    yield dup.as_batch if block_given?
    next_batch!
  end
  first_batch!
  batched
end

#first_batchObject



172
173
174
# File 'lib/active_record/collections/batching.rb', line 172

def first_batch
  dup.first_batch!.is_batch!
end

#first_batch!Object



176
177
178
# File 'lib/active_record/collections/batching.rb', line 176

def first_batch!
  batchify!(1, batch_size)
end

#flat_batch_map(&block) ⇒ Object



168
169
170
# File 'lib/active_record/collections/batching.rb', line 168

def flat_batch_map(&block)
  batch_map(&block).flatten
end

#is_batch!Object



94
95
96
97
# File 'lib/active_record/collections/batching.rb', line 94

def is_batch!
  @is_batch = true
  self
end

#is_batch?Boolean Also known as: batch?

Returns:

  • (Boolean)


99
100
101
# File 'lib/active_record/collections/batching.rb', line 99

def is_batch?
  @is_batch || false
end

#is_batched?Boolean Also known as: batched?

Returns:

  • (Boolean)


55
56
57
# File 'lib/active_record/collections/batching.rb', line 55

def is_batched?
  @is_batched || false
end

#last_batchObject



204
205
206
# File 'lib/active_record/collections/batching.rb', line 204

def last_batch
  dup.last_batch!.is_batch!
end

#last_batch!Object



208
209
210
# File 'lib/active_record/collections/batching.rb', line 208

def last_batch!
  batchify!(total_batches, batch_size)
end

#next_batchObject



184
185
186
# File 'lib/active_record/collections/batching.rb', line 184

def next_batch
  dup.next_batch!.is_batch!
end

#next_batch!Object



188
189
190
# File 'lib/active_record/collections/batching.rb', line 188

def next_batch!
  batchify!(current_batch + 1, batch_size)
end

#next_batch?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/active_record/collections/batching.rb', line 180

def next_batch?
  current_batch < total_batches
end

#per_batch(bs) ⇒ Object



68
69
70
# File 'lib/active_record/collections/batching.rb', line 68

def per_batch(bs)
  dup.per_batch!(bs)
end

#per_batch!(bs) ⇒ Object



72
73
74
# File 'lib/active_record/collections/batching.rb', line 72

def per_batch!(bs)
  batchify!(current_batch, bs)
end

#prev_batchObject



196
197
198
# File 'lib/active_record/collections/batching.rb', line 196

def prev_batch
  dup.prev_batch!.is_batch!
end

#prev_batch!Object



200
201
202
# File 'lib/active_record/collections/batching.rb', line 200

def prev_batch!
  batchify!(current_batch - 1, batch_size)
end

#prev_batch?Boolean

Returns:

  • (Boolean)


192
193
194
# File 'lib/active_record/collections/batching.rb', line 192

def prev_batch?
  current_batch > 1
end

#should_batch?Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/active_record/collections/batching.rb', line 50

def should_batch?
  return false if is_batch?
  batch_by_default?
end

#to_batchesObject



112
113
114
115
116
117
118
119
120
# File 'lib/active_record/collections/batching.rb', line 112

def to_batches
  total_count # init count once before duping
  batched = dup.batch!
  batches = [batched.first_batch!.as_batch]
  while batched.next_batch? do
    batches << batched.as_next_batch
  end
  batches
end

#total_batchesObject



82
83
84
# File 'lib/active_record/collections/batching.rb', line 82

def total_batches
  total_count == 0 ? 0 : (total_count.to_f / batch_size.to_f).ceil
end