Module: CSVDecision::Header Private

Defined in:
lib/csv_decision/header.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

Constant Summary collapse

COLUMN_TYPE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Column types recognised in the header row.

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

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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:/!?]*"

Class Method Summary collapse

Class Method Details

.column_name?(column_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return true if column name is valid.

Parameters:

  • column_name (String)

Returns:

  • (Boolean)


30
31
32
# File 'lib/csv_decision/header.rb', line 30

def self.column_name?(column_name)
  COLUMN_NAME_RE.match?(column_name)
end

.parse(table:, matchers:) ⇒ CSVDecision::Columns

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parse the header row, and the defaults row if present.

Parameters:

Returns:



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/csv_decision/header.rb', line 59

def self.parse(table:, matchers:)
  # Parse the header row
  table.columns = CSVDecision::Columns.new(table)

  # Parse the defaults row if present
  return table.columns if table.columns.defaults.blank?

  table.columns.defaults =
    Defaults.parse(columns: table.columns, matchers: matchers.outs, row: table.rows.shift)

  table.columns
end

.row?(row) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



38
39
40
# File 'lib/csv_decision/header.rb', line 38

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.



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

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