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.
-
#content ⇒ String
private
Return content of buffer.
-
#fresh_line? ⇒ Boolean
private
Test for a fresh line.
-
#indent ⇒ self
private
Increase indent.
-
#initialize ⇒ undefined
constructor
private
Initialize object.
-
#nl ⇒ self
private
Write newline.
- #root_indent ⇒ Object
-
#unindent ⇒ self
private
Decrease indent.
-
#write(fragment) ⇒ self
Write raw fragment to buffer.
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
16 17 18 19 |
# File 'lib/unparser/buffer.rb', line 16 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
29 30 31 32 33 34 35 |
# File 'lib/unparser/buffer.rb', line 29 def append(string) if @content[-1].eql?(NL) prefix end write(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
45 46 47 |
# File 'lib/unparser/buffer.rb', line 45 def append_without_prefix(string) write(string) 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
104 105 106 |
# File 'lib/unparser/buffer.rb', line 104 def content @content.dup.freeze end |
#fresh_line? ⇒ Boolean
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
94 95 96 |
# File 'lib/unparser/buffer.rb', line 94 def fresh_line? @content.empty? || @content[-1].eql?(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
55 56 57 58 |
# File 'lib/unparser/buffer.rb', line 55 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
77 78 79 |
# File 'lib/unparser/buffer.rb', line 77 def nl write(NL) end |
#root_indent ⇒ Object
81 82 83 84 85 86 |
# File 'lib/unparser/buffer.rb', line 81 def root_indent before = @indent @indent = 0 yield @indent = before 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
66 67 68 69 |
# File 'lib/unparser/buffer.rb', line 66 def unindent @indent -= 1 self end |
#write(fragment) ⇒ self
Write raw fragment to buffer
Does not do indentation logic.
115 116 117 118 |
# File 'lib/unparser/buffer.rb', line 115 def write(fragment) @content << fragment self end |