Class: RTables::TableBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rtables/tablebuilder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTableBuilder

Returns a new instance of TableBuilder.



3
4
5
6
7
8
9
# File 'lib/rtables/tablebuilder.rb', line 3

def initialize
  @table_header = []
  @table_content = []

  @columns = 0
  @rows = 0
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



11
12
13
# File 'lib/rtables/tablebuilder.rb', line 11

def columns
  @columns
end

#rowsObject (readonly)

Returns the value of attribute rows.



11
12
13
# File 'lib/rtables/tablebuilder.rb', line 11

def rows
  @rows
end

#table_contentObject (readonly)

Returns the value of attribute table_content.



11
12
13
# File 'lib/rtables/tablebuilder.rb', line 11

def table_content
  @table_content
end

#table_headerObject (readonly)

Returns the value of attribute table_header.



11
12
13
# File 'lib/rtables/tablebuilder.rb', line 11

def table_header
  @table_header
end

Instance Method Details

#add_column(name) ⇒ Object



13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
# 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) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rtables/tablebuilder.rb', line 32

def column_exist?(name)
  @table_header.include?(name)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rtables/tablebuilder.rb', line 40

def empty?
  @columns == 0 && @rows == 0
end

#inspectObject



56
57
58
# File 'lib/rtables/tablebuilder.rb', line 56

def inspect
  "<Table columns=#{@columns} rows=#{@rows}>"
end

#raise_if_emptyObject



36
37
38
# 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

#renderObject



44
45
46
# File 'lib/rtables/tablebuilder.rb', line 44

def render
  fail TableFormatError, 'This table does not generate any output.'
end

#to_sObject



48
49
50
51
52
53
54
# File 'lib/rtables/tablebuilder.rb', line 48

def to_s
  raise_if_empty

  lines = render

  lines.join("\n")
end