Class: Kiba::Extend::Transforms::Reshape::FieldsToFieldGroupWithConstant
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Reshape::FieldsToFieldGroupWithConstant
- Defined in:
- lib/kiba/extend/transforms/reshape/fields_to_field_group_with_constant.rb
Overview
Convenience transform to rename one or more fields and add a field with a constant value, by default ensuring explicit empty field values and field evenness expected in field groups. Runs the following other transforms, which you will want to understand before using this one:
- Kiba::Extend::Transforms::Rename::Fields
- Kiba::Extend::Transforms::Replace::EmptyFieldValues
- Clean::EvenFieldValues
- Delete::EmptyFieldGroups
Examples
Source data:
[
{note: 'foo', date: '2022'},
{note: nil, date: '2022'},
{note: 'foo', date: nil},
{note: '', date: nil},
{note: 'foo|bar|baz', date: '2022|2021'},
{note: 'foo|bar', date: nil},
{note: 'foo|bar|baz', date: '2022||2021'},
{note: '|bar|baz', date: '2022|2021'},
{note: 'foo|bar|', date: '2022|2021'},
]
Value of Kiba::Extend.nullvalue
= '%NULLVALUE%'
. Value of Kiba::Extend.delim
= '|'
.
Default behavior
Used in job as:
transform Reshape::FieldsToFieldGroupWithConstant,
fieldmap: {note: :a_note, date: :a_date},
constant_target: :a_type,
constant_value: 'a thing'
Results in:
[
{a_type: 'a thing', a_note: 'foo', a_date: '2022'},
{a_type: 'a thing', a_note: nil, a_date: '2022'},
{a_type: 'a thing', a_note: 'foo', a_date: nil},
{a_type: nil, a_note: nil, a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|2021|%NULLVALUE%'},
{a_type: 'a thing|a thing', a_note: 'foo|bar', a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|%NULLVALUE%|2021'},
{a_type: 'a thing|a thing|a thing', a_note: '%NULLVALUE%|bar|baz', a_date: '2022|2021|%NULLVALUE%'},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|%NULLVALUE%', a_date: '2022|2021|%NULLVALUE%'}
]
With custom treat_as_null
string
Used in job as:
transform Reshape::FieldsToFieldGroupWithConstant,
fieldmap: {note: :a_note, date: :a_date},
constant_target: :a_type,
constant_value: 'a thing',
treat_as_null: '%BLANK%'
Results in:
[
{a_type: 'a thing', a_note: 'foo', a_date: '2022'},
{a_type: 'a thing', a_note: nil, a_date: '2022'},
{a_type: 'a thing', a_note: 'foo', a_date: nil},
{a_type: nil, a_note: nil, a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|2021|%BLANK%'},
{a_type: 'a thing|a thing', a_note: 'foo|bar', a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|%BLANK%|2021'},
{a_type: 'a thing|a thing|a thing', a_note: '%BLANK%|bar|baz', a_date: '2022|2021|%BLANK%'},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|%BLANK%', a_date: '2022|2021|%BLANK%'}
]
With evener: :value
(repeat final value of field to even out field values)
Used in job as:
transform Reshape::FieldsToFieldGroupWithConstant,
fieldmap: {note: :a_note, date: :a_date},
constant_target: :a_type,
constant_value: 'a thing',
evener: :value
Results in:
[
{a_type: 'a thing', a_note: 'foo', a_date: '2022'},
{a_type: 'a thing', a_note: nil, a_date: '2022'},
{a_type: 'a thing', a_note: 'foo', a_date: nil},
{a_type: nil, a_note: nil, a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|2021|2021'},
{a_type: 'a thing|a thing', a_note: 'foo|bar', a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|%NULLVALUE%|2021'},
{a_type: 'a thing|a thing|a thing', a_note: '%NULLVALUE%|bar|baz', a_date: '2022|2021|2021'},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|%NULLVALUE%', a_date: '2022|2021|2021'}
]
With enforce_evenness: false
Used in job as:
transform Reshape::FieldsToFieldGroupWithConstant,
fieldmap: {note: :a_note, date: :a_date},
constant_target: :a_type,
constant_value: 'a thing',
enforce_evenness: false
Results in:
[
{a_type: 'a thing', a_note: 'foo', a_date: '2022'},
{a_type: 'a thing', a_note: nil, a_date: '2022'},
{a_type: 'a thing', a_note: 'foo', a_date: nil},
{a_type: nil, a_note: nil, a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|2021'},
{a_type: 'a thing|a thing', a_note: 'foo|bar', a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|%NULLVALUE%|2021'},
{a_type: 'a thing|a thing|a thing', a_note: '%NULLVALUE%|bar|baz', a_date: '2022|2021'},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|%NULLVALUE%', a_date: '2022|2021'}
]
With treat_as_null: nil
and evener: '%NULL%
Used in job as:
transform Reshape::FieldsToFieldGroupWithConstant,
fieldmap: {note: :a_note, date: :a_date},
constant_target: :a_type,
constant_value: 'a thing',
treat_as_null: nil,
evener: '%NULL%'
Results in:
[
{a_type: 'a thing', a_note: 'foo', a_date: '2022'},
{a_type: 'a thing', a_note: nil, a_date: '2022'},
{a_type: 'a thing', a_note: 'foo', a_date: nil},
{a_type: nil, a_note: nil, a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022|2021|%NULL%'},
{a_type: 'a thing|a thing', a_note: 'foo|bar', a_date: nil},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|baz', a_date: '2022||2021'},
{a_type: 'a thing|a thing|a thing', a_note: '|bar|baz', a_date: '2022|2021|%NULL%'},
{a_type: 'a thing|a thing|a thing', a_note: 'foo|bar|', a_date: '2022|2021|%NULL%'}
]
Instance Method Summary collapse
-
#initialize(fieldmap:, constant_target:, constant_value:, delim: Kiba::Extend.delim, replace_empty: true, treat_as_null: Kiba::Extend.nullvalue, enforce_evenness: true, evener: nil, uneven_warning: true, remove_empty_groups: true) ⇒ FieldsToFieldGroupWithConstant
constructor
A new instance of FieldsToFieldGroupWithConstant.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fieldmap:, constant_target:, constant_value:, delim: Kiba::Extend.delim, replace_empty: true, treat_as_null: Kiba::Extend.nullvalue, enforce_evenness: true, evener: nil, uneven_warning: true, remove_empty_groups: true) ⇒ FieldsToFieldGroupWithConstant
Returns a new instance of FieldsToFieldGroupWithConstant.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/kiba/extend/transforms/reshape/fields_to_field_group_with_constant.rb', line 198 def initialize(fieldmap:, constant_target:, constant_value:, delim: Kiba::Extend.delim, replace_empty: true, treat_as_null: Kiba::Extend.nullvalue, enforce_evenness: true, evener: nil, uneven_warning: true, remove_empty_groups: true) @renamer = Rename::Fields.new(fieldmap: fieldmap) @renamed = fieldmap.values @target = constant_target @value = constant_value @delim = delim @replace_empty = replace_empty @treat_as_null = treat_as_null.nil? ? "" : treat_as_null @enforce_evenness = enforce_evenness @evener = evener.nil? ? treat_as_null : evener @uneven_warning = uneven_warning @remove_empty_groups = remove_empty_groups @empty_replacer = Replace::EmptyFieldValues.new( fields: @renamed, value: treat_as_null, delim: delim, treat_as_null: treat_as_null ) @even_xform = Clean::EvenFieldValues.new( fields: @renamed, evener: @evener, delim: delim, warn: uneven_warning ) @group_cleaner = Delete::EmptyFieldGroups.new( groups: [[renamed, target].flatten], delim: delim, treat_as_null: treat_as_null ) @value_getter = Helpers::FieldValueGetter.new( fields: renamed, delim: delim, treat_as_null: treat_as_null ) end |
Instance Method Details
#process(row) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/kiba/extend/transforms/reshape/fields_to_field_group_with_constant.rb', line 245 def process(row) renamer.process(row) vals = value_getter.call(row) if vals.empty? row[target] = nil else empty_replacer.process(row) if replace_empty max_vals = find_max_vals(row) add_constant(row, max_vals) even_xform.process(row) if enforce_evenness group_cleaner.process(row) if remove_empty_groups end row end |