Class: CodeCoverage::MarkdownTable

Inherits:
Object
  • Object
show all
Defined in:
lib/code_coverage/markdown_table.rb

Overview

Generate a markdown table.

Constant Summary collapse

COLUMN_SEPARATOR =
'|'
HEADER_SEPARATOR =
'-'

Instance Method Summary collapse

Constructor Details

#initializeMarkdownTable

Initialize table generator.



10
11
12
13
14
# File 'lib/code_coverage/markdown_table.rb', line 10

def initialize
  @header = COLUMN_SEPARATOR.dup
  @header_separator = COLUMN_SEPARATOR.dup
  @lines = []
end

Instance Method Details

#header(*args) ⇒ Object

Add each entry to the table header



17
18
19
20
21
22
23
24
25
26
# File 'lib/code_coverage/markdown_table.rb', line 17

def header(*args)
  args.each_with_index do |item, index|
    @header << "#{item}#{COLUMN_SEPARATOR}"
    @header_separator << if index.zero?
                           ":#{HEADER_SEPARATOR}#{COLUMN_SEPARATOR}"
                         else
                           ":#{HEADER_SEPARATOR}:#{COLUMN_SEPARATOR}"
                         end
  end
end

#line(*args) ⇒ Object

Add a new line entry to the table.

Parameters:

  • args (String)
    • Multiple comma separated strings for each column entry.



38
39
40
41
42
43
44
# File 'lib/code_coverage/markdown_table.rb', line 38

def line(*args)
  line = COLUMN_SEPARATOR.dup
  args.each do |item|
    line << "#{item}#{COLUMN_SEPARATOR}"
  end
  @lines << line
end

#sizeInteger

Return the number of lines without header items.

Returns:

  • (Integer)

    Number of lines.



31
32
33
# File 'lib/code_coverage/markdown_table.rb', line 31

def size
  @lines.length
end

#to_markdownString

Combine all data to a markdown table string.

Returns:

  • (String)

    Table.



48
49
50
51
# File 'lib/code_coverage/markdown_table.rb', line 48

def to_markdown
  result = +"#{@header}\n#{@header_separator}\n"
  result << @lines.join("\n")
end