Class: PetstoreApiClient::PaginatedCollection
- Inherits:
-
Object
- Object
- PetstoreApiClient::PaginatedCollection
- Includes:
- Enumerable
- Defined in:
- lib/petstore_api_client/paginated_collection.rb
Overview
Wrapper for paginated API responses Provides metadata about pagination along with the data
This follows the Decorator pattern - it wraps an array with additional pagination information
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_count ⇒ Object
readonly
Returns the value of attribute total_count.
Instance Method Summary collapse
-
#any? ⇒ Boolean
Check if collection has any items.
-
#count ⇒ Object
(also: #size, #length)
Number of items in current page.
-
#each ⇒ Object
Delegate enumerable methods to data array.
-
#empty? ⇒ Boolean
Check if collection is empty.
-
#first_page? ⇒ Boolean
Check if this is the first page.
-
#initialize(data:, page: 1, per_page: 25, total_count: nil) ⇒ PaginatedCollection
constructor
Initialize a paginated collection.
-
#inspect ⇒ Object
Inspect override for better debugging.
-
#last_page? ⇒ Boolean
Check if this is the last page Returns nil if total_count is unknown rubocop:disable Style/ReturnNilInPredicateMethodDefinition.
-
#next_page ⇒ Object
Get next page number (nil if on last page or unknown).
-
#next_page? ⇒ Boolean
Check if there are more pages available Returns true if we have total_count and current page isn’t the last Returns nil if total_count is unknown (can’t determine) rubocop:disable Style/ReturnNilInPredicateMethodDefinition.
-
#offset ⇒ Object
Get offset for current page (0-indexed).
-
#pagination_info ⇒ Object
Summary information about pagination.
-
#prev_page ⇒ Object
(also: #previous_page)
Get previous page number (nil if on first page).
-
#prev_page? ⇒ Boolean
(also: #previous_page?)
Check if there’s a previous page.
-
#to_a ⇒ Object
(also: #to_ary)
Convert to array (returns the data).
-
#total_pages ⇒ Object
Calculate total number of pages Returns nil if total_count is unknown.
Constructor Details
#initialize(data:, page: 1, per_page: 25, total_count: nil) ⇒ PaginatedCollection
Initialize a paginated collection
20 21 22 23 24 25 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 20 def initialize(data:, page: 1, per_page: 25, total_count: nil) @data = Array(data) @page = page.to_i @per_page = per_page.to_i @total_count = total_count&.to_i end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
12 13 14 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 12 def data @data end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
12 13 14 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 12 def page @page end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
12 13 14 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 12 def per_page @per_page end |
#total_count ⇒ Object (readonly)
Returns the value of attribute total_count.
12 13 14 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 12 def total_count @total_count end |
Instance Method Details
#any? ⇒ Boolean
Check if collection has any items
102 103 104 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 102 def any? !empty? end |
#count ⇒ Object Also known as: size, length
Number of items in current page
33 34 35 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 33 def count data.count end |
#each ⇒ Object
Delegate enumerable methods to data array
28 29 30 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 28 def each(&) data.each(&) end |
#empty? ⇒ Boolean
Check if collection is empty
97 98 99 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 97 def empty? data.empty? end |
#first_page? ⇒ Boolean
Check if this is the first page
77 78 79 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 77 def first_page? page == 1 end |
#inspect ⇒ Object
Inspect override for better debugging
128 129 130 131 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 128 def inspect "#<PaginatedCollection page=#{page}/#{total_pages || "?"} " \ "per_page=#{per_page} count=#{count} total=#{total_count || "?"}>" end |
#last_page? ⇒ Boolean
Check if this is the last page Returns nil if total_count is unknown rubocop:disable Style/ReturnNilInPredicateMethodDefinition
84 85 86 87 88 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 84 def last_page? return nil if total_count.nil? page >= total_pages end |
#next_page ⇒ Object
Get next page number (nil if on last page or unknown)
57 58 59 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 57 def next_page next_page? ? page + 1 : nil end |
#next_page? ⇒ Boolean
Check if there are more pages available Returns true if we have total_count and current page isn’t the last Returns nil if total_count is unknown (can’t determine) rubocop:disable Style/ReturnNilInPredicateMethodDefinition
43 44 45 46 47 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 43 def next_page? return nil if total_count.nil? page < total_pages end |
#offset ⇒ Object
Get offset for current page (0-indexed)
92 93 94 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 92 def offset (page - 1) * per_page end |
#pagination_info ⇒ Object
Summary information about pagination
113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 113 def pagination_info { page: page, per_page: per_page, count: count, total_count: total_count, total_pages: total_pages, next_page: next_page, prev_page: prev_page, first_page: first_page?, last_page: last_page? } end |
#prev_page ⇒ Object Also known as: previous_page
Get previous page number (nil if on first page)
62 63 64 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 62 def prev_page prev_page? ? page - 1 : nil end |
#prev_page? ⇒ Boolean Also known as: previous_page?
Check if there’s a previous page
51 52 53 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 51 def prev_page? page > 1 end |
#to_a ⇒ Object Also known as: to_ary
Convert to array (returns the data)
107 108 109 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 107 def to_a data end |
#total_pages ⇒ Object
Calculate total number of pages Returns nil if total_count is unknown
69 70 71 72 73 74 |
# File 'lib/petstore_api_client/paginated_collection.rb', line 69 def total_pages return nil if total_count.nil? return 1 if total_count.zero? (total_count.to_f / per_page).ceil end |