Class: CSVDecision::ScanRow Private

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_decision/scan_row.rb

Overview

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

Data row object indicating which columns are constants versus procs.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScanRow

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.

Returns a new instance of ScanRow



72
73
74
75
# File 'lib/csv_decision/scan_row.rb', line 72

def initialize
  @constants = []
  @procs = []
end

Instance Attribute Details

#constantsArray<Integer>

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.

Returns Column indices for simple constants.

Returns:

  • (Array<Integer>)

    Column indices for simple constants.



67
68
69
# File 'lib/csv_decision/scan_row.rb', line 67

def constants
  @constants
end

#procsArray<Integer> (readonly)

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.

Returns Column indices for Proc objects.

Returns:

  • (Array<Integer>)

    Column indices for Proc objects.



70
71
72
# File 'lib/csv_decision/scan_row.rb', line 70

def procs
  @procs
end

Class Method Details

.scan(column:, matchers:, cell:) ⇒ false, Matchers::Proc

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.

Scan the table cell against all matches.

Parameters:

Returns:



21
22
23
24
25
26
27
28
29
# File 'lib/csv_decision/scan_row.rb', line 21

def self.scan(column:, matchers:, cell:)
  return false if cell == ''

  proc = scan_matchers(column: column, matchers: matchers, cell: cell)
  return proc if proc

  # Must be a simple string constant - this is OK except for a certain column types.
  invalid_constant?(type: :constant, column: column)
end

Instance Method Details

#match?(row:, scan_cols:, hash:) ⇒ 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.

Match cells in the input hash against a decision table row.

Parameters:

  • row (Array<String>)

    Data row - still just all string constants.

Returns:

  • (Boolean)

    True for a match, false otherwise.



108
109
110
111
112
113
114
115
# File 'lib/csv_decision/scan_row.rb', line 108

def match?(row:, scan_cols:, hash:)
  # Check any table row cell constants first, and maybe fail fast...
  return false if @constants.any? { |col| row[col] != scan_cols[col] }

  # These table row cells are Proc objects which need evaluating and
  # must all return a truthy value.
  @procs.all? { |col| row[col].call(value: scan_cols[col], hash: hash) }
end

#scan_columns(row:, columns:, matchers:) ⇒ Array

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.

Scan all the specified columns (e.g., inputs) in the given data row using the matchers array supplied.

Parameters:

  • row (Array<String>)

    Data row - still just all string constants.

  • columns (Array<Columns::Entry>)

    Array of column dictionary entries.

  • matchers (Array<Matchers::Matcher>)

    Array of table cell matchers.

Returns:

  • (Array)

    Data row with anything not a string constant replaced with a Proc or a non-string constant.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/csv_decision/scan_row.rb', line 85

def scan_columns(row:, columns:, matchers:)
  columns.each_pair do |col, column|
    cell = row[col]

    # An empty input cell matches everything, and so never needs to be scanned,
    # but it cannot be indexed either.
    next column.indexed = false if cell == '' && column.ins?

    # If the column is text only then no special matchers need be used.
    next @constants << col if column.eval == false

    # Need to scan the cell against all matchers, and possibly overwrite
    # the cell contents with a Matchers::Proc value.
    row[col] = scan_cell(column: column, col: col, matchers: matchers, cell: cell)
  end

  row
end