Class: XCRes::StringBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/xcres/builder/string_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringBuilder

Returns a new instance of StringBuilder.



6
7
8
9
# File 'lib/xcres/builder/string_builder.rb', line 6

def initialize
  self.indentation_string = '    '
  self.result = ''
end

Instance Attribute Details

#indentation_stringObject

Returns the value of attribute indentation_string.



3
4
5
# File 'lib/xcres/builder/string_builder.rb', line 3

def indentation_string
  @indentation_string
end

#resultObject

Returns the value of attribute result.



4
5
6
# File 'lib/xcres/builder/string_builder.rb', line 4

def result
  @result
end

Instance Method Details

#<<(input) ⇒ Object Also known as: write



11
12
13
14
15
16
# File 'lib/xcres/builder/string_builder.rb', line 11

def << input
  # Only indent string inputs on nested / delegating string builders
  input = self.indentation_string + input unless result.is_a? String

  self.result << input
end

#section(&block) ⇒ Object



24
25
26
27
28
29
# File 'lib/xcres/builder/string_builder.rb', line 24

def section &block
  builder = self.class.new
  builder.indentation_string = self.indentation_string
  builder.result = self
  block.call builder
end

#writeln(input = '') ⇒ Object



20
21
22
# File 'lib/xcres/builder/string_builder.rb', line 20

def writeln input=''
  self << input + "\n"
end