Module: Philiprehberger::Pagination
- Defined in:
- lib/philiprehberger/pagination.rb,
lib/philiprehberger/pagination/page.rb,
lib/philiprehberger/pagination/version.rb
Defined Under Namespace
Classes: Error, InvalidCursorError, InvalidPageSizeError, Page
Constant Summary collapse
- DEFAULT_PER_PAGE =
25- VERSION =
'0.3.1'
Class Method Summary collapse
-
.paginate(collection, strategy: :offset, per_page: DEFAULT_PER_PAGE, cursor: nil, page: nil, max_per_page: nil, min_per_page: nil, secret: nil) ⇒ Page
Paginate a collection using the specified strategy.
Class Method Details
.paginate(collection, strategy: :offset, per_page: DEFAULT_PER_PAGE, cursor: nil, page: nil, max_per_page: nil, min_per_page: nil, secret: nil) ⇒ Page
Paginate a collection using the specified strategy.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/philiprehberger/pagination.rb', line 30 def self.paginate(collection, strategy: :offset, per_page: DEFAULT_PER_PAGE, cursor: nil, page: nil, max_per_page: nil, min_per_page: nil, secret: nil) per_page = per_page.to_i validate_page_size!(per_page, min_per_page: min_per_page, max_per_page: max_per_page) per_page = [per_page, 1].max items = collection.to_a case strategy.to_sym when :offset paginate_offset(items, per_page: per_page, page: page) when :cursor paginate_cursor(items, per_page: per_page, cursor: cursor, secret: secret) when :keyset paginate_keyset(items, per_page: per_page, cursor: cursor, secret: secret) else raise Error, "unknown strategy: #{strategy}" end end |