Class: CSVDecision::Table

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

Overview

Decision Table that accepts input hashes and makes decisions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTable

Returns a new instance of Table.



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

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

Instance Attribute Details

#columnsCSVDecision::Columns

Returns Dictionary of all input and output columns.

Returns:



10
11
12
# File 'lib/csv_decision/table.rb', line 10

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.



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

def file
  @file
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.



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

def options
  @options
end

#outs_rowsArray<CSVDecision::ScanRow>

Returns Used to implement outputting of final results.

Returns:



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

def outs_rows
  @outs_rows
end

#rowsArray<Array>

Returns Data rows after parsing.

Returns:

  • (Array<Array>)

    Data rows after parsing.



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

def rows
  @rows
end

#scan_rowsArray<CSVDecision::ScanRow>

Returns Scanning objects used to implement input matching logic.

Returns:



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

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.

Main public method for making decisions.

Parameters:

  • input (Hash)

    Input hash.

Returns:

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

    Decision hash.



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

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

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

Note:

Input hash must have its keys symbolized.

Unsafe version of decide - will mutate the hash if set: column type is used (planned feature).

Parameters:

  • input (Hash)

    Input hash.

Returns:

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

    Decision hash.



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

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

#each(first = 0, last = @rows.count - 1) ⇒ Object

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.



59
60
61
62
63
64
65
66
# File 'lib/csv_decision/table.rb', line 59

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

    index += 1
  end
end