Class: Dashes::Table
Instance Method Summary collapse
- #align(*alignments) ⇒ Object
-
#initialize ⇒ Table
constructor
A new instance of Table.
- #max_width(max_width) ⇒ Object
- #row(*data) ⇒ Object
- #separator ⇒ Object
- #spacing(*spacing) ⇒ Object
- #to_s ⇒ Object
- #total_width ⇒ Object
- #width(width) ⇒ Object
Constructor Details
#initialize ⇒ Table
Returns a new instance of Table.
44 45 46 47 48 49 50 51 |
# File 'lib/dashes.rb', line 44 def initialize @rows = [] @separators = [] @aligns = [] @width = nil @max_width = nil @spacing = nil end |
Instance Method Details
#align(*alignments) ⇒ Object
61 62 63 |
# File 'lib/dashes.rb', line 61 def align(*alignments) @aligns = alignments end |
#max_width(max_width) ⇒ Object
69 70 71 |
# File 'lib/dashes.rb', line 69 def max_width(max_width) @max_width = max_width end |
#row(*data) ⇒ Object
53 54 55 |
# File 'lib/dashes.rb', line 53 def row(*data) @rows << data.map(&:to_s) end |
#separator ⇒ Object
57 58 59 |
# File 'lib/dashes.rb', line 57 def separator @separators << @rows.length end |
#spacing(*spacing) ⇒ Object
73 74 75 |
# File 'lib/dashes.rb', line 73 def spacing(*spacing) @spacing = spacing end |
#to_s ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/dashes.rb', line 77 def to_s return '' if @rows.empty? widths = col_widths format = [] separator = "+-#{widths.map {|w| '-'*w}.join('-+-')}-+" format << separator @rows.each_with_index do |row, index| @separators.select { |s| s == index }.count.times { format << separator } line = [] row.each_with_index do |data, col| line << cell(data, widths[col], @aligns[col] || :left) end format << "| #{line.join(' | ')} |" end @separators.select { |s| s == @rows.length }.count.times { format << separator } format << separator format.join("\n") end |
#total_width ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dashes.rb', line 96 def total_width if @rows.empty? 0 elsif @width @width else width = col_widths.reduce { |a,b| a+b } cols = @rows.first.size twidth = width + (2 * cols) + (cols + 1) @max_width ? [twidth, @max_width].min : twidth end end |
#width(width) ⇒ Object
65 66 67 |
# File 'lib/dashes.rb', line 65 def width(width) @width = width end |