Class: Clin::Text::Table
- Inherits:
-
Object
- Object
- Clin::Text::Table
- Defined in:
- lib/clin/text/table.rb
Overview
Helper class to display tables
Instance Attribute Summary collapse
-
#alignment ⇒ Object
Column alignment.
-
#border ⇒ Object
Boolean if yes or no outside border shall be included.
-
#column_delimiters ⇒ Object
Column delimiters Can be either: * 1 string: All the column delimiter will use this.
-
#column_length ⇒ Object
Returns the value of attribute column_length.
-
#header(*cells) ⇒ Object
Set or get the header row.
-
#row_delim ⇒ Object
Global Row delimiter All the separator will default to this value.
-
#rows ⇒ Object
List of rows in the table.
-
#separate_blank ⇒ Object
If blank cell should be separated with the column separator.
Instance Method Summary collapse
-
#align(*args) ⇒ Object
Set or get the the column alignment “‘ t.align :center # => All column will be centered t.align :left, :center, :right #=> First column will be align left, second center, …
- #border? ⇒ Boolean
-
#column_delimiter(*args) ⇒ Object
Set a specific column delimiter for all the column.
- #delimiter_at(index) ⇒ Object
-
#initialize(col_delim: ' | ', row_delim: '-', border: true, align: :left, separate_blank: true, &block) ⇒ Table
constructor
Create a new table.
-
#row(*cells) ⇒ Object
Add a new row.
- #separate_blank? ⇒ Boolean
-
#separator(char = nil) ⇒ Object
Add a separator row.
- #to_s ⇒ Object
-
#to_text ⇒ Object
Build the text object for this table.
- #update_column_length(index, cell_length) ⇒ Object
- #vertical_border ⇒ Object
Constructor Details
#initialize(col_delim: ' | ', row_delim: '-', border: true, align: :left, separate_blank: true, &block) ⇒ Table
Create a new table
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/clin/text/table.rb', line 47 def initialize(col_delim: ' | ', row_delim: '-', border: true, align: :left, separate_blank: true, &block) @rows = [] @header = nil @column_length = {} @column_delimiters = col_delim @row_delim = row_delim @border = border @separate_blank = separate_blank @alignment = align block.call(self) if block_given? end |
Instance Attribute Details
#alignment ⇒ Object
Column alignment. Can either be a global value(i.e. [Symbol]) or a column specific with an array of [Symbol]
-
:left
-
:center
-
:right
33 34 35 |
# File 'lib/clin/text/table.rb', line 33 def alignment @alignment end |
#border ⇒ Object
Boolean if yes or no outside border shall be included
26 27 28 |
# File 'lib/clin/text/table.rb', line 26 def border @border end |
#column_delimiters ⇒ Object
Column delimiters Can be either:
* 1 string: All the column delimiter will use this.
* list of string: The delimiters will be this list of string.
The size must be the column size - 1
19 20 21 |
# File 'lib/clin/text/table.rb', line 19 def column_delimiters @column_delimiters end |
#column_length ⇒ Object
Returns the value of attribute column_length.
38 39 40 |
# File 'lib/clin/text/table.rb', line 38 def column_length @column_length end |
#header(*cells) ⇒ Object
Set or get the header row.
68 69 70 71 |
# File 'lib/clin/text/table.rb', line 68 def header(*cells) @header = TableRow.new(self, cells) if cells.any? @header end |
#row_delim ⇒ Object
Global Row delimiter All the separator will default to this value
23 24 25 |
# File 'lib/clin/text/table.rb', line 23 def row_delim @row_delim end |
#rows ⇒ Object
List of rows in the table
12 13 14 |
# File 'lib/clin/text/table.rb', line 12 def rows @rows end |
#separate_blank ⇒ Object
If blank cell should be separated with the column separator.
36 37 38 |
# File 'lib/clin/text/table.rb', line 36 def separate_blank @separate_blank end |
Instance Method Details
#align(*args) ⇒ Object
Set or get the the column alignment “‘ t.align :center # => All column will be centered t.align :left, :center, :right #=> First column will be align left, second center, … t.align []:left, :center, :right] #=> Equivalent “`
87 88 89 90 |
# File 'lib/clin/text/table.rb', line 87 def align(*args) @alignment = sym_or_array(*args) if args.any? @alignment end |
#border? ⇒ Boolean
97 98 99 |
# File 'lib/clin/text/table.rb', line 97 def border? @border end |
#column_delimiter(*args) ⇒ Object
Set a specific column delimiter for all the column
93 94 95 |
# File 'lib/clin/text/table.rb', line 93 def column_delimiter(*args) @column_delimiters = sym_or_array(*args) end |
#delimiter_at(index) ⇒ Object
130 131 132 133 134 135 136 137 |
# File 'lib/clin/text/table.rb', line 130 def delimiter_at(index) return '' if index >= @column_length.size - 1 if @column_delimiters.is_a? Array @column_delimiters[index] else @column_delimiters end end |
#row(*cells) ⇒ Object
Add a new row
62 63 64 |
# File 'lib/clin/text/table.rb', line 62 def row(*cells) @rows << TableRow.new(self, cells) end |
#separate_blank? ⇒ Boolean
101 102 103 |
# File 'lib/clin/text/table.rb', line 101 def separate_blank? separate_blank end |
#separator(char = nil) ⇒ Object
Add a separator row
75 76 77 |
# File 'lib/clin/text/table.rb', line 75 def separator(char = nil) @rows << TableSeparatorRow.new(self, char) end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/clin/text/table.rb', line 121 def to_s to_text.to_s end |
#to_text ⇒ Object
Build the text object for this table.
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/clin/text/table.rb', line 110 def to_text t = Clin::Text.new unless @header.nil? t.line @header.to_s t.line TableSeparatorRow.new(self).to_s end t.lines @rows.map(&:to_s) add_border(t) if border? t end |
#update_column_length(index, cell_length) ⇒ Object
125 126 127 128 |
# File 'lib/clin/text/table.rb', line 125 def update_column_length(index, cell_length) @column_length[index] ||= 0 @column_length[index] = [cell_length, @column_length[index]].max end |
#vertical_border ⇒ Object
105 106 107 |
# File 'lib/clin/text/table.rb', line 105 def vertical_border '|' end |