Class: Datev::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/datev/export.rb

Direct Known Subclasses

AccountExport, BookingExport, ContactExport

Constant Summary collapse

CSV_OPTIONS =
{ col_sep: ';', encoding: 'windows-1252' }

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header_attributes) ⇒ Export

Returns a new instance of Export.



9
10
11
12
# File 'lib/datev/export.rb', line 9

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

Class Attribute Details

.header_classObject

Returns the value of attribute header_class.



6
7
8
# File 'lib/datev/export.rb', line 6

def header_class
  @header_class
end

.row_classObject

Returns the value of attribute row_class.



6
7
8
# File 'lib/datev/export.rb', line 6

def row_class
  @row_class
end

Instance Method Details

#<<(attributes) ⇒ Object



14
15
16
# File 'lib/datev/export.rb', line 14

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

#to_file(filename) ⇒ Object



29
30
31
32
33
# File 'lib/datev/export.rb', line 29

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

#to_sObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/datev/export.rb', line 18

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