Class: Philiprehberger::Pagination::Page
- Inherits:
-
Object
- Object
- Philiprehberger::Pagination::Page
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/pagination/page.rb
Overview
Represents a page of results from pagination.
Instance Attribute Summary collapse
-
#current_page ⇒ Integer?
readonly
current page number (offset only).
-
#items ⇒ Array
readonly
the items on this page.
-
#next_cursor ⇒ String?
readonly
cursor for the next page.
-
#offset ⇒ Integer?
readonly
current offset (offset only).
-
#per_page ⇒ Integer?
readonly
items per page.
-
#prev_cursor ⇒ String?
readonly
cursor for the previous page.
-
#total ⇒ Integer?
readonly
total number of items.
Instance Method Summary collapse
-
#each {|item| ... } ⇒ Enumerator
Iterate over items on this page.
-
#empty? ⇒ Boolean
Whether this page has no items.
-
#first_page? ⇒ Boolean
Whether this is the first page.
-
#has_next? ⇒ Boolean
Whether there is a next page.
-
#has_prev? ⇒ Boolean
Whether there is a previous page.
-
#initialize(items:, total: nil, next_cursor: nil, prev_cursor: nil, per_page: nil, current_page: nil, offset: nil) ⇒ Page
constructor
Create a new page result.
-
#last_page? ⇒ Boolean
Whether this is the last page.
-
#links ⇒ Hash<Symbol, String>
Navigation links as a hash.
-
#metadata ⇒ Hash
Page metadata as a hash.
-
#next_page ⇒ Integer?
Next page number for offset strategy.
-
#page_range ⇒ Range?
Range of all page numbers for offset strategy.
-
#prev_page ⇒ Integer?
Previous page number for offset strategy.
-
#size ⇒ Integer
(also: #length, #count)
Number of items on this page.
-
#to_h ⇒ Hash
Full page representation as a hash suitable for JSON serialization.
-
#total_pages ⇒ Integer?
Total number of pages when both total and per_page are known.
Constructor Details
#initialize(items:, total: nil, next_cursor: nil, prev_cursor: nil, per_page: nil, current_page: nil, offset: nil) ⇒ Page
Create a new page result.
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/philiprehberger/pagination/page.rb', line 28 def initialize(items:, total: nil, next_cursor: nil, prev_cursor: nil, per_page: nil, current_page: nil, offset: nil) @items = items @total = total @next_cursor = next_cursor @prev_cursor = prev_cursor @per_page = per_page @current_page = current_page @offset = offset end |
Instance Attribute Details
#current_page ⇒ Integer? (readonly)
current page number (offset only)
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def current_page @current_page end |
#items ⇒ Array (readonly)
the items on this page
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def items @items end |
#next_cursor ⇒ String? (readonly)
cursor for the next page
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def next_cursor @next_cursor end |
#offset ⇒ Integer? (readonly)
current offset (offset only)
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def offset @offset end |
#per_page ⇒ Integer? (readonly)
items per page
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def per_page @per_page end |
#prev_cursor ⇒ String? (readonly)
cursor for the previous page
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def prev_cursor @prev_cursor end |
#total ⇒ Integer? (readonly)
total number of items
14 15 16 |
# File 'lib/philiprehberger/pagination/page.rb', line 14 def total @total end |
Instance Method Details
#each {|item| ... } ⇒ Enumerator
Iterate over items on this page.
131 132 133 134 135 136 |
# File 'lib/philiprehberger/pagination/page.rb', line 131 def each(&block) return @items.each unless block @items.each(&block) self end |
#empty? ⇒ Boolean
Whether this page has no items.
123 124 125 |
# File 'lib/philiprehberger/pagination/page.rb', line 123 def empty? @items.empty? end |
#first_page? ⇒ Boolean
Whether this is the first page.
For offset strategy, returns true when current_page is 1. For cursor/keyset strategies, returns true when there is no previous cursor.
59 60 61 62 63 |
# File 'lib/philiprehberger/pagination/page.rb', line 59 def first_page? return @current_page == 1 if @current_page !has_prev? end |
#has_next? ⇒ Boolean
Whether there is a next page.
42 43 44 |
# File 'lib/philiprehberger/pagination/page.rb', line 42 def has_next? !@next_cursor.nil? end |
#has_prev? ⇒ Boolean
Whether there is a previous page.
49 50 51 |
# File 'lib/philiprehberger/pagination/page.rb', line 49 def has_prev? !@prev_cursor.nil? end |
#last_page? ⇒ Boolean
Whether this is the last page.
Returns true when there is no next cursor.
70 71 72 |
# File 'lib/philiprehberger/pagination/page.rb', line 70 def last_page? !has_next? end |
#links ⇒ Hash<Symbol, String>
Navigation links as a hash.
141 142 143 144 145 146 |
# File 'lib/philiprehberger/pagination/page.rb', line 141 def links result = {} result[:next] = @next_cursor if @next_cursor result[:prev] = @prev_cursor if @prev_cursor result end |
#metadata ⇒ Hash
Page metadata as a hash.
151 152 153 154 155 156 157 158 159 |
# File 'lib/philiprehberger/pagination/page.rb', line 151 def { current_page: @current_page, per_page: @per_page, total_pages: total_pages, total_count: @total, offset: @offset } end |
#next_page ⇒ Integer?
Next page number for offset strategy.
86 87 88 89 90 |
# File 'lib/philiprehberger/pagination/page.rb', line 86 def next_page return nil unless @current_page && has_next? @current_page + 1 end |
#page_range ⇒ Range?
Range of all page numbers for offset strategy.
104 105 106 107 108 109 |
# File 'lib/philiprehberger/pagination/page.rb', line 104 def page_range pages = total_pages return nil unless pages 1..pages end |
#prev_page ⇒ Integer?
Previous page number for offset strategy.
95 96 97 98 99 |
# File 'lib/philiprehberger/pagination/page.rb', line 95 def prev_page return nil unless @current_page && has_prev? @current_page - 1 end |
#size ⇒ Integer Also known as: length, count
Number of items on this page.
114 115 116 |
# File 'lib/philiprehberger/pagination/page.rb', line 114 def size @items.length end |
#to_h ⇒ Hash
Full page representation as a hash suitable for JSON serialization.
Combines items, metadata, and navigation links in a single hash.
166 167 168 169 170 171 172 |
# File 'lib/philiprehberger/pagination/page.rb', line 166 def to_h { items: @items, metadata: , links: links } end |
#total_pages ⇒ Integer?
Total number of pages when both total and per_page are known.
77 78 79 80 81 |
# File 'lib/philiprehberger/pagination/page.rb', line 77 def total_pages return nil unless @total && @per_page&.positive? (@total / @per_page.to_f).ceil end |