Class: CSVDecision::Decision

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

Overview

Accumulate the matching row(s) and calculate the final result

Instance Method Summary collapse

Constructor Details

#initialize(table:, input:) ⇒ Decision

Returns a new instance of Decision.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/csv_decision/decision.rb', line 9

def initialize(table:, input:)
  @result = {}

  # Relevant table attributes
  @first_match = table.options[:first_match]
  @outs = table.columns.outs

  # TODO: Planned feature
  # @outs_functions = table.outs_functions

  # Partial result always includes the input hash for calculating output functions
  @partial_result = input[:hash].dup if @outs_functions

  @row_picked = nil
  return if @first_match

  # Extra attributes for the accumulate option
  @rows_picked = []
  @multi_result = nil
end

Instance Method Details

#add(row) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/csv_decision/decision.rb', line 59

def add(row)
  return add_first_match(row) if @first_match

  # Accumulate output rows
  @rows_picked << row
  @outs.each_pair do |col, column|
    accumulate_outs(column_name: column.name, cell: row[col])
  end

  # Not done
  false
end

#empty?Boolean

Is the result set empty? That is, nothing matched?

Returns:

  • (Boolean)


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

def empty?
  return @row_picked.nil? if @first_match
  @rows_picked.empty?
end

#exist?Boolean

Returns:

  • (Boolean)


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

def exist?
  !empty?
end

#resultObject



40
41
42
43
44
45
# File 'lib/csv_decision/decision.rb', line 40

def result
  return {} if empty?
  return final_result unless @outs_functions

  nil
end

#scan(table:, input:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/csv_decision/decision.rb', line 47

def scan(table:, input:)
  scan_rows = table.scan_rows

  table.each do |row, index|
    done = row_scan(input: input, row: row, scan_row: scan_rows[index])

    return self if done
  end

  self
end