Class: Kiba::Extend::Transforms::Helpers::FieldEvennessChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/helpers/field_evenness_checker.rb

Overview

Given row and array of field names, checks whether fields have the same number of values

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(fields:, delim: Kiba::Extend.delim) ⇒ FieldEvennessChecker

Returns a new instance of FieldEvennessChecker.

Since:

  • 2.9.0



13
14
15
16
17
# File 'lib/kiba/extend/transforms/helpers/field_evenness_checker.rb', line 13

def initialize(fields:, delim: Kiba::Extend.delim)
  @fields = [fields].flatten
  @delim = delim
  @value_getter = FieldValueGetter.new(fields: fields, delim: delim)
end

Instance Method Details

#call(row) ⇒ Object

Since:

  • 2.9.0



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kiba/extend/transforms/helpers/field_evenness_checker.rb', line 19

def call(row)
  return :even if fields.length == 1

  vals = value_getter.call(row)
  max = max_value_ct(vals)
  checked = vals.map do |field, val|
    [field, (val.split(delim, -1).length == max) ? :even : :uneven]
  end
    .to_h
  return :even if checked.values.all?(:even)

  vals.select { |field, value| checked[field] == :uneven }
end