SusSourceSusOutputBuffered

class Buffered

Represents a buffered output handler that stores output operations for later replay.

Definitions

def initialize(tee = nil)

Initialize a new Buffered output handler.

Signature

parameter tee Output, nil

Optional output handler to tee output to.

Implementation

def initialize(tee = nil)
	@chunks = Array.new
	@tee = tee
end

attr :chunks

Signature

attribute Array

The stored output chunks.

attr :tee

Signature

attribute Output, nil

The output handler to tee to.

def inspect

Signature

returns String

A string representation of this buffered output.

Implementation

def inspect
	if @tee
		"\#<#{self.class.name} #{@chunks.size} chunks -> #{@tee.class}>"
	else
		"\#<#{self.class.name} #{@chunks.size} chunks>"
	end
end

def buffered

Create a nested buffered output handler.

Signature

returns Buffered

A new Buffered instance that tees to this one.

Implementation

def buffered
	self.class.new(self)
end

def each(&block)

Iterate over stored chunks.

Signature

yields {|chunk| ...}

Each stored chunk.

Implementation

def each(&block)
	@chunks.each(&block)
end

def append(buffer)

Append chunks from another buffer.

Signature

parameter buffer Buffered

The buffer to append from.

Implementation

def append(buffer)
	@chunks.concat(buffer.chunks)
	@tee&.append(buffer)
end

def print(output)

Replay this buffer into the given output. This allows a buffer (a captured stream of formatting instructions) to be used anywhere a printable target is expected, e.g. as a nested assertion label.

Signature

parameter output Output

The output target.

Implementation

def print(output)
	output.append(self)
end

def string

Signature

returns String

The buffered output as a string.

Implementation

def string
	io = StringIO.new
	Text.new(io).append(@chunks)
	return io.string
end

INDENT = [:indent].freeze

The indent operation marker.

def indent

Increase indentation level.

Implementation

def indent
	@chunks << INDENT
	@tee&.indent
end

OUTDENT = [:outdent].freeze

The outdent operation marker.

def outdent

Decrease indentation level.

Implementation

def outdent
	@chunks << OUTDENT
	@tee&.outdent
end

def indented

Execute a block with increased indentation.

Signature

yields {...}

The block to execute.

Implementation

def indented
	self.indent
	yield
ensure
	self.outdent
end

def write(*arguments)

Write output.

Signature

parameter arguments Array

The arguments to write.

Implementation

def write(*arguments)
	@chunks << [:write, *arguments]
	@tee&.write(*arguments)
end

def puts(*arguments)

Write output followed by a newline.

Signature

parameter arguments Array

The arguments to write.

Implementation

def puts(*arguments)
	@chunks << [:puts, *arguments]
	@tee&.puts(*arguments)
end

def variable(value, limit: Variable::TRUNCATION_LIMIT)

Write a value in the variable style: a compact, truncated representation (handling large values, recursion and styling internally).

Signature

parameter value Object

The value to represent.

parameter limit Integer

The maximum length of the representation.

Implementation

def variable(value, limit: Variable::TRUNCATION_LIMIT)
	Variable.format(self, value, limit: limit)
end

def assert(*arguments)

Record an assertion.

Signature

parameter arguments Array

The assertion arguments.

Implementation

def assert(*arguments)
	@chunks << [:assert, *arguments]
	@tee&.assert(*arguments)
end

def skip(*arguments)

Record a skip.

Signature

parameter arguments Array

The skip arguments.

Implementation

def skip(*arguments)
	@chunks << [:skip, *arguments]
	@tee&.skip(*arguments)
end

def error(*arguments)

Record an error.

Signature

parameter arguments Array

The error arguments.

Implementation

def error(*arguments)
	@chunks << [:error, *arguments]
	@tee&.error(*arguments)
end

def inform(*arguments)

Record an informational message.

Signature

parameter arguments Array

The message arguments.

Implementation

def inform(*arguments)
	@chunks << [:inform, *arguments]
	@tee&.inform(*arguments)
end