Class: Philiprehberger::Pagination::Page

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/philiprehberger/pagination/page.rb

Overview

Represents a page of results from pagination.

Instance Attribute Summary collapse

Instance Method Summary collapse

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.

Parameters:

  • items (Array)

    the items on this page

  • total (Integer, nil) (defaults to: nil)

    total number of items

  • next_cursor (String, nil) (defaults to: nil)

    cursor for the next page

  • prev_cursor (String, nil) (defaults to: nil)

    cursor for the previous page

  • per_page (Integer, nil) (defaults to: nil)

    items per page

  • current_page (Integer, nil) (defaults to: nil)

    current page number

  • offset (Integer, nil) (defaults to: nil)

    current offset



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_pageInteger? (readonly)

current page number (offset only)

Returns:

  • (Integer, nil)

    the current value of current_page



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def current_page
  @current_page
end

#itemsArray (readonly)

the items on this page

Returns:

  • (Array)

    the current value of items



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def items
  @items
end

#next_cursorString? (readonly)

cursor for the next page

Returns:

  • (String, nil)

    the current value of next_cursor



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def next_cursor
  @next_cursor
end

#offsetInteger? (readonly)

current offset (offset only)

Returns:

  • (Integer, nil)

    the current value of offset



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def offset
  @offset
end

#per_pageInteger? (readonly)

items per page

Returns:

  • (Integer, nil)

    the current value of per_page



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def per_page
  @per_page
end

#prev_cursorString? (readonly)

cursor for the previous page

Returns:

  • (String, nil)

    the current value of prev_cursor



14
15
16
# File 'lib/philiprehberger/pagination/page.rb', line 14

def prev_cursor
  @prev_cursor
end

#totalInteger? (readonly)

total number of items

Returns:

  • (Integer, nil)

    the current value of total



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.

Yields:

  • (item)

    each item on the page

Returns:

  • (Enumerator)

    if no block given



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


70
71
72
# File 'lib/philiprehberger/pagination/page.rb', line 70

def last_page?
  !has_next?
end

Navigation links as a hash.

Returns:

  • (Hash<Symbol, String>)

    links with :next and :prev keys



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

#metadataHash

Page metadata as a hash.

Returns:

  • (Hash)

    metadata including current_page, per_page, total_pages, total_count, offset



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_pageInteger?

Next page number for offset strategy.

Returns:

  • (Integer, nil)

    the next page number or nil if none



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_rangeRange?

Range of all page numbers for offset strategy.

Returns:

  • (Range, nil)

    1..total_pages, or nil if total_pages is unknown



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_pageInteger?

Previous page number for offset strategy.

Returns:

  • (Integer, nil)

    the previous page number or nil if none



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

#sizeInteger Also known as: length, count

Number of items on this page.

Returns:

  • (Integer)


114
115
116
# File 'lib/philiprehberger/pagination/page.rb', line 114

def size
  @items.length
end

#to_hHash

Full page representation as a hash suitable for JSON serialization.

Combines items, metadata, and navigation links in a single hash.

Returns:

  • (Hash)

    hash with :items, :metadata, and :links keys



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_pagesInteger?

Total number of pages when both total and per_page are known.

Returns:

  • (Integer, nil)

    total page count or nil if indeterminate



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