Class: CSVDecision::ScanRow

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

Overview

Data row object indicating which columns are constants versus procs.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScanRow

Returns a new instance of ScanRow



17
18
19
20
# File 'lib/csv_decision/scan_row.rb', line 17

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

Instance Attribute Details

#constantsArray<Integer> (readonly)

Returns Column indices for simple constants.

Returns:

  • (Array<Integer>)

    Column indices for simple constants.



12
13
14
# File 'lib/csv_decision/scan_row.rb', line 12

def constants
  @constants
end

#procsArray<Integer> (readonly)

Returns Column indices for Proc objects.

Returns:

  • (Array<Integer>)

    Column indices for Proc objects.



15
16
17
# File 'lib/csv_decision/scan_row.rb', line 15

def procs
  @procs
end

Instance Method Details

#match_constants?(row:, scan_cols:) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/csv_decision/scan_row.rb', line 53

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

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

  true
end

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

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

Parameters:

  • row (Array<String>)

    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.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/csv_decision/scan_row.rb', line 29

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

    # If the column is text only then no special matchers need be invoked
    next @constants << col if column.text_only

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

  row
end