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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94403cc736dd576a2be4ca9e465eb22766bf8890
4
- data.tar.gz: 0ebfc8a5da14a0f36fb9a5bfb262725f92ff0bb6
3
+ metadata.gz: 0d6b4baa140c325ec88e2c55175e2bae0d55566c
4
+ data.tar.gz: 3a463c83e5790a296d570eabedaa1d30bde7085e
5
5
  SHA512:
6
- metadata.gz: ab2f87844ce73790ca91a9c74231f3f4adbbfb1bd958f67b5fe2a0ab36e84fef9c1f23f78583c1b4f5a29f3a9dab152ac0f85a142a847299f6cc95bc3514467e
7
- data.tar.gz: f9bf87d4ca6d010c642b77c31383e865438f2b7a0c00d87fb4f8f20f3b81bd8ac2ee405a2c44a96d3e11e410fb720b963828bc02ee6174bd18e0cdbb42277bd7
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.total_records if !old.is_batch? && old.instance_variable_get(:@total_records).to_i > 0
82
- page!(old.current_page).per!(old.per_page) if old.is_batch? || old.batched?(false)
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 page(*num)
28
- new.page(*num)
27
+ def batch(*num)
28
+ new.batch(*num)
29
29
  end
30
- alias_method :batch, :page
31
30
 
32
- def per(num)
33
- new.per(num)
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
- next_page!.as_batch
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
- # TODO Mark need to either depend on kaminari or check for it before using page/per
101
- def page(*num)
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 page!(*num)
102
+ def batch!(batch: 1, batch_size: nil)
107
103
  reset!(false, false)
108
- @page = num[0] || 1
109
- @per ||= default_batch_size
110
- @relation = relation.page(@page).per(@per)
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 per(num=nil)
116
- dup.per!(num)
111
+ def per_batch(num=nil)
112
+ dup.per_batch!(num)
117
113
  end
118
- alias_method :batch_size, :per
119
114
 
120
- def per!(num)
115
+ def per_batch!(num=nil)
121
116
  reset!(false, false)
122
- @page ||= 1
123
- @per = num
124
- @relation = relation.page(@page).per(@per)
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 paginated?(check_if_should=false)
130
- return true if !(@page.nil? && @per.nil?)
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 current_page
141
- @page || 1
133
+ def current_batch
134
+ @current_batch || 1
142
135
  end
143
- alias_method :current_batch, :current_page
144
136
 
145
- def per_page
146
- @per || total_count
137
+ def batch_size
138
+ @batch_size || total_count
147
139
  end
148
- alias_method :per_batch, :per_page
149
140
 
150
- def total_pages
141
+ def total_batches
151
142
  return 1 if is_batch?
152
- (total_count.to_f / per_page.to_f).ceil
143
+ (total_count.to_f / batch_size.to_f).ceil
153
144
  end
154
- alias_method :total_batches, :total_pages
155
145
 
156
- def each_page(&block)
146
+ def each_batch(&block)
157
147
  batch! if should_batch?
158
148
 
159
- if total_pages <= 1
149
+ if total_batches <= 1
160
150
  yield to_a if block_given?
161
151
  return [to_a]
162
152
  end
163
153
 
164
- first_page!
165
- paged = []
166
- total_pages.times do
167
- paged << to_a
154
+ first_batch!
155
+ batched = []
156
+ total_batches.times do
157
+ batched << to_a
168
158
  yield to_a if block_given?
169
- next_page!
159
+ next_batch!
170
160
  end
171
- first_page!
172
- paged
161
+ first_batch!
162
+ batched
173
163
  end
174
- alias_method :each_batch, :each_page
175
164
 
176
- def page_map(&block)
165
+ def batch_map(&block)
177
166
  batch! if should_batch?
178
167
 
179
- if total_pages <= 1
168
+ if total_batches <= 1
180
169
  return (block_given? ? yield(to_a) : to_a)
181
170
  end
182
171
 
183
- first_page!
184
- paged = []
185
- total_pages.times do
186
- paged << (block_given? ? yield(to_a) : to_a)
187
- next_page!
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
- first_page!
190
- paged
178
+ first_batch!
179
+ batched
191
180
  end
192
- alias_method :batch_map, :page_map
193
181
 
194
- def flat_page_map(&block)
195
- page_map(&block).flatten
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 first_page
200
- dup.first_page!
186
+ def first_batch
187
+ dup.first_batch!
201
188
  end
202
- alias_method :first_batch, :first_page
203
189
 
204
- def first_page!
205
- page!(1)
190
+ def first_batch!
191
+ batch!(batch: 1)
206
192
  end
207
- alias_method :first_batch!, :first_page!
208
193
 
209
- def next_page?
210
- current_page < total_pages
194
+ def next_batch?
195
+ current_batch < total_batches
211
196
  end
212
- alias_method :next_batch?, :next_page?
213
197
 
214
- def next_page
215
- dup.next_page!
198
+ def next_batch
199
+ dup.next_batch!
216
200
  end
217
- alias_method :next_batch, :next_page
218
201
 
219
- def next_page!
220
- page!(current_page + 1) if next_page?
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 prev_page?
225
- current_page > 1
206
+ def prev_batch?
207
+ current_batch > 1
226
208
  end
227
- alias_method :prev_batch?, :prev_page?
228
209
 
229
- def prev_page
230
- dup.prev_page!
210
+ def prev_batch
211
+ dup.prev_batch!
231
212
  end
232
- alias_method :prev_batch, :prev_page
233
213
 
234
- def prev_page!
235
- page!(current_page - 1) if prev_page?
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 last_page
240
- dup.last_page!
218
+ def last_batch
219
+ dup.last_batch!
241
220
  end
242
- alias_method :last_batch, :last_page
243
221
 
244
- def last_page!
245
- page!(total_pages)
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 page_map do |batch|
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
- @relation = relation.send(meth, *args)
87
- self
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
- batch_size!(batch_size)
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
- batch_size!(batch_size)
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
- batch_size!(batch_size)
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.limit!(nil)
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, clear_pages=true)
152
- dup.reset!(clear_total, clear_pages)
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, clear_pages=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
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Collections
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-collections
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mark Rebec