Module: CSVDecision::Decide

Defined in:
lib/csv_decision/decide.rb

Overview

Main module for searching the decision table looking for one or more matches

Class Method Summary collapse

Class Method Details

.decide(table:, input:, symbolize_keys:) ⇒ Hash

Main method for making decisions.

Parameters:

  • table (CSVDecision::Table)

    Decision table.

  • input (Hash)

    Input hash (keys may or may not be symbolized)

  • symbolize_keys (true, false)

    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)

    Decision result.



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

def self.decide(table:, input:, symbolize_keys:)
  # Parse and transform the hash supplied as input
  parsed_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: parsed_input)

  # table_scan(table: table, input: parsed_input, decision: decision)
  decision.scan(table: table, input: parsed_input)
end

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



37
38
39
40
41
42
43
44
45
# File 'lib/csv_decision/decide.rb', line 37

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

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

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

.matches?(row:, input:, scan_row:) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
# File 'lib/csv_decision/decide.rb', line 28

def self.matches?(row:, input:, scan_row:)
  match = scan_row.match_constants?(row: row, scan_cols: input[:scan_cols])
  return false unless match

  return true if scan_row.procs.empty?

  scan_row.match_procs?(row: row, input: input)
end