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.



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

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.



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

def colors
  @colors
end

#columnsObject

Returns the value of attribute columns.



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

def columns
  @columns
end

#rowsObject

Returns the value of attribute rows.



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

def rows
  @rows
end

#separatorObject

Returns the value of attribute separator.



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

def separator
  @separator
end

Instance Method Details

#add_row(row = {}) ⇒ Object



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

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



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

def disable_colors!
  @colors = {}
end

#to_sObject



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

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