Class: Zizq::Resources::ErrorEnumerator
- Inherits:
-
Object
- Object
- Zizq::Resources::ErrorEnumerator
- Includes:
- Enumerable, Enumerable[Zizq::Resources::ErrorRecord]
- Defined in:
- lib/zizq/resources/error_enumerator.rb,
sig/generated/zizq/resources/error_enumerator.rbs
Overview
Provides a lazy Enumerator across all errors on a job following the cursor-based pagination.
Constant Summary collapse
- MAX_PAGE_SIZE =
Maximum page size the server can handle.
2000
Instance Method Summary collapse
-
#any? ⇒ void
Returns true if there are any errors.
-
#each(&block) ⇒ void
Iterate over errors, lazily paginating through results.
-
#each_page(&block) ⇒ void
Iterate over pages of errors.
-
#empty? ⇒ Boolean
Returns true if there are no errors.
-
#first ⇒ Resources::ErrorRecord?
Return the first error, or nil if no errors.
-
#in_pages_of(page_size) ⇒ ErrorEnumerator
Set the page size for paginated iteration.
-
#initialize(id, order: nil, limit: nil, page_size: nil) ⇒ ErrorEnumerator
constructor
Initialize the enumerator.
-
#last ⇒ Resources::ErrorRecord?
Return the last error, or nil if no errors.
-
#limit(limit) ⇒ ErrorEnumerator
Limit the total number of errors returned.
-
#none? ⇒ void
Returns true if there are no errors.
-
#one? ⇒ void
Returns true if there is exactly one error.
-
#order(order) ⇒ ErrorEnumerator
Set the sort order for iteration.
-
#rebuild(id = @id, order: @order, limit: @limit, page_size: @page_size) ⇒ ErrorEnumerator
Build a new ErrorEnumerator with the given overrides, preserving all other fields.
-
#reverse_each(&block) ⇒ void
Iterate over errors in reverse order.
-
#reverse_order ⇒ ErrorEnumerator
Reverse the sort order.
-
#take(n) ⇒ Array[Resources::ErrorRecord]
Return the first
nerrors.
Constructor Details
#initialize(id, order: nil, limit: nil, page_size: nil) ⇒ ErrorEnumerator
Initialize the enumerator.
28 29 30 31 32 33 |
# File 'lib/zizq/resources/error_enumerator.rb', line 28 def initialize(id, order: nil, limit: nil, page_size: nil) @id = id @order = order @limit = limit @page_size = page_size end |
Instance Method Details
#any? ⇒ void
This method returns an undefined value.
Returns true if there are any errors.
Without a block, optimised to fetch a single error. With a block, falls back to Enumerable (tests each error against the block).
90 91 92 93 94 |
# File 'lib/zizq/resources/error_enumerator.rb', line 90 def any? return super if block_given? !first.nil? end |
#each(&block) ⇒ void
This method returns an undefined value.
Iterate over errors, lazily paginating through results.
Respects limit if set. Without a block, returns an Enumerator.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/zizq/resources/error_enumerator.rb', line 168 def each(&block) enumerator = enum_for(:each) if block_given? remaining = @limit each_page do |page| page.errors.each do |error| if remaining break if remaining <= 0 end yield error remaining -= 1 if remaining end end end enumerator end |
#each_page(&block) ⇒ void
This method returns an undefined value.
Iterate over pages of errors.
Each page is a Resources::ErrorPage. Without a block, returns an
Enumerator.
If limit is set, terminates after the last page is reached that
exceeds the limit, but does not truncate the page.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/zizq/resources/error_enumerator.rb', line 200 def each_page(&block) enumerator = enum_for(:each_page) if block_given? page = Zizq.client.list_errors( @id, limit: [ @page_size, @limit, (@page_size || @limit) && MAX_PAGE_SIZE ].compact.min, order: @order ) remaining = @limit while page yield page if remaining remaining -= page.errors.size break if remaining <= 0 end page = page.next_page end end enumerator end |
#empty? ⇒ Boolean
Returns true if there are no errors.
Optimised: fetches a single error to check.
79 80 81 |
# File 'lib/zizq/resources/error_enumerator.rb', line 79 def empty? first.nil? end |
#first ⇒ Resources::ErrorRecord?
Return the first error, or nil if no errors.
Optimised: fetches a single error from the server (?limit=1).
138 139 140 |
# File 'lib/zizq/resources/error_enumerator.rb', line 138 def first limit(1).each.first end |
#in_pages_of(page_size) ⇒ ErrorEnumerator
Set the page size for paginated iteration.
When set, each_page fetches pages of this size, and each fetches
errors in pages of this size.
42 43 44 |
# File 'lib/zizq/resources/error_enumerator.rb', line 42 def in_pages_of(page_size) rebuild(page_size:) end |
#last ⇒ Resources::ErrorRecord?
Return the last error, or nil if no errors.
Optimised: reverses the order and fetches a single error.
147 148 149 |
# File 'lib/zizq/resources/error_enumerator.rb', line 147 def last reverse_order.first end |
#limit(limit) ⇒ ErrorEnumerator
Limit the total number of errors returned.
This is a total limit, imposed across potentially multiple page fetches.
60 61 62 |
# File 'lib/zizq/resources/error_enumerator.rb', line 60 def limit(limit) rebuild(limit:) end |
#none? ⇒ void
This method returns an undefined value.
Returns true if there are no errors.
Without a block, optimised to fetch a single error. With a block, falls back to Enumerable (tests each error against the block).
103 104 105 106 107 |
# File 'lib/zizq/resources/error_enumerator.rb', line 103 def none? return super if block_given? first.nil? end |
#one? ⇒ void
This method returns an undefined value.
Returns true if there is exactly one error.
Without a block, optimised to fetch at most two errors. With a block, falls back to Enumerable.
116 117 118 119 120 |
# File 'lib/zizq/resources/error_enumerator.rb', line 116 def one? return super if block_given? limit(2).to_a.size == 1 end |
#order(order) ⇒ ErrorEnumerator
Set the sort order for iteration.
50 51 52 |
# File 'lib/zizq/resources/error_enumerator.rb', line 50 def order(order) rebuild(order:) end |
#rebuild(id = @id, order: @order, limit: @limit, page_size: @page_size) ⇒ ErrorEnumerator
Build a new ErrorEnumerator with the given overrides, preserving all other fields.
238 239 240 |
# File 'lib/zizq/resources/error_enumerator.rb', line 238 def rebuild(id = @id, order: @order, limit: @limit, page_size: @page_size) self.class.new(id, limit:, order:, page_size:) end |
#reverse_each(&block) ⇒ void
This method returns an undefined value.
Iterate over errors in reverse order.
Optimised: pushes the reverse ordering to the server instead of fetching all errors into memory and reversing.
129 130 131 |
# File 'lib/zizq/resources/error_enumerator.rb', line 129 def reverse_each(&block) reverse_order.each(&block) end |
#reverse_order ⇒ ErrorEnumerator
Reverse the sort order.
Returns a new query with the opposite order. If no order was set, defaults to descending (the server default is ascending).
70 71 72 |
# File 'lib/zizq/resources/error_enumerator.rb', line 70 def reverse_order rebuild(order: @order == :desc ? :asc : :desc) end |
#take(n) ⇒ Array[Resources::ErrorRecord]
Return the first n errors.
Optimised: sets the limit to n so the server only returns what's
needed.
158 159 160 |
# File 'lib/zizq/resources/error_enumerator.rb', line 158 def take(n) limit(n).to_a end |