Skip to content

Instantly share code, notes, and snippets.

@kspurgin
Last active July 7, 2022 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kspurgin/c61cf54eb9d4d0e4d03559f687a3cdb6 to your computer and use it in GitHub Desktop.
Save kspurgin/c61cf54eb9d4d0e4d03559f687a3cdb6 to your computer and use it in GitHub Desktop.
name_this_transform

Combines data from multiple fields following an expected naming pattern (shown in examples below) into final fields that are part of a repeatable fieldgroup.

As an example, let's use example source data:

{displayideas: 'blah', ideadate: '2022-07-07', creditline: 'from someone'}

We want data from :displayideas and :creditline to both get mapped to annotation note fields, and we want the annotation type and annotation date fields set as appropriate. All the annotation fields need to be "even"---i.e. have the same number of values in each field---so they line up/display properly in the target system.

In order to apply this transform, we must first get the source data into the expected format:

{
  idea_annotationtype: 'display idea',
  idea_annotationdate: '2022-07-07',
  idea_annotationnote: 'blah',
  cl_annotationtype: 'credit line',
  cl_annotationdate: '%NULLVALUE%',
  cl_annotationnote: 'from someone'
}

Requirements of this pattern:

  • consistent prefix indicating orig source data
  • suffix is the eventual target field that the values will be combined into
  • prefix and suffix separated by underscore

Then we can use this transform as follows:

transform Cspace::FieldGroupCombiner,
  sources: %i[cl idea],
  targets: %i[annotationtype annotationnote annotationdate],
  delim: '|'

And the result will be:

{
  annotationtype: 'credit line|display idea',
  annotationnote: 'from someone|blah',
  annotationdate: '%NULLVALUE%'|2022-07-07'
}

The {{Clean::EmptyFieldGroups}} transform (with use_nullvalue: true) is applied by this transform after the target fields are compiled, so that, with the following source data:

[
  {a_foo: 'afoo', a_bar: 'abar', b_foo: 'bfoo', b_bar: 'bbar'},
  {a_foo: 'afoo', a_bar: 'abar', b_foo: nil, b_bar: ''},
  {a_foo: 'afoo', a_bar: '%NULLVALUE%', b_foo: '%NULLVALUE%', b_bar: 'bbar'},
  {a_foo: nil, a_bar: nil, b_foo: nil, b_bar: ''},
]

And this usage:

transform Cspace::FieldGroupCombiner,
  sources: %i[a b],
  targets: %i[foo bar],
  delim: '|'

The result will be:

[
  {foo: 'afoo|bfoo', bar: 'abar|bbar'},
  {foo: 'afoo', bar: 'abar'},
  {foo: 'afoo|%NULLVALUE%', bar: '%NULLVALUE%|bbar'},
  {foo: nil, bar: nil},
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment