class CSVDecision::Columns
Dictionary
of all this table's columns - inputs, outputs etc. @api private
Public Class Methods
@param columns [CSVDecision::Columns] Table's columns dictionary. @param cell [Object] Data
row cell. @return [void]
# File lib/csv_decision/columns.rb, line 30 def self.ins_cell_dictionary(columns:, cell:) return unless cell.is_a?(Matchers::Proc) return if cell.symbols.nil? add_ins_symbols(columns: columns, cell: cell) end
@param columns [CSVDecision::Columns] Table's columns dictionary. @param row [Array] Data
row. @return [void]
# File lib/csv_decision/columns.rb, line 23 def self.ins_dictionary(columns:, row:) row.each { |cell| ins_cell_dictionary(columns: columns, cell: cell) } end
@param table [Table] Decision
table being constructed.
# File lib/csv_decision/columns.rb, line 178 def initialize(table) # If a column does not have a valid header cell, then it's empty of data. # Return the stripped header row, and remove it from the data array. row = Header.strip_empty_columns(rows: table.rows) # No header row found? raise TableValidationError, 'table has no header row' unless row # Build a dictionary of all valid data columns from the header row. @dictionary = CSVDecision::Dictionary.build(header: row, dictionary: Dictionary.new) freeze end
@param columns [CSVDecision::Columns] Table's columns dictionary. @param row [Array] Data
row. @return [void]
# File lib/csv_decision/columns.rb, line 14 def self.outs_dictionary(columns:, row:) row.each_with_index do |cell, index| outs_check_cell(columns: columns, cell: cell, index: index) end end
Private Class Methods
# File lib/csv_decision/columns.rb, line 87 def self.add_ins_symbols(columns:, cell:) Array(cell.symbols).each do |symbol| CSVDecision::Dictionary.add_name(columns: columns, name: symbol) end end
# File lib/csv_decision/columns.rb, line 52 def self.check_outs_symbol(columns:, symbol:, index:) in_out = columns.dictionary[symbol] # If its an input column symbol then we're good. return if ins_symbol?(columns: columns, symbol: symbol, in_out: in_out) # Check if this output symbol reference is on or after this cell's column invalid_out_ref?(columns, index, in_out) end
# File lib/csv_decision/columns.rb, line 45 def self.check_outs_symbols(columns:, cell:, index:) Array(cell.symbols).each do |symbol| check_outs_symbol(columns: columns, symbol: symbol, index: index) end end
If the symbol exists either as an input or does not exist then we're good.
# File lib/csv_decision/columns.rb, line 64 def self.ins_symbol?(columns:, symbol:, in_out:) return true if in_out == :in # It must an input symbol, as all the output symbols have been parsed. return columns.dictionary[symbol] = :in if in_out.nil? false end
# File lib/csv_decision/columns.rb, line 74 def self.invalid_out_ref?(columns, index, in_out) return false if in_out < index that_column = if in_out == index 'reference to itself' else "an out of order reference to output column '#{columns.outs[in_out].name}'" end raise CellValidationError, "output column '#{columns.outs[index].name}' makes #{that_column}" end
# File lib/csv_decision/columns.rb, line 37 def self.outs_check_cell(columns:, cell:, index:) return unless cell.is_a?(Matchers::Proc) return if cell.symbols.nil? check_outs_symbols(columns: columns, cell: cell, index: index) end
Public Instance Methods
Input
columns with defaults specified
# File lib/csv_decision/columns.rb, line 133 def defaults @dictionary&.defaults end
Set defaults for columns with defaults specified
# File lib/csv_decision/columns.rb, line 138 def defaults=(value) @dictionary.defaults = value end
@return [Hash{Symbol=>[false, Integer]}] Dictionary
of all
input and output column names.
# File lib/csv_decision/columns.rb, line 144 def dictionary @dictionary.columns end
if: columns hash keyed by column index. @return [Hash{Index=>Entry}]
# File lib/csv_decision/columns.rb, line 162 def ifs @dictionary.ifs end
@return [Array<Symbol>] All input column symbols.
# File lib/csv_decision/columns.rb, line 173 def input_keys @dictionary.columns.select { |_k, v| v == :in }.keys end
Input
columns hash keyed by column index. @return [Hash{Index=>Entry}]
# File lib/csv_decision/columns.rb, line 150 def ins @dictionary.ins end
Output columns hash keyed by column index. @return [Hash{Index=>Entry}]
# File lib/csv_decision/columns.rb, line 156 def outs @dictionary&.outs end
path: columns hash keyed by column index. @return [Hash{Index=>Entry}]
# File lib/csv_decision/columns.rb, line 168 def paths @dictionary.paths end