Sha256: 1826597fc6a17b1a08c58cc7f17940356c958df0a5eb677c863ef01c37aa343a

Contents?: true

Size: 862 Bytes

Versions: 1

Compression:

Stored size: 862 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])
    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]) + "\n"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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