class RTables::TableBuilder

Attributes

columns[R]
rows[R]
table_content[R]
table_header[R]

Public Class Methods

new() click to toggle source
# File lib/rtables/tablebuilder.rb, line 3
def initialize
  @table_header = []
  @table_content = []

  @columns = 0
  @rows = 0
end

Public Instance Methods

add_column(name) click to toggle source
# File lib/rtables/tablebuilder.rb, line 13
def add_column(name)
  return false if column_exist?(name)
  fail TableFormatError, 'Cannot add more columns after rows have been added.' if @rows != 0

  @table_header.push(name)
  @columns += 1
  true
end
add_row(*args) click to toggle source
# File lib/rtables/tablebuilder.rb, line 22
def add_row(*args)
  if args.count != @columns
    fail TableFormatError, "Number of arguments passed does not equal number of columns. [#{args.count} != #{@columns}]"
  end

  @table_content.push(args)
  @rows += 1
  true
end
column_exist?(name) click to toggle source
# File lib/rtables/tablebuilder.rb, line 32
def column_exist?(name)
  @table_header.include?(name)
end
empty?() click to toggle source
# File lib/rtables/tablebuilder.rb, line 40
def empty?
  @columns == 0 && @rows == 0
end
inspect() click to toggle source
# File lib/rtables/tablebuilder.rb, line 56
def inspect
  "<Table columns=#{@columns} rows=#{@rows}>"
end
raise_if_empty() click to toggle source
# File lib/rtables/tablebuilder.rb, line 36
def raise_if_empty
  fail TableFormatError, 'Table has no content to display.' if empty? || @columns == 0 || @rows == 0
end
render() click to toggle source
# File lib/rtables/tablebuilder.rb, line 44
def render
  fail TableFormatError, 'This table does not generate any output.'
end
to_s() click to toggle source
# File lib/rtables/tablebuilder.rb, line 48
def to_s
  raise_if_empty

  lines = render

  lines.join("\n")
end