Class: Clin::Text::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/clin/text/table.rb

Overview

Helper class to display tables

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(col_delim: ' | ', row_delim: '-', border: true, align: :left, separate_blank: true, &block) ⇒ Table

Create a new table

Parameters:

  • col_delim (String) (defaults to: ' | ')

    Set the column delimiter @see #column_delimiters

  • row_delim (String) (defaults to: '-')

    Set the row delimiter @see #row_delim

  • border (String) (defaults to: true)

    Set border @see #border

  • align (String) (defaults to: :left)

    Set alignment @see #align

  • separate_blank (Boolean) (defaults to: true)

    If the column separator should be added near blank cells

  • block (Proc)

    Block with self passed as param.



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

#alignmentObject

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

#borderObject

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_delimitersObject

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_lengthObject

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.

Parameters:

  • cells (Array<String>)

    Cells.



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_delimObject

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

#rowsObject

List of rows in the table



12
13
14
# File 'lib/clin/text/table.rb', line 12

def rows
  @rows
end

#separate_blankObject

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 “`

Parameters:

  • args (Array|Symbol)

    List of alignment.

See Also:



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

Returns:

  • (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

Parameters:

  • cells (Array<String>)

    Cells.



62
63
64
# File 'lib/clin/text/table.rb', line 62

def row(*cells)
  @rows << TableRow.new(self, cells)
end

#separate_blank?Boolean

Returns:

  • (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

Parameters:

  • char (Char) (defaults to: nil)

    Separator char. If nil will use the default row delimiter



75
76
77
# File 'lib/clin/text/table.rb', line 75

def separator(char = nil)
  @rows << TableSeparatorRow.new(self, char)
end

#to_sObject



121
122
123
# File 'lib/clin/text/table.rb', line 121

def to_s
  to_text.to_s
end

#to_textObject

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_borderObject



105
106
107
# File 'lib/clin/text/table.rb', line 105

def vertical_border
  '|'
end