Class: ActiveShopifyGraphQL::Query::Scope
- Inherits:
-
Object
- Object
- ActiveShopifyGraphQL::Query::Scope
- Includes:
- Enumerable
- Defined in:
- lib/active_shopify_graphql/query/scope.rb
Overview
A chainable query builder that accumulates query configuration and executes the query when records are accessed.
Constant Summary collapse
- DEFAULT_PER_PAGE =
250
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#total_limit ⇒ Object
readonly
Returns the value of attribute total_limit.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Array-like access.
-
#count ⇒ Integer
Count records (loads all pages, use with caution).
-
#each {|Object| ... } ⇒ Object
Iterate through all records across all pages.
-
#each_page {|PaginatedResult| ... } ⇒ Object
Iterate through all pages, yielding each page.
-
#empty? ⇒ Boolean
Check if no records exist (Array compatibility).
-
#exists? ⇒ Boolean
Check if any records exist.
-
#fetch_first_page ⇒ PaginatedResult
Fetch the first page of results.
-
#fetch_page(after: nil, before: nil) ⇒ PaginatedResult
Fetch a specific page by cursor.
-
#first(count = nil) ⇒ Object, ...
Get first record.
-
#in_pages(of: DEFAULT_PER_PAGE) {|PaginatedResult| ... } ⇒ PaginatedResult, self
Configure pagination and optionally iterate through pages.
-
#initialize(model_class, conditions: {}, loader: nil, total_limit: nil, per_page: DEFAULT_PER_PAGE) ⇒ Scope
constructor
A new instance of Scope.
-
#limit(count) ⇒ Scope
Set a limit on total records to return.
-
#map(&block) ⇒ Object
Map over records (Array compatibility).
-
#select_records(&block) ⇒ Object
Select/filter records (Array compatibility).
-
#size ⇒ Integer
(also: #length)
Size/length of records (loads all pages, use with caution).
-
#to_a ⇒ Array
(also: #load)
Load all records respecting total_limit.
Constructor Details
#initialize(model_class, conditions: {}, loader: nil, total_limit: nil, per_page: DEFAULT_PER_PAGE) ⇒ Scope
Returns a new instance of Scope.
27 28 29 30 31 32 33 34 35 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 27 def initialize(model_class, conditions: {}, loader: nil, total_limit: nil, per_page: DEFAULT_PER_PAGE) @model_class = model_class @conditions = conditions @loader = loader @total_limit = total_limit @per_page = [per_page, ActiveShopifyGraphQL.configuration.max_objects_per_paginated_query].min @loaded = false @records = nil end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
25 26 27 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 25 def conditions @conditions end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
25 26 27 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 25 def model_class @model_class end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
25 26 27 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 25 def per_page @per_page end |
#total_limit ⇒ Object (readonly)
Returns the value of attribute total_limit.
25 26 27 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 25 def total_limit @total_limit end |
Instance Method Details
#[](index) ⇒ Object
Array-like access
159 160 161 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 159 def [](index) to_a[index] end |
#count ⇒ Integer
Count records (loads all pages, use with caution)
154 155 156 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 154 def count to_a.count end |
#each {|Object| ... } ⇒ Object
Iterate through all records across all pages
97 98 99 100 101 102 103 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 97 def each(&block) return to_enum(:each) unless block_given? each_page do |page| page.each(&block) end end |
#each_page {|PaginatedResult| ... } ⇒ Object
Iterate through all pages, yielding each page
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 62 def each_page current_page = fetch_first_page records_yielded = 0 loop do break if current_page.empty? # Apply total limit if set if @total_limit remaining = @total_limit - records_yielded break if remaining <= 0 if current_page.size > remaining # Trim the page to fit the limit trimmed_records = current_page.records.first(remaining) current_page = Response::PaginatedResult.new( records: trimmed_records, page_info: Response::PageInfo.new, # Empty page info to stop pagination query_scope: self ) end end yield current_page records_yielded += current_page.size break unless current_page.has_next_page? break if @total_limit && records_yielded >= @total_limit current_page = current_page.next_page end end |
#empty? ⇒ Boolean
Check if no records exist (Array compatibility)
141 142 143 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 141 def empty? first(1).empty? end |
#exists? ⇒ Boolean
Check if any records exist
135 136 137 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 135 def exists? first(1).any? end |
#fetch_first_page ⇒ PaginatedResult
Fetch the first page of results
189 190 191 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 189 def fetch_first_page fetch_page end |
#fetch_page(after: nil, before: nil) ⇒ PaginatedResult
Fetch a specific page by cursor
177 178 179 180 181 182 183 184 185 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 177 def fetch_page(after: nil, before: nil) loader.load_paginated_collection( conditions: @conditions, per_page: effective_per_page, after: after, before: before, query_scope: self ) end |
#first(count = nil) ⇒ Object, ...
Get first record
123 124 125 126 127 128 129 130 131 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 123 def first(count = nil) if count scoped = dup_with(total_limit: count, per_page: [count, max_objects_per_paginated_query].min) scoped.to_a else scoped = dup_with(total_limit: 1, per_page: 1) scoped.to_a.first end end |
#in_pages(of: DEFAULT_PER_PAGE) {|PaginatedResult| ... } ⇒ PaginatedResult, self
Configure pagination and optionally iterate through pages
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 48 def in_pages(of: DEFAULT_PER_PAGE, &block) page_size = [of, ActiveShopifyGraphQL.configuration.max_objects_per_paginated_query].min scoped = dup_with(per_page: page_size) if block_given? scoped.each_page(&block) self else scoped.fetch_first_page end end |
#limit(count) ⇒ Scope
Set a limit on total records to return
40 41 42 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 40 def limit(count) dup_with(total_limit: count) end |
#map(&block) ⇒ Object
Map over records (Array compatibility)
164 165 166 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 164 def map(&block) to_a.map(&block) end |
#select_records(&block) ⇒ Object
Select/filter records (Array compatibility)
169 170 171 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 169 def select_records(&block) to_a.select(&block) end |
#size ⇒ Integer Also known as: length
Size/length of records (loads all pages, use with caution)
147 148 149 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 147 def size to_a.size end |
#to_a ⇒ Array Also known as: load
Load all records respecting total_limit
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/active_shopify_graphql/query/scope.rb', line 107 def to_a return @records if @loaded all_records = [] each_page do |page| all_records.concat(page.to_a) end @records = all_records @loaded = true @records end |