activerecord-collections 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/active_record/collection.rb +3 -2
- data/lib/active_record/collections/batching.rb +66 -89
- data/lib/active_record/collections/delegation.rb +8 -3
- data/lib/active_record/collections/records.rb +3 -3
- data/lib/active_record/collections/relation.rb +18 -5
- data/lib/active_record/collections/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0d6b4baa140c325ec88e2c55175e2bae0d55566c
|
4
|
+
data.tar.gz: 3a463c83e5790a296d570eabedaa1d30bde7085e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ce87631a746258bb7ae4fbeead8deec3c55c547b04fcc1c668314fbf1db11e9cfd81432b4a2ae09932269351ae4ce76c6374ac531b13c300a7a906006dad098
|
7
|
+
data.tar.gz: 285370a57bbff5c61496987e14417c835b748c8c535ee8f26bfe644c06f092f6525d04cb98821168ead1ff8f19cef607049109c0a03efbdba39c81dab280bb76
|
@@ -76,10 +76,11 @@ module ActiveRecord
|
|
76
76
|
end
|
77
77
|
|
78
78
|
def initialize_copy(old)
|
79
|
+
@collectable = old.collectable
|
79
80
|
@options = old.options.dup
|
80
81
|
@records = @relation = old.relation.dup
|
81
|
-
@total_records = old.
|
82
|
-
|
82
|
+
@total_records = old.instance_variable_get(:@total_records)
|
83
|
+
batch!(batch: old.current_batch, batch_size: old.batch_size) if old.is_batch? || old.batched?(false)
|
83
84
|
is_batch! if old.is_batch?
|
84
85
|
end
|
85
86
|
end
|
@@ -24,15 +24,13 @@ module ActiveRecord
|
|
24
24
|
@batch_by_default || false
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
28
|
-
new.
|
27
|
+
def batch(*num)
|
28
|
+
new.batch(*num)
|
29
29
|
end
|
30
|
-
alias_method :batch, :page
|
31
30
|
|
32
|
-
def
|
33
|
-
new.
|
31
|
+
def batch_size(num)
|
32
|
+
new.batch_size(num)
|
34
33
|
end
|
35
|
-
alias_method :batch_size, :per
|
36
34
|
end
|
37
35
|
|
38
36
|
def default_batch_size
|
@@ -70,7 +68,7 @@ module ActiveRecord
|
|
70
68
|
end
|
71
69
|
|
72
70
|
def as_next_batch
|
73
|
-
|
71
|
+
next_batch!.as_batch
|
74
72
|
end
|
75
73
|
|
76
74
|
def to_batches
|
@@ -97,37 +95,33 @@ module ActiveRecord
|
|
97
95
|
end
|
98
96
|
alias_method :in_batches, :as_batches
|
99
97
|
|
100
|
-
|
101
|
-
|
102
|
-
dup.page!(*num)
|
98
|
+
def batch(batch: 1, batch_size: nil)
|
99
|
+
dup.batch!(batch: batch, batch_size: batch_size)
|
103
100
|
end
|
104
|
-
alias_method :batch, :page
|
105
101
|
|
106
|
-
def
|
102
|
+
def batch!(batch: 1, batch_size: nil)
|
107
103
|
reset!(false, false)
|
108
|
-
@
|
109
|
-
@
|
110
|
-
@
|
104
|
+
@current_batch = batch
|
105
|
+
@batch_size = batch_size unless batch_size.nil?
|
106
|
+
@batch_size ||= default_batch_size
|
107
|
+
@relation = relation.limit(@batch_size).offset((@current_batch - 1) * @batch_size)
|
111
108
|
self
|
112
109
|
end
|
113
|
-
alias_method :batch!, :page!
|
114
110
|
|
115
|
-
def
|
116
|
-
dup.
|
111
|
+
def per_batch(num=nil)
|
112
|
+
dup.per_batch!(num)
|
117
113
|
end
|
118
|
-
alias_method :batch_size, :per
|
119
114
|
|
120
|
-
def
|
115
|
+
def per_batch!(num=nil)
|
121
116
|
reset!(false, false)
|
122
|
-
@
|
123
|
-
@
|
124
|
-
@relation = relation.
|
117
|
+
@current_batch ||= 1
|
118
|
+
@batch_size = num || default_batch_size
|
119
|
+
@relation = relation.limit(@batch_size).offset((@current_batch - 1) * @batch_size)
|
125
120
|
self
|
126
121
|
end
|
127
|
-
alias_method :batch_size!, :per!
|
128
122
|
|
129
|
-
def
|
130
|
-
return true if !(@
|
123
|
+
def batched?(check_if_should=false)
|
124
|
+
return true if !(@current_batch.nil? && @batch_size.nil?)
|
131
125
|
if check_if_should && should_batch?(false)
|
132
126
|
batch!
|
133
127
|
true
|
@@ -135,116 +129,99 @@ module ActiveRecord
|
|
135
129
|
false
|
136
130
|
end
|
137
131
|
end
|
138
|
-
alias_method :batched?, :paginated?
|
139
132
|
|
140
|
-
def
|
141
|
-
@
|
133
|
+
def current_batch
|
134
|
+
@current_batch || 1
|
142
135
|
end
|
143
|
-
alias_method :current_batch, :current_page
|
144
136
|
|
145
|
-
def
|
146
|
-
@
|
137
|
+
def batch_size
|
138
|
+
@batch_size || total_count
|
147
139
|
end
|
148
|
-
alias_method :per_batch, :per_page
|
149
140
|
|
150
|
-
def
|
141
|
+
def total_batches
|
151
142
|
return 1 if is_batch?
|
152
|
-
(total_count.to_f /
|
143
|
+
(total_count.to_f / batch_size.to_f).ceil
|
153
144
|
end
|
154
|
-
alias_method :total_batches, :total_pages
|
155
145
|
|
156
|
-
def
|
146
|
+
def each_batch(&block)
|
157
147
|
batch! if should_batch?
|
158
148
|
|
159
|
-
if
|
149
|
+
if total_batches <= 1
|
160
150
|
yield to_a if block_given?
|
161
151
|
return [to_a]
|
162
152
|
end
|
163
153
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
154
|
+
first_batch!
|
155
|
+
batched = []
|
156
|
+
total_batches.times do
|
157
|
+
batched << to_a
|
168
158
|
yield to_a if block_given?
|
169
|
-
|
159
|
+
next_batch!
|
170
160
|
end
|
171
|
-
|
172
|
-
|
161
|
+
first_batch!
|
162
|
+
batched
|
173
163
|
end
|
174
|
-
alias_method :each_batch, :each_page
|
175
164
|
|
176
|
-
def
|
165
|
+
def batch_map(&block)
|
177
166
|
batch! if should_batch?
|
178
167
|
|
179
|
-
if
|
168
|
+
if total_batches <= 1
|
180
169
|
return (block_given? ? yield(to_a) : to_a)
|
181
170
|
end
|
182
171
|
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
172
|
+
first_batch!
|
173
|
+
batched = []
|
174
|
+
total_batches.times do
|
175
|
+
batched << (block_given? ? yield(to_a) : to_a)
|
176
|
+
next_batch!
|
188
177
|
end
|
189
|
-
|
190
|
-
|
178
|
+
first_batch!
|
179
|
+
batched
|
191
180
|
end
|
192
|
-
alias_method :batch_map, :page_map
|
193
181
|
|
194
|
-
def
|
195
|
-
|
182
|
+
def flat_batch_map(&block)
|
183
|
+
batch_map(&block).flatten
|
196
184
|
end
|
197
|
-
alias_method :flat_batch_map, :flat_page_map
|
198
185
|
|
199
|
-
def
|
200
|
-
dup.
|
186
|
+
def first_batch
|
187
|
+
dup.first_batch!
|
201
188
|
end
|
202
|
-
alias_method :first_batch, :first_page
|
203
189
|
|
204
|
-
def
|
205
|
-
|
190
|
+
def first_batch!
|
191
|
+
batch!(batch: 1)
|
206
192
|
end
|
207
|
-
alias_method :first_batch!, :first_page!
|
208
193
|
|
209
|
-
def
|
210
|
-
|
194
|
+
def next_batch?
|
195
|
+
current_batch < total_batches
|
211
196
|
end
|
212
|
-
alias_method :next_batch?, :next_page?
|
213
197
|
|
214
|
-
def
|
215
|
-
dup.
|
198
|
+
def next_batch
|
199
|
+
dup.next_batch!
|
216
200
|
end
|
217
|
-
alias_method :next_batch, :next_page
|
218
201
|
|
219
|
-
def
|
220
|
-
|
202
|
+
def next_batch!
|
203
|
+
batch!(batch: current_batch + 1) if next_batch?
|
221
204
|
end
|
222
|
-
alias_method :next_batch!, :next_page!
|
223
205
|
|
224
|
-
def
|
225
|
-
|
206
|
+
def prev_batch?
|
207
|
+
current_batch > 1
|
226
208
|
end
|
227
|
-
alias_method :prev_batch?, :prev_page?
|
228
209
|
|
229
|
-
def
|
230
|
-
dup.
|
210
|
+
def prev_batch
|
211
|
+
dup.prev_batch!
|
231
212
|
end
|
232
|
-
alias_method :prev_batch, :prev_page
|
233
213
|
|
234
|
-
def
|
235
|
-
|
214
|
+
def prev_batch!
|
215
|
+
batch!(batch: current_batch - 1) if prev_batch?
|
236
216
|
end
|
237
|
-
alias_method :prev_batch!, :prev_page!
|
238
217
|
|
239
|
-
def
|
240
|
-
dup.
|
218
|
+
def last_batch
|
219
|
+
dup.last_batch!
|
241
220
|
end
|
242
|
-
alias_method :last_batch, :last_page
|
243
221
|
|
244
|
-
def
|
245
|
-
|
222
|
+
def last_batch!
|
223
|
+
batch!(batch: total_batches)
|
246
224
|
end
|
247
|
-
alias_method :last_batch!, :last_page!
|
248
225
|
end
|
249
226
|
end
|
250
227
|
end
|
@@ -66,7 +66,7 @@ module ActiveRecord
|
|
66
66
|
protected
|
67
67
|
|
68
68
|
def call_on_records(meth, *args)
|
69
|
-
return
|
69
|
+
return batch_map do |batch|
|
70
70
|
if model.columns.map(&:name).include?(meth.to_s) && !batch.loaded?
|
71
71
|
batch.pluck(meth)
|
72
72
|
else
|
@@ -83,8 +83,13 @@ module ActiveRecord
|
|
83
83
|
|
84
84
|
def call_on_relation(meth, *args)
|
85
85
|
reset!
|
86
|
-
|
87
|
-
|
86
|
+
returned = relation.send(meth, *args)
|
87
|
+
if returned.is_a?(ActiveRecord::Relation)
|
88
|
+
@relation = returned
|
89
|
+
self
|
90
|
+
else
|
91
|
+
returned
|
92
|
+
end
|
88
93
|
end
|
89
94
|
end
|
90
95
|
end
|
@@ -41,7 +41,7 @@ module ActiveRecord
|
|
41
41
|
end
|
42
42
|
|
43
43
|
def each_in_batches(batch_size=nil, &block)
|
44
|
-
|
44
|
+
per_batch!(batch_size)
|
45
45
|
flat_batch_map.each { |record| block_given? ? yield(record) : record }
|
46
46
|
end
|
47
47
|
|
@@ -50,7 +50,7 @@ module ActiveRecord
|
|
50
50
|
end
|
51
51
|
|
52
52
|
def map_in_batches(batch_size=nil, &block)
|
53
|
-
|
53
|
+
per_batch!(batch_size)
|
54
54
|
flat_batch_map.map { |record| block_given? ? yield(record) : record }
|
55
55
|
end
|
56
56
|
|
@@ -59,7 +59,7 @@ module ActiveRecord
|
|
59
59
|
end
|
60
60
|
|
61
61
|
def flat_map_in_batches(batch_size=nil, &block)
|
62
|
-
|
62
|
+
per_batch!(batch_size)
|
63
63
|
flat_batch_map.map { |record| block_given? ? yield(record) : record }
|
64
64
|
end
|
65
65
|
end
|
@@ -40,7 +40,7 @@ module ActiveRecord
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def all
|
43
|
-
reset
|
43
|
+
reset
|
44
44
|
end
|
45
45
|
|
46
46
|
def load
|
@@ -118,6 +118,16 @@ module ActiveRecord
|
|
118
118
|
self
|
119
119
|
end
|
120
120
|
|
121
|
+
def offset(*args, &block)
|
122
|
+
dup.offset!(*args, &block)
|
123
|
+
end
|
124
|
+
|
125
|
+
def offset!(*args, &block)
|
126
|
+
reset!
|
127
|
+
relation.offset!(*args, &block)
|
128
|
+
self
|
129
|
+
end
|
130
|
+
|
121
131
|
def joins(*args)
|
122
132
|
dup.joins!(*args)
|
123
133
|
end
|
@@ -148,15 +158,18 @@ module ActiveRecord
|
|
148
158
|
self
|
149
159
|
end
|
150
160
|
|
151
|
-
def reset(clear_total=true,
|
152
|
-
dup.reset!(clear_total,
|
161
|
+
def reset(clear_total=true, clear_batches=true)
|
162
|
+
dup.reset!(clear_total, clear_batches)
|
153
163
|
end
|
154
164
|
|
155
|
-
def reset!(clear_total=true,
|
165
|
+
def reset!(clear_total=true, clear_batches=true)
|
156
166
|
@records = @record_ids = @size = nil
|
157
|
-
@page = @per = nil if clear_pages
|
158
167
|
@total_records = nil if clear_total
|
159
168
|
relation.reset
|
169
|
+
if clear_batches
|
170
|
+
@current_batch = @batch_size = nil
|
171
|
+
relation.limit!(nil).offset!(nil)
|
172
|
+
end
|
160
173
|
self
|
161
174
|
end
|
162
175
|
end
|