Class: FileBuilders::CSVFileBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/file_builders/csv_file_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, file_name, options) ⇒ CSVFileBuilder

Returns a new instance of CSVFileBuilder.



7
8
9
10
11
# File 'lib/file_builders/csv_file_builder.rb', line 7

def initialize(data, file_name, options)
  @data = data
  @file_name = file_name
  @options = options
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/file_builders/csv_file_builder.rb', line 5

def data
  @data
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



5
6
7
# File 'lib/file_builders/csv_file_builder.rb', line 5

def file_name
  @file_name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/file_builders/csv_file_builder.rb', line 5

def options
  @options
end

Instance Method Details

#export_to_fileObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/file_builders/csv_file_builder.rb', line 13

def export_to_file
  titles_row = (options[:titles].join(';').concat(';').concat("\n") if options[:titles].size.positive?)

  data_without_options = data.map { |row| row[0...-1] }
  csv_data = data_without_options.map { |row| row.join(';').concat(';') }.join("\n")

  File.open(file_name, 'a') do |file|
    file << titles_row
    file << csv_data
  end
end