lib/terminal-table/table.rb in visionmedia-terminal-table-1.0.4 vs lib/terminal-table/table.rb in visionmedia-terminal-table-1.0.5

- old
+ new

@@ -44,34 +44,43 @@ ## # Generates a ASCII table. def initialize options = {}, &block - @headings = options.delete(:headings) || [] - @rows = options.delete(:rows) || [] + @headings = options.fetch :headings, [] + @rows = options.fetch :rows, [] yield_or_eval &block if block_given? end ## # Render the table. Often you will simply call _puts_ with an # instance in order to output it to the terminal. def render - sep = seperator - s = sep + "\n" - s << Y + headings.collect_with_index { |h, i| Heading.new(length_of_column(i), h).render }.join(Y) + Y if has_headings? - s << "\n" + sep + "\n" if has_headings? - s << rows.collect { |row| Y + row.collect_with_index { |c, i| Cell.new(length_of_column(i), c).render }.join(Y) + Y }.join("\n") - s << "\n" + sep + "\n" + buffer = seperator << "\n" + if has_headings? + buffer << Y + headings.map_with_index do |heading, i| + Heading.new(length_of_column(i), *heading).render + end.join(Y) + Y + buffer << "\n#{seperator}\n" + end + buffer << rows.map do |row| + Y + row.map_with_index do |cell, i| + Cell.new(length_of_column(i), *cell).render + end.join(Y) + Y + end.join("\n") + buffer << "\n#{seperator}\n" end alias :to_s :render ## # Create a seperator based on colum lengths. def seperator - I + columns.collect_with_index { |col, i| X * (length_of_column(i) + 2) }.join(I) + I + I + columns.collect_with_index do |col, i| + X * (length_of_column(i) + 2) + end.join(I) + I end ## # Add a row. @@ -89,32 +98,32 @@ ## # Return +n+ column. def column n - rows.collect { |row| row[n] }.compact + rows.map { |row| row[n] }.compact end ## # Return +n+ column including headings. def column_with_headings n - headings_with_rows.collect { |row| row[n] }.compact + headings_with_rows.map { |row| row[n] }.compact end ## # Return columns. def columns - (0..number_of_columns-1).collect { |n| column n } + (0..number_of_columns-1).map { |n| column n } end ## # Return the largest cell found within column +n+. def largest_cell_in_column n - column_with_headings(n).sort_by { |c| Cell.new(0, c).length }.last + column_with_headings(n).sort_by { |cell| Cell.new(0, *cell).length }.last end ## # Return length of column +n+. @@ -124,28 +133,21 @@ ## # Return total number of columns available. def number_of_columns - if rows[0] - rows[0].length - else - raise Error, 'Your table needs some rows' - end + return rows.first.length unless rows.empty? + raise Error, 'your table needs some rows.' end ## # Align column +n+ to +alignment+ of :center, :left, or :right. def align_column n, alignment - column(n).each_with_index do |c, i| - unless c.is_a? Hash - @rows[i][n] = { :value => c, :align => alignment } - end + column(n).each_with_index do |col, i| + @rows[i][n] = [col, alignment] unless col.is_a? Array end end - - private ## # Return headings combined with rows. def headings_with_rows \ No newline at end of file