Class: Unparser::Buffer
- Inherits:
-
Object
- Object
- Unparser::Buffer
- Defined in:
- lib/unparser/buffer.rb
Overview
Buffer used to emit into
Constant Summary collapse
- NL =
"\n".freeze
Instance Method Summary collapse
-
#append(string) ⇒ self
private
Append string.
-
#append_without_prefix(string) ⇒ self
private
Append a string without an indentation prefix.
-
#capture_content ⇒ String
private
Capture the content written to the buffer within the block.
-
#content ⇒ String
private
Return content of buffer.
-
#fresh_line? ⇒ true, false
private
Test for a fresh line.
-
#indent ⇒ self
private
Increase indent.
-
#initialize ⇒ undefined
constructor
private
Initialize object.
-
#nl ⇒ self
private
Write newline.
-
#unindent ⇒ self
private
Decrease indent.
Constructor Details
#initialize ⇒ undefined
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize object
14 15 16 17 |
# File 'lib/unparser/buffer.rb', line 14 def initialize @content = '' @indent = 0 end |
Instance Method Details
#append(string) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Append string
27 28 29 30 31 32 33 |
# File 'lib/unparser/buffer.rb', line 27 def append(string) if @content[-1] == NL prefix end @content << string self end |
#append_without_prefix(string) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Append a string without an indentation prefix
43 44 45 46 |
# File 'lib/unparser/buffer.rb', line 43 def append_without_prefix(string) @content << string self end |
#capture_content ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Capture the content written to the buffer within the block
111 112 113 114 115 |
# File 'lib/unparser/buffer.rb', line 111 def capture_content size_before = @content.size yield @content[size_before..-1] end |
#content ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return content of buffer
101 102 103 |
# File 'lib/unparser/buffer.rb', line 101 def content @content.dup.freeze end |
#fresh_line? ⇒ true, false
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Test for a fresh line
91 92 93 |
# File 'lib/unparser/buffer.rb', line 91 def fresh_line? @content.empty? || @content[-1] == NL end |
#indent ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Increase indent
54 55 56 57 |
# File 'lib/unparser/buffer.rb', line 54 def indent @indent += 1 self end |
#nl ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Write newline
76 77 78 79 |
# File 'lib/unparser/buffer.rb', line 76 def nl @content << NL self end |
#unindent ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Decrease indent
65 66 67 68 |
# File 'lib/unparser/buffer.rb', line 65 def unindent @indent -= 1 self end |