Class: Remi::Encoder::CsvFile

Inherits:
Remi::Encoder show all
Includes:
DataSubject::CsvFile
Defined in:
lib/remi/data_subjects/csv_file.rb

Overview

CsvFile Encoder

Examples:

class MyJob < Remi::Job
  target :my_target do
    encoder Remi::Encoder::CsvFile.new(
      csv_options: { col_sep: '|' }
    )
    loader Remi::Loader::LocalFile.new(
      path: 'test.csv'
    )
  end
end

my_df = Daru::DataFrame.new({ a: 1.upto(5).to_a, b: 6.upto(10) })
job = MyJob.new
job.my_target.df = my_df
job.my_target.load

Instance Attribute Summary collapse

Attributes inherited from Remi::Encoder

#context, #field_symbolizer, #fields, #logger

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kargs, &block) ⇒ CsvFile

Returns a new instance of CsvFile.

Parameters:

  • work_path (String, Pathname)

    Path to a directory used to temporarily store CSV files (default: Settings.work_dir)

  • csv_options (Hash)

    Standard Ruby CSV parser options.



151
152
153
154
# File 'lib/remi/data_subjects/csv_file.rb', line 151

def initialize(*args, **kargs, &block)
  super
  init_csv_file_encoder(*args, **kargs, &block)
end

Instance Attribute Details

#csv_optionsHash (readonly)

Returns Csv options hash.

Returns:

  • (Hash)

    Csv options hash



159
160
161
# File 'lib/remi/data_subjects/csv_file.rb', line 159

def csv_options
  @csv_options
end

Instance Method Details

#encode(dataframe) ⇒ Object

Converts the dataframe to a CSV file stored in the local work directory. If labels are present write the CSV file with those headers but maintain the structure of the original dataframe

Parameters:

Returns:

  • (Object)

    The path to the file



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/remi/data_subjects/csv_file.rb', line 167

def encode(dataframe)
  logger.info "Writing CSV file to temporary location #{@working_file}"

  label_columns = self.fields.reduce({}) { |h, (k, v)|
    if v[:label]
      h[k] = v[:label].to_sym
    end
    h
  }
  dataframe.rename_vectors label_columns
  dataframe.write_csv @working_file, @csv_options
  @working_file
end