Class: Gauge::Table
- Inherits:
-
Object
- Object
- Gauge::Table
- Defined in:
- lib/table.rb
Overview
Holds a table definition. This corresponds to a markdown table defined in the .spec files.
Instance Method Summary collapse
-
#[](index) ⇒ Hash, string[]
Gets the table data.
-
#columns ⇒ string[]
Gets the column headers of the table.
-
#initialize(protoTable) ⇒ Table
constructor
private
A new instance of Table.
-
#rows ⇒ string[][]
Gets the rows of the table.
-
#to_s ⇒ Object
Converts the table to the markdown equivalent string.
Constructor Details
#initialize(protoTable) ⇒ Table
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Table.
11 12 13 14 15 16 17 |
# 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 |
Instance Method Details
#[](index) ⇒ Hash, string[]
Gets the table data.
41 42 43 44 |
# File 'lib/table.rb', line 41 def [](index) return @rows[index] if index.is_a?(Integer) column_values_as_array(index) end |
#columns ⇒ string[]
Gets the column headers of the table
21 22 23 |
# File 'lib/table.rb', line 21 def columns @columns end |
#rows ⇒ string[][]
Gets the rows of the table. The rows are two dimensional arrays.
27 28 29 |
# File 'lib/table.rb', line 27 def rows @rows end |
#to_s ⇒ Object
Converts the table to the markdown equivalent string
47 48 49 50 51 52 53 54 55 56 |
# 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 |