Sha256: 109251bfa765303c9e3b9740dabd0b91dc875d0f614fa5a3d02e9a19708c1a46

Contents?: true

Size: 927 Bytes

Versions: 1

Compression:

Stored size: 927 Bytes

Contents

require 'csv'

module CsvRowModel
  module Export
    class Csv
      attr_reader :export_model_class, :csv, :file

      # @param [Export] export_model export model class
      def initialize(export_model_class)
        @export_model_class = export_model_class
        @file = Tempfile.new("#{export_model_class}.csv")
      end

      def header
        export_model_class.column_headers
      end

      def append_header
        csv << header
      end

      def append_model(model, context={})
        export_model_class.new(model, context).to_rows.each do |row|
          csv << row
        end
      end

      def generate(with_header: true)
        CSV.open(file.path,"wb") do |csv|
          @csv = csv
          append_header if with_header && !export_model_class.single_model?
          yield self
        end
      ensure
        @csv = nil
      end

      def to_s
        file.read
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
csv_row_model-0.1.0 lib/csv_row_model/export/csv.rb