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 34 35 36 |
# 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).
93 94 95 96 97 |
# File 'lib/zizq/resources/error_enumerator.rb', line 93 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.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/zizq/resources/error_enumerator.rb', line 171 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.
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 |
# File 'lib/zizq/resources/error_enumerator.rb', line 203 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.
82 83 84 |
# File 'lib/zizq/resources/error_enumerator.rb', line 82 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).
141 142 143 |
# File 'lib/zizq/resources/error_enumerator.rb', line 141 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.
45 46 47 |
# File 'lib/zizq/resources/error_enumerator.rb', line 45 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.
150 151 152 |
# File 'lib/zizq/resources/error_enumerator.rb', line 150 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.
63 64 65 |
# File 'lib/zizq/resources/error_enumerator.rb', line 63 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).
106 107 108 109 110 |
# File 'lib/zizq/resources/error_enumerator.rb', line 106 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.
119 120 121 122 123 |
# File 'lib/zizq/resources/error_enumerator.rb', line 119 def one? return super if block_given? limit(2).to_a.size == 1 end |
#order(order) ⇒ ErrorEnumerator
Set the sort order for iteration.
53 54 55 |
# File 'lib/zizq/resources/error_enumerator.rb', line 53 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.
236 237 238 |
# File 'lib/zizq/resources/error_enumerator.rb', line 236 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.
132 133 134 |
# File 'lib/zizq/resources/error_enumerator.rb', line 132 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).
73 74 75 |
# File 'lib/zizq/resources/error_enumerator.rb', line 73 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.
161 162 163 |
# File 'lib/zizq/resources/error_enumerator.rb', line 161 def take(n) limit(n).to_a end |