Class: Kiba::Extend::Transforms::Count::MatchingRowsInLookup

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/count/matching_rows_in_lookup.rb

Overview

Merges count of lookup rows to be merged into specified field. By default it retuns the count as a string, since many of the other transforms assume string values

Since:

  • 2.6.0

Instance Method Summary collapse

Constructor Details

#initialize(lookup:, keycolumn:, targetfield:, conditions: {}, result_type: :str) ⇒ MatchingRowsInLookup

Returns a new instance of MatchingRowsInLookup.

Parameters:

Since:

  • 2.6.0



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kiba/extend/transforms/count/matching_rows_in_lookup.rb', line 19

def initialize(lookup:, keycolumn:, targetfield:, conditions: {},
  result_type: :str)
  @lookup = lookup
  @keycolumn = keycolumn
  @target = targetfield
  @conditions = conditions
  @result_type = result_type
  @selector = Lookup::RowSelector.call(
    conditions: conditions
  )
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

  • row (Hash{ Symbol => String, nil })

Since:

  • 2.6.0



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kiba/extend/transforms/count/matching_rows_in_lookup.rb', line 32

def process(row)
  id = row.fetch(keycolumn)
  matches = lookup.fetch(id, [])
  if matches.size.zero?
    row[target] = finalize(0)
  else
    merge_rows = selector.call(
      origrow: row,
      mergerows: matches
    )
    row[target] = finalize(merge_rows.size)
  end
  row
end