Class: Kiba::Extend::Transforms::Replace::EmptyFieldValues
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Replace::EmptyFieldValues
- 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%' },
]
Instance Method Summary collapse
-
#initialize(fields:, value:, delim: nil, treat_as_null: "") ⇒ EmptyFieldValues
constructor
A new instance of EmptyFieldValues.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fields:, value:, delim: nil, treat_as_null: "") ⇒ EmptyFieldValues
Returns a new instance of EmptyFieldValues.
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
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 |