class Unparser::Buffer
Buffer
used to emit into
Constants
- INDENT_SPACE
- NL
Public Class Methods
new()
click to toggle source
Initialize object
@return [undefined]
@api private
# File lib/unparser/buffer.rb, line 16 def initialize @content = +'' @indent = 0 end
Public Instance Methods
append(string)
click to toggle source
Append string
@param [String] string
@return [self]
@api private
# 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)
click to toggle source
Append a string without an indentation prefix
@param [String] string
@return [self]
@api private
# File lib/unparser/buffer.rb, line 45 def append_without_prefix(string) write(string) self end
capture_content() { || ... }
click to toggle source
Capture the content written to the buffer within the block
@return [String]
@api private
# File lib/unparser/buffer.rb, line 116 def capture_content size_before = @content.size yield @content[size_before..-1] end
content()
click to toggle source
Return content of buffer
@return [String]
@api private
# File lib/unparser/buffer.rb, line 106 def content @content.dup.freeze end
fresh_line?()
click to toggle source
Test for a fresh line
@return [Boolean]
@api private
# File lib/unparser/buffer.rb, line 96 def fresh_line? @content.empty? || @content[-1].eql?(NL) end
indent()
click to toggle source
Increase indent
@return [self]
@api private
# File lib/unparser/buffer.rb, line 56 def indent @indent += 1 self end
nl()
click to toggle source
Write newline
@return [self]
@api private
# File lib/unparser/buffer.rb, line 78 def nl write(NL) self end
root_indent() { || ... }
click to toggle source
# File lib/unparser/buffer.rb, line 83 def root_indent before = @indent @indent = 0 yield @indent = before end
unindent()
click to toggle source
Decrease indent
@return [self]
@api private
# File lib/unparser/buffer.rb, line 67 def unindent @indent -= 1 self end
write(fragment)
click to toggle source
Write raw fragment to buffer
Does not do indentation logic.
@param [String] fragment
@return [self]
# File lib/unparser/buffer.rb, line 129 def write(fragment) @content << fragment self end
Private Instance Methods
prefix()
click to toggle source
# File lib/unparser/buffer.rb, line 138 def prefix write(INDENT_SPACE * @indent) end