Class: Yobi::ResticOutput::LazyList
- Inherits:
-
Object
- Object
- Yobi::ResticOutput::LazyList
- 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.
11
Instance Method Summary collapse
-
#each {|item| ... } ⇒ Enumerator
If no block is given.
-
#initialize(output, offsets, transform) ⇒ LazyList
constructor
A new instance of LazyList.
- #inspect ⇒ String
- #pretty_print(q) ⇒ void
-
#size ⇒ Integer
(also: #length)
The number of messages, known upfront from #index - unlike
Enumerable#count, doesn't read/transform a single one to answer.
Constructor Details
#initialize(output, offsets, transform) ⇒ LazyList
Returns a new instance of LazyList.
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
#each ⇒ void #each ⇒ Enumerator[Elem, void]
Returns 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 |
#inspect ⇒ 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 |
#size ⇒ Integer 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.
219 220 221 |
# File 'lib/yobi/restic_output.rb', line 219 def size @offsets.size end |