Class: Kiba::Extend::Transforms::Warn::UnevenFields

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/kiba/extend/transforms/warn/uneven_fields.rb

Overview

Prints warning to STDOUT for each row having uneven values in the given fields

What is meant by “Even fields”?

When a field group is even, each field in the group contains the same number of values. For example:

{foo: 'af|bf|cf', bar: 'ab|bb|cb', baz: 'az|bz|cz'}

Depending on your application, an uneven field group may or may not be a concern:

{foo: 'af|bf|cf', bar: 'ab|bb|cb', baz: 'az|zz|bz|cz'}

foo and bar both have 3 values, while baz has 4. The assumption of a repeating field group is: foo[0] goes with bar[0] goes with baz[0], so having an extra value in baz is a problem if you expect bf, bb, and bz to line up.

Note that the following is considered to be even, because we ignore fields that have no value at all, and, though baz only has two values non-null values, the position of the null value in the middle is clearly indicated:

{foo: 'af|bf|cf', bar: nil, baz: 'az||cz'}

Several transforms have the option to automatically pad uneven value fields so that the overall field group is even. However, there’s no foolproof way to do this that will be correct for all applications, so you should be able to get warnings for all instances of unevenness, in order to check your data is being transformed as expected.

Since:

  • 2.9.0

Instance Method Summary collapse

Methods included from Helpers

empty?

Constructor Details

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

Returns a new instance of UnevenFields.

Parameters:

  • fields (Array(Symbol))

    fields across which to even field values

  • delim (String) (defaults to: Kiba::Extend.delim)

    used to split field values to determine length

Since:

  • 2.9.0



43
44
45
46
47
48
# File 'lib/kiba/extend/transforms/warn/uneven_fields.rb', line 43

def initialize(fields:, delim: Kiba::Extend.delim)
  @fields = [fields].flatten
  @delim = delim
  @checker = Helpers::FieldEvennessChecker.new(fields: fields,
    delim: delim)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kiba/extend/transforms/warn/uneven_fields.rb', line 51

def process(row)
  return row if fields.length == 1

  chk_result = checker.call(row)
  return row if chk_result == :even

  uneven = chk_result.map do |field, val|
    "#{field}: #{val}"
  end.join("; ")
  msg = "#{Kiba::Extend.warning_label}: Uneven values for #{fields.join("/")} in #{uneven}"
  warn(msg)
  row
end