Class: ActiveShopifyGraphQL::Response::PaginatedResult
- Inherits:
-
Object
- Object
- ActiveShopifyGraphQL::Response::PaginatedResult
- Includes:
- Enumerable
- Defined in:
- lib/active_shopify_graphql/response/paginated_result.rb
Overview
Represents a page of results from a paginated GraphQL query. Lazily builds model instances from attribute hashes on access. Provides methods to navigate between pages and access pagination metadata.
Instance Attribute Summary collapse
-
#page_info ⇒ Object
readonly
Returns the value of attribute page_info.
-
#query_scope ⇒ Object
readonly
Returns the value of attribute query_scope.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Access records by index.
-
#all_records ⇒ Array
Return all records across all pages Warning: This will make multiple API calls if there are many pages.
-
#each(&block) ⇒ Object
Iterate over records in this page.
-
#empty? ⇒ Boolean
Check if this page has records.
-
#end_cursor ⇒ Object
Cursor pointing to the end of this page.
-
#has_next_page? ⇒ Boolean
Check if there is a next page.
-
#has_previous_page? ⇒ Boolean
Check if there is a previous page.
-
#initialize(attributes:, model_class:, page_info:, query_scope:) ⇒ PaginatedResult
constructor
A new instance of PaginatedResult.
-
#next_page ⇒ PaginatedResult?
Fetch the next page of results.
-
#previous_page ⇒ PaginatedResult?
Fetch the previous page of results.
-
#records ⇒ Object
Get the records for this page (builds instances on first access).
-
#size ⇒ Object
(also: #length)
Number of records in this page.
-
#start_cursor ⇒ Object
Cursor pointing to the start of this page.
-
#to_a ⇒ Object
Convert to array (useful for compatibility).
Constructor Details
#initialize(attributes:, model_class:, page_info:, query_scope:) ⇒ PaginatedResult
Returns a new instance of PaginatedResult.
23 24 25 26 27 28 29 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 23 def initialize(attributes:, model_class:, page_info:, query_scope:) @attributes = attributes @model_class = model_class @page_info = page_info @query_scope = query_scope @records = nil # Lazily built end |
Instance Attribute Details
#page_info ⇒ Object (readonly)
Returns the value of attribute page_info.
21 22 23 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 21 def page_info @page_info end |
#query_scope ⇒ Object (readonly)
Returns the value of attribute query_scope.
21 22 23 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 21 def query_scope @query_scope end |
Instance Method Details
#[](index) ⇒ Object
Access records by index
42 43 44 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 42 def [](index) records[index] end |
#all_records ⇒ Array
Return all records across all pages Warning: This will make multiple API calls if there are many pages
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 101 def all_records all = records.dup current = self while current.has_next_page? current = current.next_page all.concat(current.records) end all end |
#each(&block) ⇒ Object
Iterate over records in this page
37 38 39 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 37 def each(&block) records.each(&block) end |
#empty? ⇒ Boolean
Check if this page has records
53 54 55 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 53 def empty? @attributes.empty? end |
#end_cursor ⇒ Object
Cursor pointing to the end of this page
73 74 75 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 73 def end_cursor @page_info.end_cursor end |
#has_next_page? ⇒ Boolean
Check if there is a next page
58 59 60 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 58 def has_next_page? @page_info.has_next_page? end |
#has_previous_page? ⇒ Boolean
Check if there is a previous page
63 64 65 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 63 def has_previous_page? @page_info.has_previous_page? end |
#next_page ⇒ PaginatedResult?
Fetch the next page of results
79 80 81 82 83 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 79 def next_page return nil unless has_next_page? @query_scope.fetch_page(after: end_cursor) end |
#previous_page ⇒ PaginatedResult?
Fetch the previous page of results
87 88 89 90 91 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 87 def previous_page return nil unless has_previous_page? @query_scope.fetch_page(before: start_cursor) end |
#records ⇒ Object
Get the records for this page (builds instances on first access)
32 33 34 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 32 def records @records ||= ModelBuilder.build_many(@model_class, @attributes) end |
#size ⇒ Object Also known as: length
Number of records in this page
47 48 49 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 47 def size @attributes.size end |
#start_cursor ⇒ Object
Cursor pointing to the start of this page
68 69 70 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 68 def start_cursor @page_info.start_cursor end |
#to_a ⇒ Object
Convert to array (useful for compatibility)
94 95 96 |
# File 'lib/active_shopify_graphql/response/paginated_result.rb', line 94 def to_a records.dup end |