Class: Table

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(separate_each_line, header_per_line) ⇒ Table

Returns a new instance of Table.



5
6
7
8
9
10
11
# File 'lib/clean_text_table/table.rb', line 5

def initialize( separate_each_line, header_per_line )
  @table_hsh = Hash.new
  @field_width_hsh = Hash.new
  @num_fields = 0
  @separate_each_line = separate_each_line
  @header_per_line = header_per_line
end

Instance Attribute Details

#field_width_hshObject

Returns the value of attribute field_width_hsh.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def field_width_hsh
  @field_width_hsh
end

#headerObject

Returns the value of attribute header.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def header
  @header
end

#header_per_lineObject

Returns the value of attribute header_per_line.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def header_per_line
  @header_per_line
end

#num_fieldsObject

Returns the value of attribute num_fields.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def num_fields
  @num_fields
end

#separate_each_lineObject

Returns the value of attribute separate_each_line.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def separate_each_line
  @separate_each_line
end

#table_hshObject

Returns the value of attribute table_hsh.



3
4
5
# File 'lib/clean_text_table/table.rb', line 3

def table_hsh
  @table_hsh
end

Instance Method Details

#add_header(header, sep) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/clean_text_table/table.rb', line 32

def add_header( header, sep )
  @header = header.split( sep )
  if @header.length > @num_fields
    @num_fields = @header.length
  end
  for col in 0..@header.length - 1
    curr_width = 0
    if !@header[ col ].nil?
      curr_width = @header[ col ].length
    end
    max_width = @field_width_hsh[ col ]
    if max_width.nil? || curr_width > max_width
      @field_width_hsh[ col ] = curr_width
    end
  end
end

#add_row(row, sep) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/clean_text_table/table.rb', line 13

def add_row( row, sep )
  fields = row.split( sep )
  if fields.length > @num_fields
    @num_fields = fields.length
  end
  i = @table_hsh.length
  @table_hsh[ i ] = fields 
  for col in 0..fields.length - 1
    curr_width = 0
    if !fields[ col ].nil?
      curr_width = fields[ col ].length
    end
    max_width = @field_width_hsh[ col ]
    if max_width.nil? || curr_width > max_width
      @field_width_hsh[ col ] = curr_width
    end
  end
end

#dumpObject



49
50
51
52
53
54
55
# File 'lib/clean_text_table/table.rb', line 49

def dump
  #add header
  table_str = add_header_to_table_str( "" )
  #add table contents
  table_str = add_table_contents( table_str )
  return table_str.to_s
end