Class: Kiba::Extend::Transforms::Clean::EvenFieldValues

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/clean/even_field_values.rb

Overview

Ensures even field values across multiple fields (i.e. a field group) by appending some value to the end of fields containing fewer values.

See explanation of “even fields” at Warn::UnevenFields

This transform appends the value of @evener to field values as necessary to achieve evenness across fields. So, with evener: '%NULLVALUE%':

 {a_foo: 'af', a_bar: 'ab', a_baz: 'az|zz'}

is transformed to:

  {a_foo: 'af|%NULLVALUE%', a_bar: 'ab|%NULLVALUE%', a_baz: 'az|zz'}

However, what if af and ab are intended to go with zz instead of az? Or what if, in this situation, you really want:

 {a_foo: 'af|af', a_bar: 'ab|ab', a_baz: 'az|zz'}

Only you can be sure, so, by default, you get a warning whenever a source gets padded to enforce evenness. You can disable warnings by including warn: false in your transform set-up.

Examples

Default use

Source data:

[
  {foo: 'a', bar: 'b', baz: 'c'},
  {foo: '', bar: nil, baz: 'c'},
  {foo: 'a|a|a', bar: '|b', baz: 'c'}
]

Used as:

transform Clean::EvenFieldValues, fields: %i[foo bar baz], delim: '|'

Results in:

[
  {foo: 'a', bar: 'b', baz: 'c'},
  {foo: '', bar: nil, baz: 'c'},
  {
    foo: 'a|a|a',
    bar: '|b|%NULLVALUE%',
    baz: 'c|%NULLVALUE%|%NULLVALUE%'
  }
]

NOTE: nil or empty field values are skipped altogether

Custom evener (String)

Source data:

[
  {foo: 'a|a|a', bar: '|b', baz: 'c'}
]

Used as:

transform Clean::EvenFieldValues,
  fields: %i[foo bar baz],
  delim: '|',
  evener: '%BLANK%'

Results in:

[
  {foo: 'a|a|a', bar: '|b|%BLANK%', baz: 'c|%BLANK%|%BLANK%'}
]

Custom evener (:value)

This setting causes the last value in the field before padding/evening the field to be repeated as necessary to achieve evenness across fields in the group.

Source data:

[
  {foo: '', bar: nil, baz: 'c'},
  {foo: 'a|a|a', bar: '|b', baz: 'c'},
  {foo: 'a|a|a', bar: 'b|', baz: 'c|a'}
]

Used as:

transform Clean::EvenFieldValues,
  fields: %i[foo bar baz],
  delim: '|',
  evener: :value

Results in:

[
  {foo: '', bar: nil, baz: 'c'},
  {foo: 'a|a|a', bar: '|b|b', baz: 'c|c|c'},
  {foo: 'a|a|a', bar: 'b||', baz: 'c|a|a'},
]

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(fields:, treat_as_null: Kiba::Extend.nullvalue, evener: Kiba::Extend.nullvalue, delim: Kiba::Extend.delim, warn: true) ⇒ EvenFieldValues

Returns a new instance of EvenFieldValues.

Parameters:

  • fields (Array(Symbol))

    fields across which to even field values

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

    value(s) to treat as empty when determining if the entire field is empty or not

  • evener (String, :value) (defaults to: Kiba::Extend.nullvalue)

    value used to even out uneven field values. If given a String, that string will be appended to even out fields. If :value, the final value in the field needing evening will be repeated to even out the field.

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

    used to split/join multiple values in a field

  • warn (Boolean) (defaults to: true)

    whether to print warning of uneven fields to STDOUT

Since:

  • 2.9.0



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kiba/extend/transforms/clean/even_field_values.rb', line 149

def initialize(
  fields:,
  treat_as_null: Kiba::Extend.nullvalue,
  evener: Kiba::Extend.nullvalue,
  delim: Kiba::Extend.delim,
  warn: true
)
  @fields = [fields].flatten
  @treat_as_null = treat_as_null.nil? ? [] : [treat_as_null].flatten
  @evener = evener
  @delim = delim
  @warn = warn

  @value_getter = Helpers::FieldValueGetter.new(fields: fields,
    treat_as_null: treat_as_null, delim: delim)
  @checker = Helpers::FieldEvennessChecker.new(fields: fields,
    delim: delim)
  @warner = Warn::UnevenFields.new(fields: fields, delim: delim)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/kiba/extend/transforms/clean/even_field_values.rb', line 170

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

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

  warner.process(row) if warn
  max = find_max_vals(row)
  pad_uneven_values(row, chk, max)

  row
end