Class: TextLayout::Table

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

Defined Under Namespace

Classes: Cell, Span

Instance Method Summary collapse

Constructor Details

#initialize(table, options = {}) ⇒ Table

Returns a new instance of Table.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/text_layout/table.rb', line 2

def initialize(table, options = {})
  @table = table
  @options = {
    :align => :auto,
    :col_border => "|",
    :row_border => "-",
    :cross => "+",
    :border => false,
    :padding => " "
  }.merge(options)

  if @options[:border] == true
    @options[:border] = [:top, :bottom, :left, :right, :cell].inject({}){|r, i|r[i] = true;r}
  end
end

Instance Method Details

#cell_formatObject



22
23
24
# File 'lib/text_layout/table.rb', line 22

def cell_format
  @cell_format ||= @options[:padding] + "%s" + @options[:padding]
end

#column_border_widthObject



18
19
20
# File 'lib/text_layout/table.rb', line 18

def column_border_width
  @column_border_width ||= (@options[:padding] + @options[:col_border] + @options[:padding]).display_width
end

#layoutObject



46
47
48
49
50
51
# File 'lib/text_layout/table.rb', line 46

def layout
  unknot
  calculate_cell_size
  expand_cell_size
  build_string.join("\n")
end

#line_formatObject



26
27
28
# File 'lib/text_layout/table.rb', line 26

def line_format
  @line_format ||= @options[:col_border] + "%s" + @options[:col_border]
end