Class: CSVDecision::Decision Private

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_decision/decision.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.

Accumulate the matching row(s) and calculate the final result.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:, input:) ⇒ Decision

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

Parameters:

  • table (CSVDecision::Table)

    Decision table being processed.

  • input (Hash{Symbol=>Object})

    Input hash data structure.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/csv_decision/decision.rb', line 32

def initialize(table:, input:)
  # The result object is a hash of values, and each value will be an array if this is
  # a multi-row result for the +first_match: false+ option.
  @result = Result.new(table: table, input: input[:hash])

  # All rows picked by the matching process. An array if +first_match: false+,
  # otherwise a single row.
  @rows_picked = []

  @first_match = table.options[:first_match]

  @table = table
  @input = input
end

Class Method Details

.make(table:, input:, symbolize_keys:) ⇒ Hash{Symbol=>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.

Main method for making decisions.

Parameters:

  • table (CSVDecision::Table)

    Decision table.

  • input (Hash)

    Input hash (keys may or may not be symbolized)

  • symbolize_keys (Boolean)

    Set to false if keys are symbolized and it’s OK to mutate the input hash. Otherwise a copy of the input hash is symbolized.

Returns:

  • (Hash{Symbol=>Object})

    Decision result.



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

def self.make(table:, input:, symbolize_keys:)
  # Parse and transform the hash supplied as input
  input = Input.parse(table: table, input: input, symbolize_keys: symbolize_keys)

  # The decision object collects the results of the search and
  # calculates the final result
  decision = Decision.new(table: table, input: input)

  # Use the table's index if present
  table.index ? decision.index_scan : decision.table_scan
end

Instance Method Details

#index_scanHash{Symbol=>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.

Use an index to scan the decision table up against the input hash.

Returns:

  • (Hash{Symbol=>Object})

    Decision result.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/csv_decision/decision.rb', line 64

def index_scan
  # If the index lookup fails, there's no match.
  return {} unless (index_rows = Array(@table.index.hash[@input[:key]]))

  hash = @input[:hash]
  scan_cols = @input[:scan_cols]

  if @first_match
    index_scan_first_match(scan_cols: scan_cols, hash: hash, index_rows: index_rows)
  else
    index_scan_accumulate(scan_cols: scan_cols, hash: hash, index_rows: index_rows)
  end
end

#table_scanHash{Symbol=>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.

Scan the decision table up against the input hash.

Returns:

  • (Hash{Symbol=>Object})

    Decision result.



50
51
52
53
54
55
56
57
58
59
# File 'lib/csv_decision/decision.rb', line 50

def table_scan
  hash = @input[:hash]
  scan_cols = @input[:scan_cols]

  if @first_match
    scan_first_match(hash: hash, scan_cols: scan_cols)
  else
    scan_accumulate(hash: hash, scan_cols: scan_cols)
  end
end