Class: CSVDecision::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_decision/table.rb

Overview

Decision table that accepts an input hash and outputs a decision (hash).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

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 Table



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

def initialize
  @columns = nil
  @file = nil
  @options = nil
  @outs_functions = nil
  @outs_rows = []
  @if_rows = []
  @rows = []
  @scan_rows = []
  # @tables = nil
end

Instance Attribute Details

#columnsCSVDecision::Columns

Returns Dictionary of all input and output columns.

Returns:



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

def columns
  @columns
end

#fileFile, ...

Returns File path name if decision table was loaded from a CSV file.

Returns:

  • (File, Pathname, nil)

    File path name if decision table was loaded from a CSV file.



34
35
36
# File 'lib/csv_decision/table.rb', line 34

def file
  @file
end

#if_rowsArray<CSVDecision::ScanRow>

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 Used to implement filtering of final results.

Returns:



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

def if_rows
  @if_rows
end

#optionsHash

Returns All options, explicitly set or defaulted, used to parse the table.

Returns:

  • (Hash)

    All options, explicitly set or defaulted, used to parse the table.



37
38
39
# File 'lib/csv_decision/table.rb', line 37

def options
  @options
end

#outs_functionsObject

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.

Set if the table row has any output functions (planned feature)



41
42
43
# File 'lib/csv_decision/table.rb', line 41

def outs_functions
  @outs_functions
end

#outs_rowsArray<CSVDecision::ScanRow>

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 Used to implement outputting of final results.

Returns:



54
55
56
# File 'lib/csv_decision/table.rb', line 54

def outs_rows
  @outs_rows
end

#rowsArray<Array>

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 Data rows after parsing.

Returns:

  • (Array<Array>)

    Data rows after parsing.



45
46
47
# File 'lib/csv_decision/table.rb', line 45

def rows
  @rows
end

#scan_rowsArray<CSVDecision::ScanRow>

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 Scanning objects used to implement input matching logic.

Returns:



50
51
52
# File 'lib/csv_decision/table.rb', line 50

def scan_rows
  @scan_rows
end

Instance Method Details

#decide(input) ⇒ {Symbol => Object, Array<Object>}

Note:

Input hash keys may or may not be symbolized.

Make a decision based off an input hash.

Parameters:

  • input (Hash)

    Input hash.

Returns:

  • ({Symbol => Object, Array<Object>})

    Decision hash.



15
16
17
# File 'lib/csv_decision/table.rb', line 15

def decide(input)
  Decide.decide(table: self, input: input, symbolize_keys: true)
end

#decide!(input) ⇒ {Symbol => Object, Array<Object>}

Note:

Input hash must have its keys symbolized. Input hash will be mutated by any functions that have side effects.

Unsafe version of decide - may mutate the input hash and assumes the input hash is symbolized.

Parameters:

  • input (Hash)

    Input hash.

Returns:

  • ({Symbol => Object, Array<Object>})

    Decision hash.



26
27
28
# File 'lib/csv_decision/table.rb', line 26

def decide!(input)
  Decide.decide(table: self, input: input, symbolize_keys: false)
end

#each(first = 0, last = @rows.count - 1) ⇒ 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.

Iterate through all data rows of the decision table, with an optional first and last row index given.

Parameters:

  • first (Integer) (defaults to: 0)

    Start row.

  • last (Integer, nil) (defaults to: @rows.count - 1)

    Last row.



66
67
68
69
70
71
72
73
# File 'lib/csv_decision/table.rb', line 66

def each(first = 0, last = @rows.count - 1)
  index = first
  while index <= (last || first)
    yield(@rows[index], index)

    index += 1
  end
end