Module: CSVDecision::Header

Defined in:
lib/csv_decision/header.rb

Overview

Parse the CSV file's header row. These methods are only required at table load time.

Constant Summary

COLUMN_TYPE =

Column types recognise din the header row.

%r{
  \A(?<type>in|out|in/text|out/text)
  \s*:\s*(?<name>\S?.*)\z
}xi
COLUMN_NAME =

Regular expression string for a column name. More lenient than a Ruby method name - note any spaces will have been replaced with underscores.

"\\w[\\w:/!?]*"
COLUMN_NAME_RE =

Column name regular expression.

Matchers.regexp(COLUMN_NAME)

Class Method Summary collapse

Class Method Details

.dictionary(row:) ⇒ Hash<Hash>

Classify and build a dictionary of all input and output columns.

Parameters:

  • row (Array<String>)

    The header row after removing any empty columns.

Returns:

  • (Hash<Hash>)

    Column dictionary is a hash of hashes.



57
58
59
60
61
62
63
64
65
# File 'lib/csv_decision/header.rb', line 57

def self.dictionary(row:)
  dictionary = Columns::Dictionary.new

  row.each_with_index do |cell, index|
    dictionary = parse_cell(cell: cell, index: index, dictionary: dictionary)
  end

  dictionary
end

.row?(row) ⇒ Boolean

Check if the given row contains a recognisable header cell.

Parameters:

  • row (Array<String>)

    Header row.

Returns:

  • (Boolean)

    Return true if the row looks like a header.



36
37
38
# File 'lib/csv_decision/header.rb', line 36

def self.row?(row)
  row.find { |cell| cell.match(COLUMN_TYPE) }
end

.strip_empty_columns(rows:) ⇒ Array<Array<String>>

Strip empty columns from all data rows.

Parameters:

  • rows (Array<Array<String>>)

    Data rows.

Returns:

  • (Array<Array<String>>)

    Data array after removing any empty columns and the header row.



45
46
47
48
49
50
51
# File 'lib/csv_decision/header.rb', line 45

def self.strip_empty_columns(rows:)
  empty_cols = empty_columns?(row: rows.first)
  Data.strip_columns(data: rows, empty_columns: empty_cols) if empty_cols

  # Remove header row from the data array.
  rows.shift
end