class Gauge::Table
Holds a table definition. This corresponds to a markdown table defined in the .spec files. @api public
Public Class Methods
new(protoTable)
click to toggle source
@api private
# File lib/table.rb, line 11 def initialize(protoTable) @columns = protoTable.headers.cells @rows = [] protoTable.rows.each do |row| @rows.push Row.new(@columns, row.cells) end end
Public Instance Methods
[](index)
click to toggle source
Gets the table data. @param index Either row index, or Column name. @return [Hash] When an integer index is passed. Values correspond to a the row in the table with the index. @example
table[0] => {"Col1" => "Row1.Cell1", "Col2" => "Row2.Col1", ...} table[i] => nil # when index is out of range
@return [string When a string key is passed. Values correspond to the respective cells in a row, matching the index of value in Column headers. @example
table["Col1"] => ["Row1.Cell1", "Row1.Cell1", ...] table["Invalid_Col_Name"] => nil
# File lib/table.rb, line 41 def [](index) return @rows[index] if index.is_a?(Integer) column_values_as_array(index) end
columns()
click to toggle source
Gets the column headers of the table @return [string
# File lib/table.rb, line 21 def columns @columns end
rows()
click to toggle source
Gets the rows of the table. The rows are two dimensional arrays. @return [string[]]
# File lib/table.rb, line 27 def rows @rows end
to_s()
click to toggle source
Converts the table to the markdown equivalent string
# File lib/table.rb, line 47 def to_s col_str = Array.new(@rows.length+2, '') @columns.each{|c| col_vals = column_values_as_array(c).unshift c col_width = col_vals.map(&:length).max col_vals.insert(1, '-' * col_width) col_str = col_str.zip(col_vals.map{|x| '|' + x.ljust(col_width)}).map { |x| x[0] + x[1]} } col_str.join('|\n') + '|' end
Private Instance Methods
column_values_as_array(col_name)
click to toggle source
# File lib/table.rb, line 60 def column_values_as_array(col_name) i = @columns.index col_name i.nil? ? nil : @rows.map { |r| r[i] } end