Sha256: 46d2ab84c25fc9737fe1818cc768601bca8e1bb7cbc96534dc51f7dcb849ef2b

Contents?: true

Size: 914 Bytes

Versions: 2

Compression:

Stored size: 914 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
      string = to_csv_line(@header.output) <<
               to_csv_line(self.class.row_class.fields.map(&:name))

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

      string.encode(CSV_OPTIONS[:encoding], invalid: :replace, undef: :replace, replace: ' ')
    end

    def to_file(filename)
      File.open(filename, 'wb') do |file|
        file.write(to_s)
      end
    end

  private

    def to_csv_line(data)
      data.join(CSV_OPTIONS[:col_sep]) + "\r\n"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
datev-0.6.0 lib/datev/export.rb
datev-0.5.1 lib/datev/export.rb