lib/riif/iif.rb in riif-0.1.0 vs lib/riif/iif.rb in riif-0.2.0

- old
+ new

@@ -21,33 +21,35 @@ require 'riif/dsl/vtype' module Riif class IIF def initialize(&block) - @output = { - headers: [], - rows: [] - } + @output = {} if block_given? instance_eval(&block) end end def output CSV.generate(col_sep: "\t") do |tsv| - @output[:headers].uniq.each do |header| - tsv << header + + @output.each do |_, list| + list[:headers].uniq.each do |header| + tsv << header + end + list[:rows].each do |row| + tsv << row + end end - @output[:rows].each do |row| - tsv << row - end end end def method_missing(method_name, *args, &block) result = eval("::Riif::DSL::#{method_name.capitalize}").new.build(&block) - @output[:headers].concat(result[:headers]) - @output[:rows].concat(result[:rows]) + @output[method_name] ||= { headers: [], rows: [] } + + @output[method_name][:headers].concat(result[:headers]) + @output[method_name][:rows].concat(result[:rows]) end end end