Module: CSVDecision::Options

Defined in:
lib/csv_decision/options.rb

Overview

Validate and normalize the options values supplied.

Constant Summary collapse

VALID =

All valid CSVDecision::parse options with their default values.

{
  first_match: true,
  regexp_implicit: false,
  text_only: false,
  matchers: DEFAULT_MATCHERS
}.freeze
CSV_NAMES =

These options may appear in the CSV file before the header row. They get converted to a normalized option key value pair.

{
  first_match: [:first_match, true],
  accumulate: [:first_match, false],
  regexp_implicit: [:regexp_implicit, true],
  text_only: [:text_only, true]
}.freeze

Class Method Summary collapse

Class Method Details

.from_csv(rows:, options:) ⇒ Hash

Read any options supplied in the CSV file placed before the header row.

Parameters:

  • rows (Array<Array<String>>)

    Table data rows.

  • options (Hash)

    Input options hash built so far.

Returns:

  • (Hash)

    Options hash overridden with any values found in the CSV file.



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

def self.from_csv(rows:, options:)
  row = rows.first
  return options if row.nil?

  # Have we hit the header row?
  return options if Header.row?(row)

  # Scan each cell looking for valid option values
  options = scan_cells(row: row, options: options)

  rows.shift
  from_csv(rows: rows, options: options)
end

.normalize(options) ⇒ Hash

Validate options and supply default values for any options not explicitly set.

Parameters:

  • options (Hash)

    Input options hash supplied by the user.

Returns:

  • (Hash)

    Options hash filled in with all required values, defaulted if necessary.

Raises:

  • (ArgumentError)

    For invalid option keys.



48
49
50
51
# File 'lib/csv_decision/options.rb', line 48

def self.normalize(options)
  validate(options)
  default(options)
end