Class: Kiba::Extend::Transforms::Replace::EmptyFieldValues

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/replace/empty_field_values.rb

Overview

Replace empty field values the given value

Works on single or multivalue fields. Can be given a treat_as_null value to count as empty.

Examples

Defaults (single value, replaces truly nil or empty values)

Source data:

[
  {species: 'guineafowl', name: nil, sex: ''},
  {species: 'guineafowl', name: '%NULL%', sex: '%NULL%'},
  {species: 'guineafowl', name: 'Weddy||Grimace|', sex: ''},
  {species: 'guineafowl', name: '|Weddy|Grimace|', sex: '%NULL%|m|m|'}
]

Used as:

transform Replace::EmptyFieldValues, fields: %i[name sex], value: '%NULLVALUE%'

Results in:

[
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%' },
  {species: 'guineafowl', name: '%NULL%', sex: '%NULL%'},
  {species: 'guineafowl', name: 'Weddy||Grimace|', sex: '%NULLVALUE%'}
  {species: 'guineafowl', name: '|Weddy|Grimace|', sex: '%NULL%|m|m|'}
]

Null placeholder (single value)

Using same source data as above, and transform set up as:

transform Replace::EmptyFieldValues, fields: %i[name sex], value: '%NULLVALUE%',
  treat_as_null: '%NULL%'

Results in:

[
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%' },
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%'},
  {species: 'guineafowl', name: 'Weddy||Grimace|', sex: '%NULLVALUE%'}
  {species: 'guineafowl', name: '|Weddy|Grimace|', sex: '%NULL%|m|m|'}
]

Multivalued (given a delim value)

Using same source data as above, and transform set up as:

transform Replace::EmptyFieldValues, fields: %i[name sex], delim: '|', value: '%NULLVALUE%'

Results in:

[
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%' },
  {species: 'guineafowl', name: '%NULL%', sex: '%NULL%'},
  {species: 'guineafowl', name: 'Weddy|%NULLVALUE%|Grimace|%NULLVALUE%', sex: '%NULLVALUE%'},
  {species: 'guineafowl', name: '%NULLVALUE%|Weddy|Grimace|%NULLVALUE%', sex: '%NULL%|m|m|%NULLVALUE%'}
]

Multivalued (given a delim value) with treat_as_null

Using same source data as above, and transform set up as:

transform Replace::EmptyFieldValues, fields: %i[name sex], delim: '|', value: '%NULLVALUE%',
  treat_as_null: '%NULL%'

Results in:

[
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%' },
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%'},
  {species: 'guineafowl', name: 'Weddy|%NULLVALUE%|Grimace|%NULLVALUE%', sex: '%NULLVALUE%'},
  {species: 'guineafowl', name: '%NULLVALUE%|Weddy|Grimace|%NULLVALUE%', sex: '%NULLVALUE%|m|m|%NULLVALUE%'}
]

Multiple treat_as_null values

Results in:

[
  {species: 'guineafowl', name: '%NULL%', sex: '%NADA%' }
]

transform Replace::EmptyFieldValues, fields: %i[name sex], value: ‘%NULLVALUE%’, treat_as_null: [‘%NULL%’, ‘%NADA%’] ~~~

Results in:

[
  {species: 'guineafowl', name: '%NULLVALUE%', sex: '%NULLVALUE%' },
]

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(fields:, value:, delim: nil, treat_as_null: "") ⇒ EmptyFieldValues

Returns a new instance of EmptyFieldValues.

Parameters:

  • fields (Array(Symbol), Symbol)

    in which to perform replacements

  • value (String)

    replaces the empty value(s)

  • delim (String, nil) (defaults to: nil)

    if provided, replacement of individual empty values in a multivalue field will be performed after splitting on this string

  • treat_as_null (String, Array(String)) (defaults to: "")

    string(s) to treat as empty values

Since:

  • 2.9.0



133
134
135
136
137
138
139
140
141
142
# File 'lib/kiba/extend/transforms/replace/empty_field_values.rb', line 133

def initialize(fields:, value:, delim: nil, treat_as_null: "")
  @fields = [fields].flatten
  @value = value
  @delim = delim
  @treat_as_null = treat_as_null
  @initial_getter = Helpers::FieldValueGetter.new(fields: fields,
    discard: [])
  @replacement_getter = Helpers::FieldValueGetter.new(fields: fields,
    delim: delim)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



145
146
147
148
149
# File 'lib/kiba/extend/transforms/replace/empty_field_values.rb', line 145

def process(row)
  replace_fully_empty_fields(row)
  replace_multival_empty(row) if delim
  row
end