Created
January 6, 2015 13:09
-
-
Save lessless/d40c3b9b06d87fce69e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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