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.



15
16
17
18
# File 'lib/sus/output/buffered.rb', line 15

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

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



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

def chunks
  @chunks
end

#teeObject (readonly)

Returns the value of attribute tee.



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

def tee
  @tee
end

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



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

attr :chunks

Instance Method Details

#append(buffer) ⇒ Object

Append chunks from another buffer.



49
50
51
52
# File 'lib/sus/output/buffered.rb', line 49

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

#assert(*arguments) ⇒ Object

Record an assertion.



104
105
106
107
# File 'lib/sus/output/buffered.rb', line 104

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

#bufferedObject

Create a nested buffered output handler.



37
38
39
# File 'lib/sus/output/buffered.rb', line 37

def buffered
	self.class.new(self)
end

#each(&block) ⇒ Object

Iterate over stored chunks.



43
44
45
# File 'lib/sus/output/buffered.rb', line 43

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

#error(*arguments) ⇒ Object

Record an error.



118
119
120
121
# File 'lib/sus/output/buffered.rb', line 118

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

#indentObject

Increase indentation level.



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

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

#indentedObject

Execute a block with increased indentation.



81
82
83
84
85
86
# File 'lib/sus/output/buffered.rb', line 81

def indented
	self.indent
	yield
ensure
	self.outdent
end

#inform(*arguments) ⇒ Object

Record an informational message.



125
126
127
128
# File 'lib/sus/output/buffered.rb', line 125

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

#inspectObject



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

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.



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

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

#puts(*arguments) ⇒ Object

Write output followed by a newline.



97
98
99
100
# File 'lib/sus/output/buffered.rb', line 97

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

#skip(*arguments) ⇒ Object

Record a skip.



111
112
113
114
# File 'lib/sus/output/buffered.rb', line 111

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

#stringObject



55
56
57
58
59
# File 'lib/sus/output/buffered.rb', line 55

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

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



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

attr :tee

#write(*arguments) ⇒ Object

Write output.



90
91
92
93
# File 'lib/sus/output/buffered.rb', line 90

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