Sha256: 85d97d1275a10f20d7474b437808d1078a48b8c33386820dc785539365c5f9a0

Contents?: true

Size: 742 Bytes

Versions: 1

Compression:

Stored size: 742 Bytes

Contents

require 'csv'
require 'datev/header'
require 'datev/booking'

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

    def initialize(header_attributes)
      @header = Header.new header_attributes
      @rows = []
    end

    def <<(attributes)
      @rows << Datev::Booking.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 << Booking.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.2.3 lib/datev/export.rb