diff --git a/lib/bmg/writer.rb b/lib/bmg/writer.rb index 02446cf..8fd229c 100644 --- a/lib/bmg/writer.rb +++ b/lib/bmg/writer.rb @@ -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' diff --git a/lib/bmg/writer/xlsx.rb b/lib/bmg/writer/xlsx.rb index 0789b03..626ddc3 100644 --- a/lib/bmg/writer/xlsx.rb +++ b/lib/bmg/writer/xlsx.rb @@ -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 diff --git a/spec/unit/writer/test_xlsx.rb b/spec/unit/writer/test_xlsx.rb index 7096217..2cf6dbc 100644 --- a/spec/unit/writer/test_xlsx.rb +++ b/spec/unit/writer/test_xlsx.rb @@ -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 } ] }