Class: CSVDecision::Result Private

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_decision/result.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) into a result hash.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table:, input:) ⇒ Object

Parameters:

  • table (CSVDecision::Table)

    Decision table being processed.

  • input (Hash{Symbol=>Object})

    Input hash data structure.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/csv_decision/result.rb', line 19

def initialize(table:, input:)
  @outs = table.columns.outs
  @if_columns = table.columns.ifs

  # Partial result always copies in the input hash for calculating output functions.
  # Note that these input key values will not be mutated, as output columns can never
  # have the same symbol as an input hash key.
  # However, the rest of this hash is mutated as output column evaluation results
  # are accumulated.
  @partial_result = input[:hash]&.slice(*table.columns.input_keys) if table.outs_functions

  # Attributes hash contains the output decision key value pairs
  @attributes = {}

  # Set to true if the result has more than one row.
  # Only possible for the first_match: false option.
  @multi_result = false
end

Instance Attribute Details

#attributesHash{Symbol=>Object}, Hash{Integer=>Object} (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 The decision result hash containing both result values and if: columns, which eventually get evaluated and removed.

Returns:

  • (Hash{Symbol=>Object}, Hash{Integer=>Object})

    The decision result hash containing both result values and if: columns, which eventually get evaluated and removed.



13
14
15
# File 'lib/csv_decision/result.rb', line 13

def attributes
  @attributes
end

#multi_resultBoolean (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 this is a multi-row result

Returns:

  • (Boolean)

    Returns true if this is a multi-row result



16
17
18
# File 'lib/csv_decision/result.rb', line 16

def multi_result
  @multi_result
end

Instance Method Details

#accumulate_outs(row) ⇒ 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.

Accumulate the outs into arrays of values.

Parameters:

  • row (Array)


49
50
51
# File 'lib/csv_decision/result.rb', line 49

def accumulate_outs(row)
  @outs.each_pair { |col, column| add_cell(column_name: column.name, cell: row[col]) }
end

#add_outs(row) ⇒ 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.

Common case for building a single row result is just copying output column values to the final result hash.

Parameters:

  • row (Array)


42
43
44
# File 'lib/csv_decision/result.rb', line 42

def add_outs(row)
  @outs.each_pair { |col, column| @attributes[column.name] = row[col] }
end

#eval_cell_proc(proc:, column_name:, index:) ⇒ 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 using the partial result calculated so far.

Parameters:

  • proc (Matchers::Pro)
  • column_name (Symbol, Integer)
  • index (Integer)


82
83
84
# File 'lib/csv_decision/result.rb', line 82

def eval_cell_proc(proc:, column_name:, index:)
  @attributes[column_name][index] = proc.function[partial_result(index)]
end

#eval_outs(row) ⇒ {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.

Evaluate the output columns, and use them to start building the final result, along with the partial result required to evaluate functions.

Parameters:

  • row (Array)

Returns:

  • ({Symbol=>Object})


67
68
69
70
71
72
73
74
75
# File 'lib/csv_decision/result.rb', line 67

def eval_outs(row)
  # Set the constants first, in case the functions refer to them
  eval_outs_constants(row: row)

  # Then evaluate the procs, left to right
  eval_outs_procs(row: row)

  final
end

#final{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.

Derive the final result.

Returns:

  • ({Symbol=>Object})


55
56
57
58
59
60
# File 'lib/csv_decision/result.rb', line 55

def final
  # If there are no if: columns, then nothing needs to be filtered out of this result hash.
  return @attributes if @if_columns.empty?

  @multi_result ? multi_row_result : single_row_result
end