Class: Kiba::Extend::Transforms::Prepend::FieldToFieldValue

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(target_field:, prepended_field:, sep: "", delete_prepended: false, mvdelim: "", multivalue_prepended_field: false) ⇒ FieldToFieldValue

Returns a new instance of FieldToFieldValue.

Parameters:

  • target_field (Symbol)

    Name of field to prepend to

  • prepended_field (Symbol)

    Name of field whose value should be prepended

  • sep (String) (defaults to: "")

    Text inserted between prepended field value and target field value

  • delete_prepended (Boolean) (defaults to: false)

    Whether or not to delete the prepended_field column after prepending

  • mvdelim (String) (defaults to: "")

    Character(s) on which to split multiple values in target field before prepending. If empty string, behaves as a single value field

  • multivalue_prepended_field (Boolean) (defaults to: false)

    Whether prepended field should be treated as multivalued

Raises:



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

Parameters:

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


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