Class: Bob::Compiler::Buffer

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

Defined Under Namespace

Classes: SameLineProxy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent_level = 0) ⇒ Buffer

Returns a new instance of Buffer.



20
21
22
23
24
# 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

Instance Attribute Details

#stringObject (readonly) Also known as: to_s

Returns the value of attribute string.



17
18
19
# File 'lib/bob/compiler/buffer.rb', line 17

def string
  @string
end

Instance Method Details

#<<(string, same_line = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

#indented(increment = 2) ⇒ Object



56
57
58
59
60
61
62
# 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) ⇒ Object



42
43
44
45
46
47
48
# 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!Object Also known as: blankline!



50
51
52
# File 'lib/bob/compiler/buffer.rb', line 50

def newline!
  @string << "\n"
end