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 = [], colors = {}) ⇒ Table

Returns a new instance of Table.



13
14
15
16
17
18
19
20
21
# File 'lib/ppl/format/table.rb', line 13

def initialize(columns=[], colors={})
  @columns   = columns
  @rows      = []
  @separator = SEPARATOR_SPACES
  @colors    = colors
  @color_adapter = Ppl::Adapter::Color::Colored.new
  @column_widths = {}
  @columns.each { |c| @column_widths[c] = 0 }
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



11
12
13
# File 'lib/ppl/format/table.rb', line 11

def colors
  @colors
end

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#separatorObject

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#add_row(row = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/ppl/format/table.rb', line 23

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

#disable_colors!Object



40
41
42
# File 'lib/ppl/format/table.rb', line 40

def disable_colors!
  @colors = {}
end

#to_sObject



34
35
36
37
38
# File 'lib/ppl/format/table.rb', line 34

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