Class: Legitable::Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alignment: {}, delimiter: " | ", separator: "-", style: nil, &block) ⇒ Table

Returns a new instance of Table.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/legitable/table.rb', line 7

def initialize(alignment: {}, delimiter: " | ", separator: "-", style: nil, &block)
  @rows = []
  @headers = []
  @format = {}
  @formatters = {}
  @alignment = alignment
  @delimiter = delimiter
  @separator = separator
  @style = style

  if style == :markdown
    @delimiter = " | "
    @separator = "-"
  end

  instance_eval(&block) unless block.nil?
end

Instance Attribute Details

#delimiterObject (readonly)

Returns the value of attribute delimiter.



5
6
7
# File 'lib/legitable/table.rb', line 5

def delimiter
  @delimiter
end

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/legitable/table.rb', line 5

def format
  @format
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/legitable/table.rb', line 5

def headers
  @headers
end

#rowsObject (readonly)

Returns the value of attribute rows.



5
6
7
# File 'lib/legitable/table.rb', line 5

def rows
  @rows
end

#separatorObject (readonly)

Returns the value of attribute separator.



5
6
7
# File 'lib/legitable/table.rb', line 5

def separator
  @separator
end

#styleObject (readonly)

Returns the value of attribute style.



5
6
7
# File 'lib/legitable/table.rb', line 5

def style
  @style
end

Instance Method Details

#<<(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/legitable/table.rb', line 25

def <<(data)
  case data
  when Array
    data.each { |row| self << row }
  when Hash
    initialize_headers(data) if headers.empty?
    add_row data
  when Enumerable
    data.each { |row| self << row }
  end
  self
end

#to_sObject



38
39
40
41
42
43
# File 'lib/legitable/table.rb', line 38

def to_s
  <<~EOS
    #{render_headers}
    #{render_rows}
  EOS
end