Module: CSVDecision::Data

Defined in:
lib/csv_decision/data.rb

Overview

Methods to load data from a file, CSV string or array of arrays

Constant Summary

CSV_OPTIONS =
{ encoding: 'UTF-8', skip_blanks: true }.freeze

Class Method Summary collapse

Class Method Details

.input_file?(input) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/csv_decision/data.rb', line 21

def self.input_file?(input)
  input.is_a?(Pathname) || input.is_a?(File)
end

.strip_columns(data:, empty_columns:) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/csv_decision/data.rb', line 25

def self.strip_columns(data:, empty_columns:)
  # Adjust column indices as we delete columns the rest shift to the left by 1
  empty_columns.map!.with_index { |col, index| col - index }

  # Delete all empty columns from the array of arrays
  empty_columns.each { |col| data.each_index { |row| data[row].delete_at(col) } }
end

.to_array(data:) ⇒ Object

Parse the input data which may either be a file path name, CSV string or array of arrays. Strips out empty columns/rows and comment cells



17
18
19
# File 'lib/csv_decision/data.rb', line 17

def self.to_array(data:)
  strip_rows(data: data_array(data))
end