class Gemat::Formatter

Public Class Methods

new(gems, columns, write_path: nil) click to toggle source
# File lib/formatter.rb, line 5
def initialize(gems, columns, write_path: nil)
  @gems = gems
  @columns = columns
  @write_path = write_path
  @rows = []
  set_column_name_length
  gen_rows
end

Public Instance Methods

run() click to toggle source
# File lib/formatter.rb, line 14
def run
  if @write_path
    File.open(@write_path, 'w') do |file|
      each_write(@rows) { |string| file << string }
    end
  else
    print "\n\n"
    each_write(@rows) { |string| print string }
    print "\n"
  end
end

Private Instance Methods

each_write(rows, &proc) click to toggle source
# File lib/formatter.rb, line 28
def each_write(rows, &proc)
  rows.each do |row|
    proc.call(row)
    proc.call("\n")
  end
end
set_column_name_length() click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/formatter.rb, line 36
def set_column_name_length
  rows = []
  rows << @columns.map(&:column_name)
  @gems.each do |gem|
    rows << @columns.map { |dsl| dsl.call(gem) }
  end
  maxes = rows.transpose.map { |row| row.max { |a, b| a.to_s.length <=> b.to_s.length }.to_s.length }
  maxes.zip(@columns) do |max, column|
    column.max_length = max
  end
end