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



59
60
61
62
# File 'lib/csv_decision/scan_row.rb', line 59

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

Instance Attribute Details

#constantsArray<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 simple constants.

Returns:

  • (Array<Integer>)

    Column indices for simple constants.



54
55
56
# File 'lib/csv_decision/scan_row.rb', line 54

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.



57
58
59
# File 'lib/csv_decision/scan_row.rb', line 57

def procs
  @procs
end

Class Method Details

.eval_matcher(proc:, hash:, value: nil) ⇒ Object

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.

Evaluate the cell proc against the column's input value and/or input hash.

Parameters:

  • proc (CSVDecision::Proc)

    Proc in the table cell.

  • value (Object)

    Value supplied in the input hash corresponding to this column.

  • hash ({Symbol=>Object})

    Input hash with symbolized keys.



43
44
45
46
47
48
49
50
51
# File 'lib/csv_decision/scan_row.rb', line 43

def self.eval_matcher(proc:, hash:, value: nil)
  function = proc.function

  # A symbol guard expression just needs to be passed the input hash
  return function[hash] if proc.type == :guard

  # All other procs can take one or two args
  function.arity == 1 ? function[value] : function[value, hash]
end

.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:



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

def self.scan(column:, matchers:, cell:)
  matchers.each do |matcher|
    # Guard function only accepts the same matchers as an output column.
    next if column.type == :guard && !matcher.outs?

    if (proc = matcher.matches?(cell))
      guard_constant?(type: proc.type, column: column)
      return proc
    end
  end

  # Must be a simple string constant - this is OK except for a guard column
  guard_constant?(type: :constant, column: column)
end

Instance Method Details

#match_constants?(row:, scan_cols:) ⇒ 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 containing simple constants.

Parameters:

  • scan_cols (Hash{Integer=>Object})
  • row (Array)

    Data row.

Returns:

  • (Boolean)

    True for a match, false otherwise.



92
93
94
95
96
97
98
# File 'lib/csv_decision/scan_row.rb', line 92

def match_constants?(row:, scan_cols:)
  constants.each do |col|
    return false unless row[col] == scan_cols[col]
  end

  true
end

#match_procs?(row:, input:) ⇒ 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 containing a Proc object.

Parameters:

  • input (Hash{Symbol => Hash{Symbol=>Object}, Hash{Integer=>Object}})
  • row (Array)

    Data row.

Returns:

  • (Boolean)

    True for a match, false otherwise.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/csv_decision/scan_row.rb', line 104

def match_procs?(row:, input:)
  hash = input[:hash]
  scan_cols = input[:scan_cols]

  procs.each do |col|
    match = ScanRow.eval_matcher(proc: row[col], value: scan_cols[col], hash: hash)
    return false unless match
  end

  true
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)

    Data row.

  • 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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/csv_decision/scan_row.rb', line 72

def scan_columns(row:, columns:, matchers:)
  columns.each_pair do |col, column|
    # An empty input cell matches everything, and so never needs to be scanned
    next if (cell = row[col]) == '' && column.ins?

    # If the column is text only then no special matchers need be invoked
    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.
    row[col] = scan_cell(column: column, col: col, matchers: matchers, cell: cell)
  end

  row
end