module WorkerTools::CsvOutput

Public Instance Methods

csv_ouput_ensure_target_folder() click to toggle source
# File lib/worker_tools/csv_output.rb, line 49
def csv_ouput_ensure_target_folder
  FileUtils.mkdir_p(cvs_output_target_folder) unless File.directory?(cvs_output_target_folder)
end
csv_output_col_sep() click to toggle source
# File lib/worker_tools/csv_output.rb, line 57
def csv_output_col_sep
  ';'
end
csv_output_column_headers() click to toggle source
# File lib/worker_tools/csv_output.rb, line 16
def csv_output_column_headers
  # These columns are used to set the headers, also
  # to set the row values depending on your implementation.
  #
  # To ignore them set it to _false_
  #
  # Ex:
  # @csv_output_column_headers ||= {
  #   foo: 'Foo Header',
  #   bar: 'Bar Header'
  # }
  raise "csv_output_column_headers has to be defined in #{self}"
end
csv_output_encoding() click to toggle source
# File lib/worker_tools/csv_output.rb, line 61
def csv_output_encoding
  Encoding::UTF_8
end
csv_output_entries() click to toggle source
# File lib/worker_tools/csv_output.rb, line 12
def csv_output_entries
  raise "csv_output_entries has to be defined in #{self}"
end
csv_output_insert_headers(csv) click to toggle source
# File lib/worker_tools/csv_output.rb, line 65
def csv_output_insert_headers(csv)
  csv << csv_output_column_headers.values if csv_output_column_headers
end
csv_output_row_values(entry) click to toggle source

rubocop:disable Lint/UnusedMethodArgument

# File lib/worker_tools/csv_output.rb, line 31
def csv_output_row_values(entry)
  # Ex:
  # {
  #   foo: entry.foo,
  #   bar: entry.bar
  # }.values_at(*csv_output_column_headers.keys)
  raise "csv_output_row_values has to be defined in #{self}"
end
csv_output_target() click to toggle source

if defined, this file will be written to this destination (regardless of whether the model saves the file as well)

# File lib/worker_tools/csv_output.rb, line 7
def csv_output_target
  # Ex: Rails.root.join('shared', 'foo', 'bar.csv')
  false
end
csv_output_target_file_name() click to toggle source
# File lib/worker_tools/csv_output.rb, line 45
def csv_output_target_file_name
  File.basename(csv_output_target)
end
csv_output_tmp_file() click to toggle source
# File lib/worker_tools/csv_output.rb, line 53
def csv_output_tmp_file
  @csv_output_tmp_file ||= Tempfile.new(['output', '.csv'])
end
csv_output_write_file() click to toggle source
# File lib/worker_tools/csv_output.rb, line 69
def csv_output_write_file
  CSV.open(csv_output_tmp_file, 'wb', col_sep: csv_output_col_sep, encoding: csv_output_encoding) do |csv|
    csv_output_insert_headers(csv)
    csv_output_entries.each { |entry| csv << csv_output_row_values(entry) }
  end
  csv_output_write_target if csv_output_target
end
csv_output_write_target() click to toggle source
# File lib/worker_tools/csv_output.rb, line 77
def csv_output_write_target
  csv_ouput_ensure_target_folder
  FileUtils.cp(csv_output_tmp_file.path, csv_output_target)
end
cvs_output_target_folder() click to toggle source

rubocop:enable Lint/UnusedMethodArgument

# File lib/worker_tools/csv_output.rb, line 41
def cvs_output_target_folder
  File.dirname(csv_output_target)
end