Class: Unparser::Buffer

Inherits:
Object
  • Object
show all
Defined in:
lib/unparser/buffer.rb

Overview

Buffer used to emit into

Constant Summary collapse

NL =
"\n".freeze

Instance Method Summary collapse

Constructor Details

#initializeundefined

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

Parameters:

  • string (String)

Returns:

  • (self)


29
30
31
32
33
34
35
# File 'lib/unparser/buffer.rb', line 29

def append(string)
  if @content[-1] == 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

Parameters:

  • string (String)

Returns:

  • (self)


45
46
47
48
# File 'lib/unparser/buffer.rb', line 45

def append_without_prefix(string)
  write(string)
  self
end

#capture_contentString

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

Returns:

  • (String)


113
114
115
116
117
# File 'lib/unparser/buffer.rb', line 113

def capture_content
  size_before = @content.size
  yield
  @content[size_before..-1]
end

#contentString

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

Returns:

  • (String)


103
104
105
# File 'lib/unparser/buffer.rb', line 103

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

Returns:

  • (true)

    if the buffer content ends with a fresh line

  • (false)

    otherwise



93
94
95
# File 'lib/unparser/buffer.rb', line 93

def fresh_line?
  @content.empty? || @content[-1] == NL
end

#indentself

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

Returns:

  • (self)


56
57
58
59
# File 'lib/unparser/buffer.rb', line 56

def indent
  @indent += 1
  self
end

#nlself

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

Returns:

  • (self)


78
79
80
81
# File 'lib/unparser/buffer.rb', line 78

def nl
  write(NL)
  self
end

#unindentself

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

Returns:

  • (self)


67
68
69
70
# File 'lib/unparser/buffer.rb', line 67

def unindent
  @indent -= 1
  self
end