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:) ⇒ Object

Parameters:



25
26
27
28
29
# File 'lib/csv_decision/result.rb', line 25

def initialize(table:)
  @outs = table.columns.outs
  @outs_functions = table.outs_functions
  @table = table
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



22
23
24
# File 'lib/csv_decision/result.rb', line 22

def multi_result
  @multi_result
end

#outsHash{Index=>Dictionary::Entry} (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 Output columns.

Returns:



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

def outs
  @outs
end

#outs_functionsnil, true (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 Set to true if the table has output functions.

Returns:

  • (nil, true)

    Set to true if the table has output functions.



19
20
21
# File 'lib/csv_decision/result.rb', line 19

def outs_functions
  @outs_functions
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)


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

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)


51
52
53
# File 'lib/csv_decision/result.rb', line 51

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)


91
92
93
# File 'lib/csv_decision/result.rb', line 91

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

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


76
77
78
79
80
81
82
83
84
# File 'lib/csv_decision/result.rb', line 76

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_result
end

#final_resultHash{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:

  • (Hash{Symbol=>Object})


64
65
66
67
68
69
# File 'lib/csv_decision/result.rb', line 64

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

  @multi_result ? multi_row_result : single_row_result
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 object for new input data.

Parameters:

  • data (Hash{Symbol=>Object})

    Input data hash.



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

def input(data)
  # Attributes hash contains the output decision key value pairs
  @attributes = {}
  @multi_result = false
  # 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 = data.slice(*@table.columns.input_keys) if @outs_functions
end