module CSVDecision::Dictionary

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

Public Class Methods

add_name(columns:, name:, out: false) click to toggle source

Add a new symbol to the dictionary of named input and output columns.

@param columns [{Symbol=>Symbol}] Hash of column names with key values :in or :out. @param name [Symbol] Symbolized column name. @param out [false, Index] False if an input column, otherwise the index of the output column. @return [Hash{Symbol=>[:in, Integer]}] Column dictionary updated with the new name.

# File lib/csv_decision/dictionary.rb, line 17
def self.add_name(columns:, name:, out: false)
  Validate.name(columns: columns, name: name, out: out)

  columns[name] = out ? out : :in
  columns
end
build(header:, dictionary:) click to toggle source

Classify and build a dictionary of all input and output columns by parsing the header row.

@param header [Array<String>] The header row after removing any empty columns. @param dictionary [Columns::Dictionary] Table's columns dictionary. @return [Columns::Dictionary] Table's columns dictionary.

# File lib/csv_decision/dictionary.rb, line 121
def self.build(header:, dictionary:)
  header.each_with_index do |cell, index|
    dictionary = parse_cell(cell: cell, index: index, dictionary: dictionary)
  end

  dictionary
end

Private Class Methods

dictionary_entry(dictionary:, entry:, index:) click to toggle source
# File lib/csv_decision/dictionary.rb, line 138
def self.dictionary_entry(dictionary:, entry:, index:)
  case entry.type
  # A guard column is still added to the ins hash for parsing as an input column.
  when :in, :guard, :set
    input_entry(dictionary: dictionary, entry: entry, index: index)

  when :out, :if
    output_entry(dictionary: dictionary, entry: entry, index: index)

  when :path
    dictionary.paths[index] = entry
  end

  dictionary
end
input_entry(dictionary:, entry:, index:) click to toggle source
# File lib/csv_decision/dictionary.rb, line 169
def self.input_entry(dictionary:, entry:, index:)
  dictionary.ins[index] = entry

  # Default function will set the input value unconditionally or conditionally.
  dictionary.defaults[index] = entry if entry.type == :set

  # guard: columns are anonymous
  Dictionary.add_name(columns: dictionary.columns, name: entry.name) unless entry.type == :guard
end
output_entry(dictionary:, entry:, index:) click to toggle source
# File lib/csv_decision/dictionary.rb, line 155
def self.output_entry(dictionary:, entry:, index:)
  dictionary.outs[index] = entry

  case entry.type
  # if: columns are anonymous, even if the user names them
  when :if
    dictionary.ifs[index] = entry

  when :out
    Dictionary.add_name(columns: dictionary.columns, name: entry.name, out: index)
  end
end
parse_cell(cell:, index:, dictionary:) click to toggle source
# File lib/csv_decision/dictionary.rb, line 129
def self.parse_cell(cell:, index:, dictionary:)
  column_type, column_name = Validate.column(cell: cell, index: index)

  dictionary_entry(dictionary: dictionary,
                   entry: Entry.create(name: column_name, type: column_type),
                   index: index)
end