Class: Ppl::Format::Table

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

Constant Summary collapse

SEPARATOR_SPACES =
0
SEPARATOR_TABS =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns = []) ⇒ Table

Returns a new instance of Table.



11
12
13
14
15
16
17
18
# File 'lib/ppl/format/table.rb', line 11

def initialize(columns=[])
  @columns   = columns
  @rows      = []
  @separator = SEPARATOR_SPACES

  @column_widths = {}
  @columns.each { |c| @column_widths[c] = 0 }
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



7
8
9
# File 'lib/ppl/format/table.rb', line 7

def columns
  @columns
end

#rowsObject

Returns the value of attribute rows.



8
9
10
# File 'lib/ppl/format/table.rb', line 8

def rows
  @rows
end

#separatorObject

Returns the value of attribute separator.



9
10
11
# File 'lib/ppl/format/table.rb', line 9

def separator
  @separator
end

Instance Method Details

#add_row(row = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/ppl/format/table.rb', line 20

def add_row(row={})
  row.each do |column, value|
    width     = sprintf("%s", value).length
    max_width = @column_widths[column]
    if width > max_width
      @column_widths[column] = width
    end
  end
  @rows.push(row)
end

#to_sObject



31
32
33
34
35
# File 'lib/ppl/format/table.rb', line 31

def to_s
  string = ""
  @rows.each { |row| string += format_row(row).strip + "\n" }
  string.strip
end