Class: Sus::Output::Buffered

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/output/buffered.rb

Overview

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

Constant Summary collapse

INDENT =

The indent operation marker.

[:indent].freeze
OUTDENT =

The outdent operation marker.

[:outdent].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tee = nil) ⇒ Buffered

Initialize a new Buffered output handler.



17
18
19
20
# File 'lib/sus/output/buffered.rb', line 17

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

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



23
24
25
# File 'lib/sus/output/buffered.rb', line 23

def chunks
  @chunks
end

#teeObject (readonly)

Returns the value of attribute tee.



26
27
28
# File 'lib/sus/output/buffered.rb', line 26

def tee
  @tee
end

#The stored output chunks.(storedoutputchunks.) ⇒ Object (readonly)



23
# File 'lib/sus/output/buffered.rb', line 23

attr :chunks

Instance Method Details

#append(buffer) ⇒ Object

Append chunks from another buffer.



51
52
53
54
# File 'lib/sus/output/buffered.rb', line 51

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

#assert(*arguments) ⇒ Object

Record an assertion.



122
123
124
125
# File 'lib/sus/output/buffered.rb', line 122

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

#bufferedObject

Create a nested buffered output handler.



39
40
41
# File 'lib/sus/output/buffered.rb', line 39

def buffered
	self.class.new(self)
end

#each(&block) ⇒ Object

Iterate over stored chunks.



45
46
47
# File 'lib/sus/output/buffered.rb', line 45

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

#error(*arguments) ⇒ Object

Record an error.



136
137
138
139
# File 'lib/sus/output/buffered.rb', line 136

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

#indentObject

Increase indentation level.



75
76
77
78
# File 'lib/sus/output/buffered.rb', line 75

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

#indentedObject

Execute a block with increased indentation.



91
92
93
94
95
96
# File 'lib/sus/output/buffered.rb', line 91

def indented
	self.indent
	yield
ensure
	self.outdent
end

#inform(*arguments) ⇒ Object

Record an informational message.



143
144
145
146
# File 'lib/sus/output/buffered.rb', line 143

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

#inspectObject



29
30
31
32
33
34
35
# File 'lib/sus/output/buffered.rb', line 29

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

#outdentObject

Decrease indentation level.



84
85
86
87
# File 'lib/sus/output/buffered.rb', line 84

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

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.



60
61
62
# File 'lib/sus/output/buffered.rb', line 60

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

#puts(*arguments) ⇒ Object

Write output followed by a newline.



107
108
109
110
# File 'lib/sus/output/buffered.rb', line 107

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

#skip(*arguments) ⇒ Object

Record a skip.



129
130
131
132
# File 'lib/sus/output/buffered.rb', line 129

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

#stringObject



65
66
67
68
69
# File 'lib/sus/output/buffered.rb', line 65

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

#The output handler to tee to.=(outputhandlertoteeto. = (value)) ⇒ Object



26
# File 'lib/sus/output/buffered.rb', line 26

attr :tee

#variable(value, limit: Variable::TRUNCATION_LIMIT) ⇒ Object

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



116
117
118
# File 'lib/sus/output/buffered.rb', line 116

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

#write(*arguments) ⇒ Object

Write output.



100
101
102
103
# File 'lib/sus/output/buffered.rb', line 100

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