Class: Zizq::Resources::ErrorEnumerator

Inherits:
Object
  • Object
show all
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.

Returns:

  • (Integer)
2000

Instance Method Summary collapse

Constructor Details

#initialize(id, order: nil, limit: nil, page_size: nil) ⇒ ErrorEnumerator

Initialize the enumerator.

RBS:

  • id: String

  • order: Zizq::sort_direction?

  • limit: Integer?

  • page_size: Integer?

  • return: void

Parameters:

  • id (String)
  • order: (Zizq::sort_direction, nil) (defaults to: nil)
  • limit: (Integer, nil) (defaults to: nil)
  • page_size: (Integer, nil) (defaults to: nil)


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).

RBS:

  • &block: ?(Resources::ErrorRecord) -> bool

  • return: bool



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.

RBS:

  • &block: ?(Resources::ErrorRecord) -> void

  • return: ::Enumerator[Zizq::Resources::ErrorRecord, void]



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.

RBS:

  • &block: ?(Resources::ErrorPage) -> void

  • return: ::Enumerator[Zizq::Resources::ErrorPage, void]



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.

RBS:

  • return: bool

Returns:

  • (Boolean)


79
80
81
# File 'lib/zizq/resources/error_enumerator.rb', line 79

def empty?
  first.nil?
end

#firstResources::ErrorRecord?

Return the first error, or nil if no errors.

Optimised: fetches a single error from the server (?limit=1).

RBS:

  • return: Resources::ErrorRecord?

Returns:



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.

RBS:

  • page_size: Integer?

  • return: ErrorEnumerator

Parameters:

  • page_size (Integer, nil)

Returns:



42
43
44
# File 'lib/zizq/resources/error_enumerator.rb', line 42

def in_pages_of(page_size)
  rebuild(page_size:)
end

#lastResources::ErrorRecord?

Return the last error, or nil if no errors.

Optimised: reverses the order and fetches a single error.

RBS:

  • return: Resources::ErrorRecord?

Returns:



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.

RBS:

  • limit: Integer?

  • return: ErrorEnumerator

Parameters:

  • limit (Integer, nil)

Returns:



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).

RBS:

  • &block: ?(Resources::ErrorRecord) -> bool

  • return: bool



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.

RBS:

  • &block: ?(Resources::ErrorRecord) -> bool

  • return: bool



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.

RBS:

  • order: Zizq::sort_direction?

  • return: ErrorEnumerator

Parameters:

  • order (Zizq::sort_direction, nil)

Returns:



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.

RBS:

  • return: ErrorEnumerator

Parameters:

  • id (Object) (defaults to: @id)
  • order: (Object) (defaults to: @order)
  • limit: (Object) (defaults to: @limit)
  • page_size: (Object) (defaults to: @page_size)

Returns:



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.

RBS:

  • &block: ?(Resources::ErrorRecord) -> void

  • return: ::Enumerator[Zizq::Resources::ErrorRecord, void]



129
130
131
# File 'lib/zizq/resources/error_enumerator.rb', line 129

def reverse_each(&block)
  reverse_order.each(&block)
end

#reverse_orderErrorEnumerator

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).

RBS:

  • return: ErrorEnumerator

Returns:



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.

RBS:

  • n: Integer

  • return: Array[Resources::ErrorRecord]

Parameters:

  • n (Integer)

Returns:



158
159
160
# File 'lib/zizq/resources/error_enumerator.rb', line 158

def take(n)
  limit(n).to_a
end