Class: Yobi::ResticOutput::LazyList

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Enumerable[Elem]
Defined in:
lib/yobi/restic_output.rb,
sig/yobi.rbs

Overview

A lazily-transformed view over a set of #index offsets, printing like a plain Array (bounded to a preview, unlike one). Returned by #messages when given a transform block. #inspect/#pretty_print's preview is memoized, and #each reuses it rather than re-reading/re-transforming those same offsets again when the caller iterates the whole thing.

Constant Summary collapse

INSPECT_PREVIEW_SIZE =

Items #inspect/#pretty_print show before truncating with "...". One louder than ActiveRecord::Relation#inspect's own preview size.

Returns:

  • (Integer)
11

Instance Method Summary collapse

Constructor Details

#initialize(output, offsets, transform) ⇒ LazyList

Returns a new instance of LazyList.

Parameters:

  • output (Yobi::ResticOutput)
  • offsets (Array<Integer>)
  • transform (Proc)

    applied to each offset's raw parsed Hash



200
201
202
203
204
# File 'lib/yobi/restic_output.rb', line 200

def initialize(output, offsets, transform)
  @output = output
  @offsets = offsets
  @transform = transform
end

Instance Method Details

#eachvoid #eachEnumerator[Elem, void]

Returns if no block is given.

Overloads:

  • #eachvoid

    This method returns an undefined value.

  • #eachEnumerator[Elem, void]

    Returns:

    • (Enumerator[Elem, void])

Yields:

Yield Parameters:

  • item (Object)

Yield Returns:

  • (void)

Returns:

  • (Enumerator)

    if no block is given



208
209
210
211
212
213
# File 'lib/yobi/restic_output.rb', line 208

def each
  return enum_for(:each) unless block_given?

  preview.each { |item| yield item }
  @offsets[preview.size..].each { |offset| yield transform_at(offset) }
end

#inspectString

Returns:

  • (String)


225
226
227
228
229
# File 'lib/yobi/restic_output.rb', line 225

def inspect
  shown = preview.first(INSPECT_PREVIEW_SIZE).map(&:inspect).join(", ")
  shown += ", ..." if preview.size > INSPECT_PREVIEW_SIZE
  "[#{shown}]"
end

#pretty_print(q) ⇒ void

This method returns an undefined value.



232
233
234
235
236
237
# File 'lib/yobi/restic_output.rb', line 232

def pretty_print(q)
  q.group(1, "[", "]") do
    q.seplist(preview.first(INSPECT_PREVIEW_SIZE)) { |item| q.pp item }
    q.text ", ..." if preview.size > INSPECT_PREVIEW_SIZE
  end
end

#sizeInteger Also known as: length

The number of messages, known upfront from Yobi::ResticOutput#index - unlike Enumerable#count, doesn't read/transform a single one to answer.

Returns:

  • (Integer)


219
220
221
# File 'lib/yobi/restic_output.rb', line 219

def size
  @offsets.size
end