Class: DiverDown::Web::IndentedStringIo

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/diver_down/web/indented_string_io.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tab: ' ') ⇒ IndentedStringIo

Returns a new instance of IndentedStringIo.

Parameters:

  • tab (String) (defaults to: ' ')


16
17
18
19
20
# File 'lib/diver_down/web/indented_string_io.rb', line 16

def initialize(tab: '  ')
  @io = StringIO.new
  @indent = 0
  @tab = tab
end

Instance Attribute Details

#indentObject

Returns the value of attribute indent.



13
14
15
# File 'lib/diver_down/web/indented_string_io.rb', line 13

def indent
  @indent
end

Instance Method Details

#indentedObject

increase the indent level for the block



51
52
53
54
55
56
# File 'lib/diver_down/web/indented_string_io.rb', line 51

def indented
  @indent += 1
  yield
ensure
  @indent -= 1
end

#puts(*contents, indent: true) ⇒ void

This method returns an undefined value.

Parameters:

  • content (String)


45
46
47
48
# File 'lib/diver_down/web/indented_string_io.rb', line 45

def puts(*contents, indent: true)
  write("#{contents.join("\n")}\n", indent:)
  nil
end

#write(*contents, indent: true) ⇒ void

This method returns an undefined value.

Parameters:

  • contents (Array<String>)
  • indent (Boolean) (defaults to: true)

    Enable or disable indentation



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/diver_down/web/indented_string_io.rb', line 25

def write(*contents, indent: true)
  indent_string = if indent
                    @tab * @indent
                  else
                    ''
                  end

  string = contents.join
  lines = string.lines
  lines.each do |line|
    if line == "\n"
      @io.write "\n"
    else
      @io.write "#{indent_string}#{line}"
    end
  end
end