Class: Tap::Support::SimpleTable
- Inherits:
-
Object
- Object
- Tap::Support::SimpleTable
- Defined in:
- lib/tap/support/simple_table.rb
Overview
SimpleTable represents a simple table where each row has the same number of columns. Provides accessors of column data, and supports headers.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
An array of table data.
-
#default_value ⇒ Object
readonly
The default value for empty cells.
-
#headers ⇒ Object
readonly
An array of headers, padded to n_columns.
-
#n_columns ⇒ Object
readonly
The number of columns in self.
Class Method Summary collapse
-
.parse_data(string, row_delimiter = /\r?\n/, col_delimiter = "\t") ⇒ Object
Parses the string into an array of table data, using the specified row and column delimiters.
Instance Method Summary collapse
-
#blank_row ⇒ Object
Returns an array of n_columns length and default_value.
-
#column(index) ⇒ Object
Returns column data for the specified index.
-
#column_by_header(header) ⇒ Object
Returns column data for the specified header.
-
#initialize(data, options = {}) ⇒ SimpleTable
constructor
A new instance of SimpleTable.
- #join(row_delimiter = "\n", col_delimiter = "\t", include_headers = true) ⇒ Object
-
#n_rows ⇒ Object
The number of rows in self.
- #normalize! ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ SimpleTable
Returns a new instance of SimpleTable.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/tap/support/simple_table.rb', line 33 def initialize(data, ={}) @data = data @default_value = [:default_value] || nil @headers = case [:header_row] when true if @data.length == 0 raise "no header row available" end @data.shift else [] end normalize! end |
Instance Attribute Details
#data ⇒ Object (readonly)
An array of table data. Each entry (row) is an array of column data padded to a uniform length (n_columns) using default_value.
22 23 24 |
# File 'lib/tap/support/simple_table.rb', line 22 def data @data end |
#default_value ⇒ Object (readonly)
The default value for empty cells
25 26 27 |
# File 'lib/tap/support/simple_table.rb', line 25 def default_value @default_value end |
#headers ⇒ Object (readonly)
An array of headers, padded to n_columns.
31 32 33 |
# File 'lib/tap/support/simple_table.rb', line 31 def headers @headers end |
#n_columns ⇒ Object (readonly)
The number of columns in self
28 29 30 |
# File 'lib/tap/support/simple_table.rb', line 28 def n_columns @n_columns end |
Class Method Details
.parse_data(string, row_delimiter = /\r?\n/, col_delimiter = "\t") ⇒ Object
Parses the string into an array of table data, using the specified row and column delimiters.
13 14 15 16 17 |
# File 'lib/tap/support/simple_table.rb', line 13 def parse_data(string, row_delimiter=/\r?\n/, col_delimiter="\t") string.split(row_delimiter).collect do |row| row.split(col_delimiter) end end |
Instance Method Details
#blank_row ⇒ Object
Returns an array of n_columns length and default_value
68 69 70 |
# File 'lib/tap/support/simple_table.rb', line 68 def blank_row Array.new(n_columns, default_value) end |
#column(index) ⇒ Object
Returns column data for the specified index.
73 74 75 |
# File 'lib/tap/support/simple_table.rb', line 73 def column(index) data.collect {|row| row[index] } end |
#column_by_header(header) ⇒ Object
Returns column data for the specified header.
78 79 80 81 82 83 |
# File 'lib/tap/support/simple_table.rb', line 78 def column_by_header(header) index = headers.index(header) raise "could not find header: #{header}" if index == nil column(index) end |
#join(row_delimiter = "\n", col_delimiter = "\t", include_headers = true) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/tap/support/simple_table.rb', line 85 def join(row_delimiter="\n", col_delimiter="\t", include_headers=true) (include_headers ? headers.join(col_delimiter) + row_delimiter : "") + data.collect do |row| row.join(col_delimiter) end.join(row_delimiter) end |
#n_rows ⇒ Object
The number of rows in self.
63 64 65 |
# File 'lib/tap/support/simple_table.rb', line 63 def n_rows @data.length end |
#normalize! ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/tap/support/simple_table.rb', line 49 def normalize! # Determine the number of columns in self. @n_columns = data.inject(0) do |max, column| max > column.length ? max : column.length end # Normalize the data rows to the number of columns. @data = data.collect {|row| normalize_row(row) } @headers = normalize_row(@headers) self end |