Class: Ppl::Format::Table
- Inherits:
-
Object
- Object
- Ppl::Format::Table
- Defined in:
- lib/ppl/format/table.rb
Constant Summary collapse
- SEPARATOR_SPACES =
0
- SEPARATOR_TABS =
1
Instance Attribute Summary collapse
-
#columns ⇒ Object
Returns the value of attribute columns.
-
#rows ⇒ Object
Returns the value of attribute rows.
-
#separator ⇒ Object
Returns the value of attribute separator.
Instance Method Summary collapse
- #add_row(row = {}) ⇒ Object
-
#initialize(columns = []) ⇒ Table
constructor
A new instance of Table.
- #to_s ⇒ Object
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
#columns ⇒ Object
Returns the value of attribute columns.
7 8 9 |
# File 'lib/ppl/format/table.rb', line 7 def columns @columns end |
#rows ⇒ Object
Returns the value of attribute rows.
8 9 10 |
# File 'lib/ppl/format/table.rb', line 8 def rows @rows end |
#separator ⇒ Object
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_s ⇒ Object
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 |