Class: Bulker::Pager
- Inherits:
-
Object
- Object
- Bulker::Pager
- Defined in:
- lib/bulker/pager.rb
Overview
Bulker::Pager provides paging functionality to split large number of data to devided segments. It can handle large data very easily such like array with each method.
Bulker::Pager is especially designed for ActiveRecord.
class Item < ActiveRecord::Base; end
Bulker::Pager.each(Item.where(:status => "selling"), 100) do
# do some process for each item
end
For other data source, Bulker::Criteria provides interface for Bulker::Pager.
Class Method Summary collapse
-
.each(cursor, limit, &block) ⇒ Object
access to every
cursordata with fetch limitationlimit.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
access to every data.
-
#initialize(cursor, limit) ⇒ Pager
constructor
create pager from
cursordata and limitaionlimit.
Constructor Details
#initialize(cursor, limit) ⇒ Pager
create pager from cursor data and limitaion limit
23 24 25 26 27 |
# File 'lib/bulker/pager.rb', line 23 def initialize(cursor, limit) raise RangeError.new("limit must be positive number, but #{limit}") if limit <= 0 @cursor = cursor @limit = limit end |
Class Method Details
.each(cursor, limit, &block) ⇒ Object
access to every cursor data with fetch limitation limit.
16 17 18 19 |
# File 'lib/bulker/pager.rb', line 16 def each(cursor, limit, &block) pager = Pager.new cursor, limit pager.each { |page| block.call(page) } end |
Instance Method Details
#each(&block) ⇒ Object
access to every data.
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/bulker/pager.rb', line 30 def each(&block) size = @cursor.count pages = size / @limit + (size % @limit == 0 ? 0 : 1) pages.times do |i| offset = i * @limit @cursor.offset(offset).limit(@limit).each do |row| yield row end end end |