Class: RTables::Table::MonoTable

Inherits:
RTables::TableBuilder show all
Defined in:
lib/rtables/tables/monotable.rb

Instance Attribute Summary

Attributes inherited from RTables::TableBuilder

#columns, #rows, #table_content, #table_header

Instance Method Summary collapse

Methods inherited from RTables::TableBuilder

#add_column, #add_row, #column_exist?, #empty?, #initialize, #inspect, #raise_if_empty, #to_s

Constructor Details

This class inherits a constructor from RTables::TableBuilder

Instance Method Details

#max_content_lengthObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rtables/tables/monotable.rb', line 43

def max_content_length
  max_len = 0

  @table_content.each do |contents|
    contents.each do |content|
      len = content.length
      max_len = len if max_len < len
    end
  end

  max_len
end

#max_header_lengthObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/rtables/tables/monotable.rb', line 56

def max_header_length
  max_len = 0

  @table_header.each do |head|
    len = head.length
    max_len = len if max_len < len
  end

  max_len
end

#pad_content(content, clen) ⇒ Object



37
38
39
40
41
# File 'lib/rtables/tables/monotable.rb', line 37

def pad_content(content, clen)
  spacing = clen - content.length

  content + (' ' * spacing)
end

#pad_header(header, hlen) ⇒ Object



31
32
33
34
35
# File 'lib/rtables/tables/monotable.rb', line 31

def pad_header(header, hlen)
  spacing = hlen - header.length

  header + (' ' * spacing)
end

#renderObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rtables/tables/monotable.rb', line 4

def render
  corner = '+'
  line_horizontal = '-'
  line_vertical = '|'

  hlen = max_header_length
  clen = max_content_length

  row_sep = "#{corner}#{line_horizontal * (hlen + 2)}#{corner}#{line_horizontal * (clen + 2)}#{corner}"
  line_fmt = "#{line_vertical} %{header} #{line_vertical} %{content} #{line_vertical}"
  lines = []
  i = 0

  lines << row_sep
  @table_content.each do |contents|
    contents.each do |content|
      lines << line_fmt % { header: pad_header(@table_header[i], hlen), content: pad_content(content, clen) }
      i += 1
    end

    i = 0
    lines << row_sep
  end

  lines
end