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.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/csv_decision/table.rb', line 80

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:



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

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.



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

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:



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

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.



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

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)



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

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:



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

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.



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

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:



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

def scan_rows
  @scan_rows
end

Instance Method Details

#decide(input) ⇒ Hash{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:

  • (Hash{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) ⇒ Hash{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 - assumes the input hash is symbolized.

Parameters:

  • input (Hash)

    Input hash.

Returns:

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

    Decision hash.



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

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.



70
71
72
73
74
75
76
77
# File 'lib/csv_decision/table.rb', line 70

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

    index += 1
  end
end