Class: Zizq::Resources::ErrorEnumerator
- Inherits:
-
Object
- Object
- Zizq::Resources::ErrorEnumerator
- Includes:
- Enumerable
- Defined in:
- lib/zizq/resources/error_enumerator.rb
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? ⇒ Boolean
Returns true if there are any errors.
-
#each(&block) ⇒ Object
Iterate over errors, lazily paginating through results.
-
#each_page(&block) ⇒ Object
Iterate over pages of errors.
-
#empty? ⇒ Boolean
Returns true if there are no errors.
-
#first ⇒ Object
Return the first error, or nil if no errors.
-
#in_pages_of(page_size) ⇒ Object
Set the page size for paginated iteration.
-
#initialize(id, order: nil, limit: nil, page_size: nil) ⇒ ErrorEnumerator
constructor
Initialize the enumerator.
-
#last ⇒ Object
Return the last error, or nil if no errors.
-
#limit(limit) ⇒ Object
Limit the total number of errors returned.
-
#none? ⇒ Boolean
Returns true if there are no errors.
-
#one? ⇒ Boolean
Returns true if there is exactly one error.
-
#order(order) ⇒ Object
Set the sort order for iteration.
-
#reverse_each(&block) ⇒ Object
Iterate over errors in reverse order.
-
#reverse_order ⇒ Object
Reverse the sort order.
-
#take(n) ⇒ Object
Return the first ‘n` errors.
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? ⇒ Boolean
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) ⇒ Object
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) ⇒ Object
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 ⇒ Object
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) ⇒ Object
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 ⇒ Object
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) ⇒ Object
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? ⇒ Boolean
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? ⇒ Boolean
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) ⇒ Object
Set the sort order for iteration.
53 54 55 |
# File 'lib/zizq/resources/error_enumerator.rb', line 53 def order(order) rebuild(order:) end |
#reverse_each(&block) ⇒ Object
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 ⇒ Object
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) ⇒ Object
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 |