Class: RubyUIConverter::CodeBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_ui_converter/code_builder.rb

Overview

Accumulates indented lines of Ruby source.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent: " ", level: 0) ⇒ CodeBuilder

Returns a new instance of CodeBuilder.



6
7
8
9
10
# File 'lib/ruby_ui_converter/code_builder.rb', line 6

def initialize(indent: "  ", level: 0)
  @indent = indent
  @level = level
  @lines = []
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



12
13
14
# File 'lib/ruby_ui_converter/code_builder.rb', line 12

def level
  @level
end

Instance Method Details

#dedentObject



24
25
26
27
# File 'lib/ruby_ui_converter/code_builder.rb', line 24

def dedent
  @level -= 1 if @level.positive?
  self
end

#indentObject



19
20
21
22
# File 'lib/ruby_ui_converter/code_builder.rb', line 19

def indent
  @level += 1
  self
end

#line(str = nil) ⇒ Object



14
15
16
17
# File 'lib/ruby_ui_converter/code_builder.rb', line 14

def line(str = nil)
  @lines << (str.nil? || str.empty? ? "" : (@indent * @level) + str)
  self
end

#to_sObject



29
30
31
# File 'lib/ruby_ui_converter/code_builder.rb', line 29

def to_s
  "#{@lines.join("\n")}\n"
end