Class: Kiba::Extend::Transforms::Merge::ConstantValue
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Merge::ConstantValue
- 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
-
#initialize(target:, value:) ⇒ ConstantValue
constructor
A new instance of ConstantValue.
-
#process(row) ⇒ Object
Methods included from SingleWarnable
Constructor Details
#initialize(target:, value:) ⇒ ConstantValue
Returns a new instance of ConstantValue.
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
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 |