class CSVDecision::Paths

Build an index for a decision table with one or more input columns designated as keys @api private

Attributes

paths[R]

@return [Hash] The index hash mapping in input values to one or more data array row indexes.

Public Class Methods

new(table:, columns:) click to toggle source

@param table [CSVDecision::Table] Decision table. @param columns [Array<Index>] Array of column indexes to be indexed.

# File lib/csv_decision/paths.rb, line 45
def initialize(table:, columns:)
  @paths = []
  @columns = columns

  build(table)

  freeze
end
scan(table:) click to toggle source

Build the index of paths

@param table [CSVDecision::Table] Decision table being indexed. @return [CSVDecision::Paths] The built index of paths.

# File lib/csv_decision/paths.rb, line 16
def self.scan(table:)
  # Do we even have paths?
  columns = table.columns.paths.keys
  return [] if columns.empty?

  table.paths = Paths.new(table: table, columns: columns).paths
end
symbol(value) click to toggle source

@param value [String] Cell value for the path: column. @return [nil, Symbol] Non-empty string converted to a symbol.

# File lib/csv_decision/paths.rb, line 36
def self.symbol(value)
  value.blank? ? nil : value.to_sym
end
value(current_value, index) click to toggle source

@param current_value [Integer, Array] Current path value. @param index [Integer] Array row index to be included in the path entry. @return [Integer, Array] New path key value.

# File lib/csv_decision/paths.rb, line 27
def self.value(current_value, index)
  return [current_value, index] if current_value.is_a?(Integer)

  current_value[-1] = index
  current_value
end

Private Instance Methods

build(table) click to toggle source
# File lib/csv_decision/paths.rb, line 56
def build(table)
  last_path = nil
  key = -1
  rows = nil
  table.each do |row, index|
    path = build_path(row: row)
    if path == last_path
      rows = Paths.value(rows, index)
    else
      rows = index
      key += 1
      last_path = path
    end

    @paths[key] = [path, rows]
  end
end
build_path(row:) click to toggle source
# File lib/csv_decision/paths.rb, line 74
def build_path(row:)
  @columns.map { |col| Paths.symbol(row[col]) }.compact
end