Skip to content

Instantly share code, notes, and snippets.

@lessless
Created January 6, 2015 13:09
Show Gist options
  • Save lessless/d40c3b9b06d87fce69e1 to your computer and use it in GitHub Desktop.
Save lessless/d40c3b9b06d87fce69e1 to your computer and use it in GitHub Desktop.
class ReportFormatter
class << self
def ascii(data)
new.ascii(data)
end
end
# {first_table: [{title: "contents"}], second_table: []}
def ascii(data)
data.map do |k, v|
@headings = headings(v)
Terminal::Table.new title: title(k), headings: @headings, rows: v
end
end
private
def headings(rows)
rows.map(&:keys).flatten.uniq.map { |h| title(h) }
end
def title(label)
label.to_s.gsub("_", " ").capitlize
end
def rows(data)
data.reduce([]) { |a, e| a << e.values if e.keys == @headings }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment