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



86
87
88
89
# File 'lib/csv_decision/scan_row.rb', line 86

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.



81
82
83
# File 'lib/csv_decision/scan_row.rb', line 81

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.



84
85
86
# File 'lib/csv_decision/scan_row.rb', line 84

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.



70
71
72
73
74
75
76
77
78
# File 'lib/csv_decision/scan_row.rb', line 70

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:



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

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



119
120
121
122
123
124
125
# File 'lib/csv_decision/scan_row.rb', line 119

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.



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/csv_decision/scan_row.rb', line 131

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/csv_decision/scan_row.rb', line 99

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