Class: Kiba::Extend::Transforms::Delete::EmptyFieldGroups

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/delete/empty_field_groups.rb

Overview

Deletes empty value from each field in a field group if values in that position in each field in the field group are empty. For example, if the 3rd value in grouped fields annotationType, annotationDate, and annotationNote are all blank, then the 3rd value from each field is deleted.

Features/behaviors:

  • aims to keep field groups even (See Warn::UnevenFields for explanation of field group) evenness
  • skips processing field groups found to be uneven, as it is impossible to know which value is intended to go with which other values across the field group
  • converts fields considered empty to nil
  • considers delimiter-only fields to be empty. Runs DelimiterOnlyFieldValues to clear them.

Examples

Source data for both examples below:

[
  {aa: '', ab: '', bb: '', bc: '', bd: ''},
  {aa: 'n|', ab: nil, bb: '|e', bc: '|n', bd: '|e'},
  {aa: 'n', ab: '', bb: 'n', bc: 'e', bd: 'p'},
  {aa: 'n|e', ab: 'e|n', bb: 'n|e', bc: 'e|n', bd: 'ne|'},
  {aa: 'n|', ab: 'e|', bb: '|e', bc: 'n|e', bd: '|e'},
  {aa: '|', ab: '|', bb: 'e||n|', bc: 'n||e|', bd: 'e||p|'},
  {aa: '%NULLVALUE%', ab: '%NULLVALUE%', bb: '%NULLVALUE%|%NULLVALUE%', bc: nil, bd: '|'},
  {aa: '|', ab: '', bb: '%NULLVALUE%|', bc: '%NULLVALUE%|%NULLVALUE%', bd: '%NULLVALUE%|a'},
  {aa: '|', ab: '', bb: '%NULLVALUE%|', bc: '%NULLVALUE%|NULL', bd: '%NULLVALUE%|a'}
]

Value of Kiba::Extend.nullvalue is %NULLVALUE%.

Default behavior

By default, this transform considers the value of Kiba::Extend.nullvalue to represent an empty value.

Used in job as:

transform Delete::EmptyFieldGroups, groups: [%i[aa ab], %i[bb bc bd]], delim: '|'

Results in:

[
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: nil},
  {aa: 'n', ab: nil, bb: 'e', bc: 'n', bd: 'e'},
  {aa: 'n', ab: nil, bb: 'n', bc: 'e', bd: 'p'},
  {aa: 'n|e', ab: 'e|n', bb: 'n|e', bc: 'e|n', bd: 'ne|'},
  {aa: 'n', ab: 'e', bb: '|e', bc: 'n|e', bd: '|e'},
  {aa: nil, ab: nil, bb: 'e|n', bc: 'n|e', bd: 'e|p'},
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: nil},
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: 'a'},
  {aa: nil, ab: nil, bb: nil, bc: 'NULL', bd: 'a'}
]

Treat multiple strings as empty

Note that the array given for treat_as_null here overrides the default value of that parameter. If you want the value of Kiba::Extend.nullvalue to be one of the included values, you must specify it in the array given.

Used in job as:

transform Delete::EmptyFieldGroups,
  groups: [%i[aa ab], %i[bb bc bd]],
  treat_as_null: ['NULL', Kiba::Extend.nullvalue],
  delim: '|'

Results in:

[
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: nil},
  {aa: 'n', ab: nil, bb: 'e', bc: 'n', bd: 'e'},
  {aa: 'n', ab: nil, bb: 'n', bc: 'e', bd: 'p'},
  {aa: 'n|e', ab: 'e|n', bb: 'n|e', bc: 'e|n', bd: 'ne|'},
  {aa: 'n', ab: 'e', bb: '|e', bc: 'n|e', bd: '|e'},
  {aa: nil, ab: nil, bb: 'e|n', bc: 'n|e', bd: 'e|p'},
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: nil},
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: 'a'},
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: 'a'}
]

Do not treat any strings except empty string ('') as empty

Used in job as:

transform Delete::EmptyFieldGroups,
  groups: [%i[aa ab], %i[bb bc bd]],
  treat_as_null: nil,
  delim: '|'

Results in:

[
  {aa: nil, ab: nil, bb: nil, bc: nil, bd: nil},
  {aa: 'n', ab: nil, bb: 'e', bc: 'n', bd: 'e'},
  {aa: 'n', ab: nil, bb: 'n', bc: 'e', bd: 'p'},
  {aa: 'n|e', ab: 'e|n', bb: 'n|e', bc: 'e|n', bd: 'ne|'},
  {aa: 'n', ab: 'e', bb: '|e', bc: 'n|e', bd: '|e'},
  {aa: nil, ab: nil, bb: 'e|n', bc: 'n|e', bd: 'e|p'},
  {aa: '%NULLVALUE%', ab: '%NULLVALUE%', bb: '%NULLVALUE%|%NULLVALUE%', bc: nil, bd: nil},
  {aa: nil, ab: nil, bb: '%NULLVALUE%|', bc: '%NULLVALUE%|%NULLVALUE%', bd: '%NULLVALUE%|a'},
  {aa: nil, ab: nil, bb: '%NULLVALUE%|', bc: '%NULLVALUE%|NULL', bd: '%NULLVALUE%|a'}
]

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(groups:, delim: Kiba::Extend.delim, treat_as_null: Kiba::Extend.nullvalue) ⇒ EmptyFieldGroups

Returns a new instance of EmptyFieldGroups.

Parameters:

  • groups (Array(Array(Symbol)))

    Each of the arrays inside groups should list all fields that are part of a repeating field group or field subgroup

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

    delimiter used to split/join field values

  • treat_as_null (nil, String, Array(String)) (defaults to: Kiba::Extend.nullvalue)

    values aside from nil and '' that should be treated as null/empty when removing empty field groups

Since:

  • 2.9.0



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/kiba/extend/transforms/delete/empty_field_groups.rb', line 135

def initialize(groups:, delim: Kiba::Extend.delim,
  treat_as_null: Kiba::Extend.nullvalue)
  @groups = groups
  @delim = delim
  @null_vals = treat_as_null ? [treat_as_null].flatten.sort_by do |v|
                                 v.length
                               end.reverse : []
  @delim_only_cleaner = Delete::DelimiterOnlyFieldValues.new(
    fields: groups.flatten,
    delim: delim,
    treat_as_null: null_vals
  )
  @evenness_checkers = groups.map do |fields|
    Helpers::FieldEvennessChecker.new(fields: fields, delim: delim)
  end
  @value_getters = groups.map do |fields|
    Helpers::FieldValueGetter.new(fields: fields, delim: delim)
  end
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



156
157
158
159
160
161
162
163
164
# File 'lib/kiba/extend/transforms/delete/empty_field_groups.rb', line 156

def process(row)
  delim_only_cleaner.process(row)
  groups.each_with_index do |group, idx|
    process_group(row, group, evenness_checkers[idx],
      value_getters[idx])
  end

  row
end