Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/bmg/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ def infer_headers(from)
attrlist ? output_preferences.order_attrlist(attrlist) : nil
end

def infer_formats(relation, headers)
relation
.page([], 1, page_size: 100)
.summarize([], headers.each_with_object({}){|h,memo|
memo[h] = Summarizer.multiple({
:count => Summarizer.count,
:min => Summarizer.min(h.to_sym),
:max => Summarizer.max(h.to_sym),
:distinct => Summarizer.distinct(h.to_sym),
:types => Summarizer.by_proc{|t,memo| ((memo || []) << t[h.to_sym].class).uniq },
:length_80 => Summarizer.percentile(80){|t| t[h.to_sym].to_s.size }
})
memo
})
.tap{|r| puts JSON.pretty_generate(r.one) }
end

end # module Writer
end # module Bmg
require_relative 'writer/csv'
3 changes: 2 additions & 1 deletion lib/bmg/writer/xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ def _call(relation, path)
@workbook = WriteXLSX.new(path)
@worksheet = workbook.add_worksheet

headers = infer_headers(relation.type)
headers, formats = infer_headers(relation.type), nil
relation.each_with_index do |tuple,i|
headers = infer_headers(tuple) if headers.nil?
formats = infer_formats(relation, headers) if formats.nil?
headers.each_with_index do |h,i|
worksheet.write_string(0, i, h)
end if i == 0
Expand Down
3 changes: 2 additions & 1 deletion spec/unit/writer/test_xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ module Writer
let(:relation) {
Relation.new [
{ id: 1, name: "Bernard", nonnum: "1", when: date },
{ id: 2, name: "Yoann", nonnum: "2", when: date }
{ id: 2, name: "Yoann", nonnum: "2", when: date },
{ id: 3, name: "Louis", nonnum: "1", when: date+10 }
]
}

Expand Down