Class: Kiba::Extend::Transforms::Replace::FieldValueWithStaticMapping

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

Overview

Looks up value of source field in given mapping Hash. Replaces orignal value with the result.

Optional: put result in new target field; look up multiple values from a multivalue source field, provide a fallback value if source value is not found in mapping

Examples

The examples all share the following mapping Hash:

{
  'cb' => 'coral blue',
  'rp' => 'royal purple',
  'p' => 'pied',
  'pl' => 'pearl gray',
  nil => 'undetermined'
}

Initial examples all use the following rows as input:

[
  {name: 'Lazarus', color: 'cb'},
  {name: 'Inkpot', color: 'rp'},
  {name: 'Zipper', color: 'rp|p'},
  {name: 'Divebomber|Earlybird', color: 'pl|pl'},
  {name: 'Vern', color: 'v'},
  {name: 'Clover|Hops', color: 'rp|c'},
  {name: 'New', color: nil},
  {name: 'Old', color: ''},
  {name: 'New|Hunter', color: '|pl'}
]

Using:

transform Replace::FieldValueWithStaticMapping, source: :color, mapping: mapping

Results in:

[
  {name: 'Lazarus', color: 'coral blue'},
    {name: 'Inkpot', color: 'royal purple'},
    {name: 'Zipper', color: 'rp|p'},
    {name: 'Divebomber|Earlybird', color: 'pl|pl'},
    {name: 'Vern', color: 'v'},
    {name: 'Clover|Hops', color: 'rp|c'},
    {name: 'New', color: 'undetermined'},
    {name: 'Old', color: ''},
    {name: 'New|Hunter', color: '|pl'}
]

Using:

transform Replace::FieldValueWithStaticMapping, source: :color, target: :fullcol, mapping: mapping

Results in (showing first row only):

[
  {name: 'Lazarus', fullcol: 'coral blue'},
  ...
]

Using:

transform Replace::FieldValueWithStaticMapping,
  source: :color,
  target: :fullcol,
  mapping: mapping,
  delete_source: false

Results in (showing first row only):

[
  {name: 'Lazarus', color: 'cb', fullcol: 'coral blue'},
  ...
]

Using:

transform Replace::FieldValueWithStaticMapping,
  source: :color,
  mapping: mapping,
  delim: '|'

Results in:

[
  {name: 'Lazarus', color: 'coral blue'},
  {name: 'Inkpot', color: 'royal purple'},
  {name: 'Zipper', color: 'royal purple|pied'},
  {name: 'Divebomber|Earlybird', color: 'pearl gray|pearl gray'},
  {name: 'Vern', color: 'v'},
  {name: 'Clover|Hops', color: 'royal purple|c'},
  {name: 'New', color: 'undetermined'},
  {name: 'Old', color: ''},
  {name: 'New|Hunter', color: '|pearl gray'}
]

The remaining examples use only the following rows as input:

[
  {name: 'Vern', color: 'v'},
  {name: 'Clover|Hops', color: 'rp|c'},
  {name: 'New', color: nil},
  {name: 'Old', color: ''},
  {name: 'New|Hunter', color: '|pl'}
]

Using:

transform Replace::FieldValueWithStaticMapping,
  source: :color,
  mapping: mapping,
  delim: '|',
  fallback_val: :nil

Results in:

[
  {name: 'Vern', color: nil},
  {name: 'Clover|Hops', color: 'royal purple|'},
  {name: 'New', color: 'undetermined'},
  {name: 'Old', color: nil},
  {name: 'New|Hunter', color: '|pearl gray'}
]

Using:

transform Replace::FieldValueWithStaticMapping,
  source: :color,
  mapping: mapping,
  delim: '|',
  fallback_val: 'nope'

Results in:

[
  {name: 'Vern', color: 'nope'},
  {name: 'Clover|Hops', color: 'royal purple|nope'},
  {name: 'New', color: 'undetermined'},
  {name: 'Old', color: 'nope'},
  {name: 'New|Hunter', color: 'nope|pearl gray'}
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source:, mapping:, target: nil, fallback_val: :orig, delete_source: true, delim: nil) ⇒ FieldValueWithStaticMapping

Returns a new instance of FieldValueWithStaticMapping.

Parameters:

  • source (Symbol)

    the field containing the value to look up for mapping

  • target (nil, Symbol) (defaults to: nil)

    optional new field in which to put the mapped/looked up result

  • mapping (Hash)

    keys = source field values

  • fallback_val (:orig, :nil, String) (defaults to: :orig)

    value to use if no match for source value is found in mapping

  • delete_source (Boolean) (defaults to: true)

    whether to remove source field after mapping. Has no effect if a different target field is not given

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

    if a value is given, turns on “multival” mode, splitting the whole field value on the string given (since 3.0.0)



219
220
221
222
223
224
225
226
227
228
# File 'lib/kiba/extend/transforms/replace/field_value_with_static_mapping.rb', line 219

def initialize(source:, mapping:, target: nil, fallback_val: :orig,
  delete_source: true, delim: nil)
  @source = source
  @target = target || source
  @mapping = mapping
  @fallback = fallback_val
  @del = delete_source
  @delim = delim
  @multival = true if @delim
end

Class Method Details

.multival_msgObject



182
183
184
185
186
187
188
# File 'lib/kiba/extend/transforms/replace/field_value_with_static_mapping.rb', line 182

def multival_msg
  <<~MSG
    #{name} no longer supports the `multival` parameter.
    If a `delim` value is given, the transform will operate in multival mode
    TO FIX: remove `multival` parameter, ensuring a `delim` value is given
  MSG
end

.new(source:, mapping:, target: nil, fallback_val: :orig, delete_source: true, delim: nil, multival: nil, sep: nil) ⇒ Object

Overridden to provide more informative/detailed ArgumentError messages for parameters that are removed after not having been deprecated very long.



192
193
194
195
196
197
198
199
200
# File 'lib/kiba/extend/transforms/replace/field_value_with_static_mapping.rb', line 192

def new(source:, mapping:, target: nil, fallback_val: :orig, delete_source: true, delim: nil,
  multival: nil, sep: nil)
  instance = allocate
  fail(ArgumentError, sep_msg) if sep
  fail(ArgumentError, multival_msg) if multival
  instance.send(:initialize, source: source, target: target, mapping: mapping, fallback_val: fallback_val,
    delete_source: delete_source, delim: delim)
  instance
end

.sep_msgObject



202
203
204
205
206
207
# File 'lib/kiba/extend/transforms/replace/field_value_with_static_mapping.rb', line 202

def sep_msg
  <<~MSG
    #{name} no longer supports the `sep` parameter
    TO FIX: change `sep` to `delim`"
  MSG
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/kiba/extend/transforms/replace/field_value_with_static_mapping.rb', line 231

def process(row)
  set_initial_value(row)
  rowval = row[source]
  vals = prep_vals(rowval)

  @fallback_val = get_fallback_vals(vals)

  row[target] = join_result(result(vals))

  row.delete(source) if source != target && del
  row
end