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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:) ⇒ 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:



34
35
36
37
38
39
40
# File 'lib/csv_decision/decision.rb', line 34

def initialize(table:)
  # 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)
  @first_match = table.options[:first_match]
  @table = table
end

Instance Attribute Details

#first_matchBoolean (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 True if a first match decision table.

Returns:

  • (Boolean)

    True if a first match decision table.



28
29
30
# File 'lib/csv_decision/decision.rb', line 28

def first_match
  @first_match
end

#tableCSVDecision::Table (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 Decision table object.

Returns:



31
32
33
# File 'lib/csv_decision/decision.rb', line 31

def table
  @table
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 without a path.

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
# File 'lib/csv_decision/decision.rb', line 18

def self.make(table:, input:, symbolize_keys:)
  # Parse and transform the hash supplied as input
  data = 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.new(table: table).scan(data)
end

Instance Method Details

#index_scan_accumulate(scan_cols:, hash:, index_rows:) ⇒ 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.

Scan the index for an accumulated result.

Parameters:

  • scan_cols (Hash{Integer=>Object})
  • hash (Hash{Symbol=>Object})
  • index_rows (Array<Integer>)

Returns:

  • (Hash{Symbol=>Object})


90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/csv_decision/decision.rb', line 90

def index_scan_accumulate(scan_cols:, hash:, index_rows:)
  index_rows.each do |start_row, end_row|
    @table.each(start_row, end_row || start_row) do |row, index|
      next unless @table.scan_rows[index].match?(row: row, hash: hash, scan_cols: scan_cols)

      # Accumulate output rows.
      @rows_picked << row
      @result.accumulate_outs(row)
    end
  end

  @rows_picked.empty? ? {} : accumulated_result
end

#index_scan_first_match(scan_cols:, hash:, index_rows:) ⇒ 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.

Scan the index for a first match result.

Parameters:

  • scan_cols (Hash{Integer=>Object})
  • hash (Hash{Symbol=>Object})
  • index_rows (Array<Integer>)

Returns:

  • (Hash{Symbol=>Object})


72
73
74
75
76
77
78
79
80
81
82
# File 'lib/csv_decision/decision.rb', line 72

def index_scan_first_match(scan_cols:, hash:, index_rows:)
  index_rows.each do |start_row, end_row|
    @table.each(start_row, end_row || start_row) do |row, index|
      next unless @table.scan_rows[index].match?(row: row, hash: hash, scan_cols: scan_cols)

      return @result.attributes if first_match_found(row)
    end
  end

  {}
end

#input(data) ⇒ void

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.

This method returns an undefined value.

Initialize the input data used to make the decision.

Parameters:

  • data (Hash{Symbol=>Object})

    Input hash data structure.



46
47
48
49
50
51
52
53
54
# File 'lib/csv_decision/decision.rb', line 46

def input(data)
  @result.input(data[:hash])

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

  @input = data
end

#scan(data) ⇒ 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.

Scan the decision table and produce an output decision.

Parameters:

  • data (Hash{Symbol=>Object})

    Input hash data structure.

Returns:

  • (Hash{Symbol=>Object})

    Decision result.



60
61
62
63
64
# File 'lib/csv_decision/decision.rb', line 60

def scan(data)
  input(data)
  # Use the table's index if present
  @table.index ? index_scan : table_scan
end