lib/cli_format/presenter/space.rb in cli-format-0.3.0 vs lib/cli_format/presenter/space.rb in cli-format-0.4.0

- old
+ new

@@ -1,19 +1,19 @@ class CliFormat::Presenter class Space < Base - def text - max_widths = calculate_max_widths + extend Memoist + def text @buffer << format_row(@header, max_widths) if @header @rows.each do |row| @buffer << format_row(row, max_widths) end @buffer.join("\n") end - def calculate_max_widths + def max_widths all_rows = [@header, *@rows].compact max_widths = Array.new(all_rows[0].size, 0) all_rows.each do |row| row.each_with_index do |col, idx| @@ -21,15 +21,21 @@ end end max_widths end + memoize :max_widths def format_row(row, max_widths) formatted_row = row.each_with_index.map do |col, idx| idx == row.size - 1 ? col.to_s : col.to_s.ljust(max_widths[idx]) end - formatted_row.join(" ") + formatted_row.join(formatted_row_separator) + end + + # interface method + def formatted_row_separator + " " end end end