Class: Remi::DataTarget::CsvFile

Inherits:
Object
  • Object
show all
Includes:
Remi::DataTarget
Defined in:
lib/remi/data_target/csv_file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Remi::DataSubject

#df, #df=, #fields

Constructor Details

#initialize(path:, csv_options: {}, logger: Remi::Settings.logger) ⇒ CsvFile

Returns a new instance of CsvFile.



17
18
19
20
21
# File 'lib/remi/data_target/csv_file.rb', line 17

def initialize(path:, csv_options: {}, logger: Remi::Settings.logger)
  @path = path
  @csv_options = self.class.default_csv_options.merge(csv_options)
  @logger = logger
end

Instance Attribute Details

#csv_optionsObject (readonly)

Returns the value of attribute csv_options.



24
25
26
# File 'lib/remi/data_target/csv_file.rb', line 24

def csv_options
  @csv_options
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/remi/data_target/csv_file.rb', line 23

def path
  @path
end

Class Method Details

.default_csv_optionsObject



6
7
8
9
10
11
12
13
14
15
# File 'lib/remi/data_target/csv_file.rb', line 6

def self.default_csv_options
  CSV::DEFAULT_OPTIONS.merge({
    headers: true,
    header_converters: Remi::FieldSymbolizers[:standard],
    col_sep: ',',
    encoding: 'UTF-8',
    quote_char: '"',
    row_sep: "\n"
  })
end

Instance Method Details

#field_symbolizerObject



26
27
28
# File 'lib/remi/data_target/csv_file.rb', line 26

def field_symbolizer
  self.class.default_csv_options[:header_converters]
end

#loadObject



30
31
32
33
34
35
36
37
38
# File 'lib/remi/data_target/csv_file.rb', line 30

def load
  return true if @loaded || df.size == 0

  @logger.info "Writing CSV file #{@path}"

  df.write_csv @path, @csv_options

  @loaded = true
end