Class: Table
- Inherits:
-
Object
- Object
- Table
- Defined in:
- lib/clean_text_table/table.rb
Instance Attribute Summary collapse
-
#field_width_hsh ⇒ Object
Returns the value of attribute field_width_hsh.
-
#header ⇒ Object
Returns the value of attribute header.
-
#header_per_line ⇒ Object
Returns the value of attribute header_per_line.
-
#num_fields ⇒ Object
Returns the value of attribute num_fields.
-
#separate_each_line ⇒ Object
Returns the value of attribute separate_each_line.
-
#table_hsh ⇒ Object
Returns the value of attribute table_hsh.
Instance Method Summary collapse
- #add_header(header, sep) ⇒ Object
- #add_row(row, sep) ⇒ Object
- #dump ⇒ Object
-
#initialize(separate_each_line, header_per_line) ⇒ Table
constructor
A new instance of Table.
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_hsh ⇒ Object
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 |
#header ⇒ Object
Returns the value of attribute header.
3 4 5 |
# File 'lib/clean_text_table/table.rb', line 3 def header @header end |
#header_per_line ⇒ Object
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_fields ⇒ Object
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_line ⇒ Object
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_hsh ⇒ Object
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 |
#dump ⇒ Object
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 |