Sha256: ff75605e98d807bab3bda7c7f26f3eb2346e9517f936f25892f44ea4388c0a3d
Contents?: true
Size: 1.15 KB
Versions: 6
Compression:
Stored size: 1.15 KB
Contents
require 'csv' module TN::CSVBuilder module ClassMethods def csv_header(attribute, name: attribute) @csv_headers ||= {} @csv_headers[attribute] = name end attr_reader :csv_headers end module InstanceMethods def csv(io, flush_after_each_row: false) csv_header_row ||= CSV::Row.new(csv_headers.keys, csv_headers.values, true) csv = CSV.new(io) csv << csv_header_row csv_data do |row| row_data = [] csv_headers.each do |key, _| row_data << row[key] end row.each do |key, _| fail "#{key} not a valid csv header!" unless csv_headers.key?(key) end csv << CSV::Row.new(csv_headers, row_data) csv.flush if flush_after_each_row end csv end def generate_csv_tempfile(flush_after_each_row: false) file = TN::TempFile.new csv(file, flush_after_each_row: flush_after_each_row) file.close yield(file) if block_given? file end def csv_headers self.class.csv_headers end end def self.included(klass) klass.extend(ClassMethods) klass.send(:include, InstanceMethods) end end
Version data entries
6 entries across 6 versions & 1 rubygems