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
- #as_batch ⇒ Object
- #as_batches {|batches.first| ... } ⇒ Object (also: #in_batches)
- #as_next_batch ⇒ Object
- #batch_by_default? ⇒ Boolean
- #batching_threshold ⇒ Object
- #current_page ⇒ Object (also: #current_batch)
- #default_batch_size ⇒ Object
- #each_page(&block) ⇒ Object (also: #each_batch)
- #first_page ⇒ Object (also: #first_batch)
- #first_page! ⇒ Object (also: #first_batch!)
- #flat_page_map(&block) ⇒ Object (also: #flat_batch_map)
- #is_batch! ⇒ Object
- #is_batch? ⇒ Boolean (also: #batch?)
- #last_page ⇒ Object (also: #last_batch)
- #last_page! ⇒ Object (also: #last_batch!)
- #next_page ⇒ Object (also: #next_batch)
- #next_page! ⇒ Object (also: #next_batch!)
- #next_page? ⇒ Boolean (also: #next_batch?)
- #page(*num) ⇒ Object (also: #batch)
- #page!(*num) ⇒ Object (also: #batch!)
- #page_map(&block) ⇒ Object (also: #batch_map)
- #paginated?(check_if_should = false) ⇒ Boolean (also: #batched?)
- #per(num = nil) ⇒ Object (also: #batch_size)
- #per!(num) ⇒ Object (also: #batch_size!)
- #per_page ⇒ Object (also: #per_batch)
- #prev_page ⇒ Object (also: #prev_batch)
- #prev_page! ⇒ Object (also: #prev_batch!)
- #prev_page? ⇒ Boolean (also: #prev_batch?)
- #should_batch?(check_if_batched = true) ⇒ Boolean
- #to_batches ⇒ Object
- #total_pages ⇒ Object (also: #total_batches)
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_batch ⇒ Object
68 69 70 |
# File 'lib/active_record/collections/batching.rb', line 68 def as_batch dup.is_batch! end |
#as_batches {|batches.first| ... } ⇒ Object Also known as: in_batches
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/active_record/collections/batching.rb', line 86 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.next_batch!.as_batch yield b if block_given? batches << b end batches end |
#as_next_batch ⇒ Object
72 73 74 |
# File 'lib/active_record/collections/batching.rb', line 72 def as_next_batch next_page!.as_batch end |
#batch_by_default? ⇒ Boolean
46 47 48 |
# File 'lib/active_record/collections/batching.rb', line 46 def batch_by_default? self.class.batch_by_default? end |
#batching_threshold ⇒ Object
42 43 44 |
# File 'lib/active_record/collections/batching.rb', line 42 def batching_threshold self.class.batching_threshold end |
#current_page ⇒ Object Also known as: current_batch
139 140 141 |
# File 'lib/active_record/collections/batching.rb', line 139 def current_page @page || 1 end |
#default_batch_size ⇒ Object
38 39 40 |
# File 'lib/active_record/collections/batching.rb', line 38 def default_batch_size self.class.default_batch_size end |
#each_page(&block) ⇒ Object Also known as: each_batch
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/active_record/collections/batching.rb', line 155 def each_page(&block) if total_pages <= 1 yield to_a if block_given? return [to_a] end first_page! paged = [] total_pages.times do paged << to_a yield to_a if block_given? next_page! end first_page! paged end |
#first_page ⇒ Object Also known as: first_batch
194 195 196 |
# File 'lib/active_record/collections/batching.rb', line 194 def first_page dup.first_page! end |
#first_page! ⇒ Object Also known as: first_batch!
199 200 201 |
# File 'lib/active_record/collections/batching.rb', line 199 def first_page! page!(1) end |
#flat_page_map(&block) ⇒ Object Also known as: flat_batch_map
189 190 191 |
# File 'lib/active_record/collections/batching.rb', line 189 def flat_page_map(&block) page_map(&block).flatten end |
#is_batch! ⇒ Object
58 59 60 61 |
# File 'lib/active_record/collections/batching.rb', line 58 def is_batch! @is_batch = true self end |
#is_batch? ⇒ Boolean Also known as: batch?
63 64 65 |
# File 'lib/active_record/collections/batching.rb', line 63 def is_batch? @is_batch || false end |
#last_page ⇒ Object Also known as: last_batch
234 235 236 |
# File 'lib/active_record/collections/batching.rb', line 234 def last_page dup.last_page! end |
#last_page! ⇒ Object Also known as: last_batch!
239 240 241 |
# File 'lib/active_record/collections/batching.rb', line 239 def last_page! page!(total_pages) end |
#next_page ⇒ Object Also known as: next_batch
209 210 211 |
# File 'lib/active_record/collections/batching.rb', line 209 def next_page dup.next_page! end |
#next_page! ⇒ Object Also known as: next_batch!
214 215 216 |
# File 'lib/active_record/collections/batching.rb', line 214 def next_page! page!(current_page + 1) if next_page? end |
#next_page? ⇒ Boolean Also known as: next_batch?
204 205 206 |
# File 'lib/active_record/collections/batching.rb', line 204 def next_page? current_page < total_pages end |
#page(*num) ⇒ Object Also known as: batch
100 101 102 |
# File 'lib/active_record/collections/batching.rb', line 100 def page(*num) dup.page!(*num) end |
#page!(*num) ⇒ Object Also known as: batch!
105 106 107 108 109 110 111 |
# File 'lib/active_record/collections/batching.rb', line 105 def page!(*num) reset!(false, false) @page = num[0] || 1 @per ||= default_batch_size @relation = relation.page(@page).per(@per) self end |
#page_map(&block) ⇒ Object Also known as: batch_map
173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/active_record/collections/batching.rb', line 173 def page_map(&block) if total_pages <= 1 return (block_given? ? yield(to_a) : to_a) end first_page! paged = [] total_pages.times do paged << (block_given? ? yield(to_a) : to_a) next_page! end first_page! paged end |
#paginated?(check_if_should = false) ⇒ Boolean Also known as: batched?
128 129 130 131 132 133 134 135 136 |
# File 'lib/active_record/collections/batching.rb', line 128 def paginated?(check_if_should=false) return true if !(@page.nil? && @per.nil?) if check_if_should && should_batch?(false) batch! true else false end end |
#per(num = nil) ⇒ Object Also known as: batch_size
114 115 116 |
# File 'lib/active_record/collections/batching.rb', line 114 def per(num=nil) dup.per!(num) end |
#per!(num) ⇒ Object Also known as: batch_size!
119 120 121 122 123 124 125 |
# File 'lib/active_record/collections/batching.rb', line 119 def per!(num) reset!(false, false) @page ||= 1 @per = num @relation = relation.page(@page).per(@per) self end |
#per_page ⇒ Object Also known as: per_batch
144 145 146 |
# File 'lib/active_record/collections/batching.rb', line 144 def per_page @per || total_count end |
#prev_page ⇒ Object Also known as: prev_batch
224 225 226 |
# File 'lib/active_record/collections/batching.rb', line 224 def prev_page dup.prev_page! end |
#prev_page! ⇒ Object Also known as: prev_batch!
229 230 231 |
# File 'lib/active_record/collections/batching.rb', line 229 def prev_page! page!(current_page - 1) if prev_page? end |
#prev_page? ⇒ Boolean Also known as: prev_batch?
219 220 221 |
# File 'lib/active_record/collections/batching.rb', line 219 def prev_page? current_page > 1 end |
#should_batch?(check_if_batched = true) ⇒ Boolean
50 51 52 53 54 55 56 |
# File 'lib/active_record/collections/batching.rb', line 50 def should_batch?(check_if_batched=true) return false if is_batch? return false if check_if_batched && batched? batch_by_default? || ( batching_threshold > 0 && total_records >= batching_threshold ) end |
#to_batches ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/active_record/collections/batching.rb', line 76 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.next_batch!.as_batch end batches end |
#total_pages ⇒ Object Also known as: total_batches
149 150 151 152 |
# File 'lib/active_record/collections/batching.rb', line 149 def total_pages return 1 if is_batch? (total_count.to_f / per_page.to_f).ceil end |