Class: Kiba::Extend::Transforms::Merge::ConstantValue

Inherits:
Object
  • Object
show all
Includes:
SingleWarnable
Defined in:
lib/kiba/extend/transforms/merge/constant_value.rb

Overview

Merges given value into the given target field in every row. Target field is added new. If it already exists, values in target field are overridden by contant value.

Examples

Source data:

{name: 'Weddy', sex: 'm', source: 'adopted'},
{name: 'Kernel', sex: 'f', source: 'adopted', species: 'Numida meleagris'}

Used in pipeline as:

transform Merge::ConstantValue, target: :species, value: 'guinea fowl'

Results in:

{name: 'Weddy', sex: 'm', source: 'adopted', species: 'guinea fowl'},
{name: 'Kernel', sex: 'f', source: 'adopted', species: 'guinea fowl'}

A warning will be printed to STDOUT since the existing species value is overwritten in the second row.

Instance Method Summary collapse

Methods included from SingleWarnable

single_warnings

Constructor Details

#initialize(target:, value:) ⇒ ConstantValue

Returns a new instance of ConstantValue.

Parameters:

  • target (Symbol)

    target field in which to enter constant value

  • value (String)

    the constant value to enter in target field



40
41
42
43
44
# File 'lib/kiba/extend/transforms/merge/constant_value.rb', line 40

def initialize(target:, value:)
  @target = target
  @value = value
  setup_single_warning
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


47
48
49
50
51
52
53
54
# File 'lib/kiba/extend/transforms/merge/constant_value.rb', line 47

def process(row)
  unless row.fetch(target, nil).blank?
    add_single_warning("Any values in existing `#{target}` field will be overwritten with `#{value}`")
  end

  row[target] = value
  row
end