class Bob::Compiler::Buffer

Attributes

string[R]
to_s[R]

Public Class Methods

new(indent_level = 0) click to toggle source
# File lib/bob/compiler/buffer.rb, line 20
def initialize(indent_level = 0)
  @string = ''
  @indent_level = indent_level
  @same_line_proxy = SameLineProxy.new(self)
end

Public Instance Methods

<<(string, same_line = false) click to toggle source
# File lib/bob/compiler/buffer.rb, line 26
def <<(string, same_line = false)
  newline! unless first_line? || same_line

  if indented?
    string = indent(string, @indent_level)
  end

  if same_line
    string = string.lstrip
  end

  @string << string

  @same_line_proxy
end
blankline!()
Alias for: newline!
indented(increment = 2) { || ... } click to toggle source
# File lib/bob/compiler/buffer.rb, line 56
def indented(increment = 2)
  indent_level_was = @indent_level
  @indent_level += increment
  yield
ensure
  @indent_level = indent_level_was
end
merge(buffer) click to toggle source
# File lib/bob/compiler/buffer.rb, line 42
def merge(buffer)
  buffer.string.each_line.with_index do |line, i|
    self.<<(line.chomp, i == 0)
  end

  @same_line_proxy
end
newline!() click to toggle source
# File lib/bob/compiler/buffer.rb, line 50
def newline!
  @string << "\n"
end
Also aliased as: blankline!

Private Instance Methods

first_line?() click to toggle source
# File lib/bob/compiler/buffer.rb, line 65
def first_line?
  @string.empty?
end
indent(str, multiplier = 2) click to toggle source
# File lib/bob/compiler/buffer.rb, line 73
def indent(str, multiplier = 2)
  spaces = " " * multiplier
  str.each_line.map { |line| line.blank? ? line : "#{spaces}#{line}" }.join
end
indented?() click to toggle source
# File lib/bob/compiler/buffer.rb, line 69
def indented?
  @indent_level > 0
end