Class: Kiba::Extend::Transforms::Prepend::FieldToFieldValue
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Prepend::FieldToFieldValue
- Defined in:
- lib/kiba/extend/transforms/prepend/field_to_field_value.rb
Overview
Adds the value of prepended_field
to the beginning of the value(s) of the target_field
If target field value is blank, it is left blank, even if there is a prepended field value. If there is no value in prepended field, target field is left as-is
Examples
Used in pipeline as:
transform Prepend::FieldToFieldValue, target_field: :a, prepended_field: :b, sep: ': '
Input table:
| a | b |
|-------+-----|
| c | d |
| e;f | g |
| | h |
| i | |
| j;k | l;m |
| o;p;q | r;s |
| ;t | u;v
Results in:
| a | b |
|------------|-----|
| d: c | d |
| g: e;f | g |
| | h |
| i | |
| l;m: j;k | l;m |
| r;s: o;p;q | r;s |
| u;v: ;t | u;v |
Used in pipeline as:
transform Prepend::FieldToFieldValue, target_field: :a, prepended_field: :b, sep: ': ',
delete_prepended: true, mvdelim: ';'
Input table:
| a | b |
|-------+-----|
| c | d |
| e;f | g |
| | h |
| i | |
| j;k | l;m |
| o;p;q | r;s |
| ;t | u;v
Results in:
| a |
|----------------------|
| d: c |
| g: e;g: f |
| |
| i |
| l;m: j;l;m: k |
| r;s: o;r;s: p;r;s: q |
| ;u;v: t |
This probably introduces extra unexpected mvdelim
strings in the result.
If prepended_field
contains the mvdelim
character, you probably want to set
multivalue_prepended_field: true
.
Used in pipeline as:
transform Prepend::FieldToFieldValue, target_field: :a, prepended_field: :b, sep: ': ',
delete_prepended: true, mvdelim: ';',
multivalue_prepended_field: true
Results in:
| a |
|----------------|
| d: c |
| g: e;f |
| |
| i |
| l: j;m: k |
| r: o;s: p;q |
| ;v: t |
If there are more target_field
values than prepend_field
values after they are split,
nothing is prepended to remaining target_field
values.
Defined Under Namespace
Classes: MissingDelimiterError
Instance Method Summary collapse
-
#initialize(target_field:, prepended_field:, sep: "", delete_prepended: false, mvdelim: "", multivalue_prepended_field: false) ⇒ FieldToFieldValue
constructor
A new instance of FieldToFieldValue.
-
#process(row) ⇒ Object
Constructor Details
#initialize(target_field:, prepended_field:, sep: "", delete_prepended: false, mvdelim: "", multivalue_prepended_field: false) ⇒ FieldToFieldValue
Returns a new instance of FieldToFieldValue.
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/kiba/extend/transforms/prepend/field_to_field_value.rb', line 134 def initialize(target_field:, prepended_field:, sep: "", delete_prepended: false, mvdelim: "", multivalue_prepended_field: false) @field = target_field @prepend = prepended_field @sep = sep @delete = delete_prepended @mvdelim = mvdelim @multival_prepend = multivalue_prepended_field raise MissingDelimiterError if @multival_prepend && @mvdelim.blank? end |
Instance Method Details
#process(row) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/kiba/extend/transforms/prepend/field_to_field_value.rb', line 146 def process(row) field_val = row.fetch(@field, nil) prepend_val = row.fetch(@prepend, nil) row.delete(@prepend) if @delete return row if field_val.blank? || prepend_val.blank? values = @mvdelim.blank? ? [field_val] : field_val.split(@mvdelim) if @multival_prepend row[@field] = mv_prepend(row, prepend_val, values) end return row if @multival_prepend row[@field] = sv_prepend(row, prepend_val, values) row end |