Sha256: 294096672bf426f9aa17e5ee445291b4898b653c75e6801287730f96fa3cb65a

Contents?: true

Size: 790 Bytes

Versions: 2

Compression:

Stored size: 790 Bytes

Contents

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

2 entries across 2 versions & 1 rubygems

Version Path
datev-0.4.0 lib/datev/export.rb
datev-0.3.1 lib/datev/export.rb