Class: Sus::Output::Buffered
- Inherits:
-
Object
- Object
- Sus::Output::Buffered
- 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
-
#chunks ⇒ Object
readonly
Returns the value of attribute chunks.
-
#tee ⇒ Object
readonly
Returns the value of attribute tee.
- #The stored output chunks.(storedoutputchunks.) ⇒ Object readonly
Instance Method Summary collapse
-
#append(buffer) ⇒ Object
Append chunks from another buffer.
-
#assert(*arguments) ⇒ Object
Record an assertion.
-
#buffered ⇒ Object
Create a nested buffered output handler.
-
#each(&block) ⇒ Object
Iterate over stored chunks.
-
#error(*arguments) ⇒ Object
Record an error.
-
#indent ⇒ Object
Increase indentation level.
-
#indented ⇒ Object
Execute a block with increased indentation.
-
#inform(*arguments) ⇒ Object
Record an informational message.
-
#initialize(tee = nil) ⇒ Buffered
constructor
Initialize a new Buffered output handler.
- #inspect ⇒ Object
-
#outdent ⇒ Object
Decrease indentation level.
-
#print(output) ⇒ Object
Replay this buffer into the given output.
-
#puts(*arguments) ⇒ Object
Write output followed by a newline.
-
#skip(*arguments) ⇒ Object
Record a skip.
- #string ⇒ Object
- #The output handler to tee to.=(outputhandlertoteeto. = (value)) ⇒ Object
-
#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).
-
#write(*arguments) ⇒ Object
Write output.
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
#chunks ⇒ Object (readonly)
Returns the value of attribute chunks.
23 24 25 |
# File 'lib/sus/output/buffered.rb', line 23 def chunks @chunks end |
#tee ⇒ Object (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 |
#buffered ⇒ Object
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 |
#indent ⇒ Object
Increase indentation level.
75 76 77 78 |
# File 'lib/sus/output/buffered.rb', line 75 def indent @chunks << INDENT @tee&.indent end |
#indented ⇒ Object
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 |
#inspect ⇒ Object
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 |
#outdent ⇒ Object
Decrease indentation level.
84 85 86 87 |
# File 'lib/sus/output/buffered.rb', line 84 def outdent @chunks << OUTDENT @tee&.outdent end |
#print(output) ⇒ Object
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 |
#string ⇒ Object
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 |