Sha256: a1c1809d716b93e73ce239149a80e91058892c3a3a236bc8bf06d9a5dd1f5b0c

Contents?: true

Size: 828 Bytes

Versions: 1

Compression:

Stored size: 828 Bytes

Contents

require 'csv'
require 'datev/header'

module Datev
  class Export
    CSV_OPTIONS = { :col_sep => ';', :encoding => 'windows-1252' }

    class << self
      attr_accessor :header_class, :row_class
    end

    def initialize(header_attributes)
      @header = self.class.header_class.new(header_attributes)
      @rows = []
    end

    def <<(attributes)
      @rows << self.class.row_class.new(attributes)
    end

    def to_s
      CSV.generate(CSV_OPTIONS) do |csv|
        write(csv)
      end
    end

    def to_file(filename)
      CSV.open(filename, 'wb', CSV_OPTIONS) do |csv|
        write(csv)
      end
    end

  private

    def write(csv)
      csv << @header.output
      csv << self.class.row_class.fields.map(&:name)

      @rows.each do |row|
        csv << row.output(@header)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datev-0.3.0 lib/datev/export.rb