Class: Kiba::Extend::Transforms::Collapse::FieldsToTypedFieldPair

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/collapse/fields_to_typed_field_pair.rb

Overview

Takes multiple fields like :workphone, :homephone, :mobilephone and produces two new fields like :phone and :phonetype where :phonetype depends on the original field taken from

Examples

Input table:

| work | home     | mobile | other | name |
|------+----------+--------+-------+------|
| 123  | 456      | 789    | 897   | Sue  |
|      | 987;555  |        | 253   | Bob  |
| nil  |          |        | nil   | Mae  |
| 654  | 321      | 257    |       | Sid  |

Used in pipeline as:

 transform Collapse::FieldsToTypedFieldPair,
   sourcefieldmap: { home: 'h', work: 'b', mobile: 'm', other: '' },
   datafield: :phone,
   typefield: :phonetype,
   sourcesep: ';',
   targetsep: '^',
   delete_sources: false

Results in:

| work | home     | mobile | other | phone           | phonetype | name |
|------+----------+--------+-------|-----------------+-----------+------|
| 123  | 456      | 789    | 897   | 456^123^789^897 | h^b^m^    | Sue  |
|      | 987;555  |        | 253   | 987^555^253     | h^h^      | Bob  |
| nil  |          |        | nil   | nil             | nil       | Mae  |
| 654  | 321      | 257    |       | 321^654^257     | h^b^m     | Sid  |

Used in pipeline as:

 transform Collapse::FieldsToTypedFieldPair,
   sourcefieldmap: { home: 'h', work: 'b', mobile: '', other: 'o' },
   datafield: :phone,
   typefield: :phonetype,
   targetsep: '^'

Results in:

| phone           | phonetype | name |
|-----------------+-----------+------|
| 456^123^789^897 | h^b^m^    | Sue  |
| 987;555^253     | h^        | Bob  |
| nil             | nil       | Mae  |
| 321^654^257     | h^b^m     | Sid  |

Notice

  • The number of values in phone and phonetype are kept even
  • The data in the target fields is in the order of the keys in the sourcefieldmap: home, work, mobile, other.

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(sourcefieldmap:, datafield:, typefield:, targetsep:, sourcesep: nil, delete_sources: true) ⇒ FieldsToTypedFieldPair

Returns a new instance of FieldsToTypedFieldPair.

Parameters:

  • sourcefieldmap (Hash{Symbol => String})

    Keys are the names of the source fields. Each key’s value is the type that should be assigned in typefield

  • datafield (Symbol)

    Target field into which the original data value(s) from source fields will be mapped

  • typefield (Symbol)

    Target field into which the type values will be mapped

  • sourcesep (String) (defaults to: nil)

    Delimiter used to split source data into multiple values

  • targetsep (String)

    Delimiter used to join multiple values in target fields

  • delete_sources (Boolean) (defaults to: true)

    Whether to delete source fields after mapping them to target fields

Since:

  • 2.9.0



81
82
83
84
85
86
87
88
89
# File 'lib/kiba/extend/transforms/collapse/fields_to_typed_field_pair.rb', line 81

def initialize(sourcefieldmap:, datafield:, typefield:, targetsep:,
  sourcesep: nil, delete_sources: true)
  @map = sourcefieldmap
  @df = datafield
  @tf = typefield
  @sourcesep = sourcesep
  @targetsep = targetsep
  @del = delete_sources
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.9.0



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/kiba/extend/transforms/collapse/fields_to_typed_field_pair.rb', line 92

def process(row)
  data = []
  type = []
  @map.each_key do |sourcefield|
    vals = row.fetch(sourcefield)
    unless vals.nil?
      vals = @sourcesep.nil? ? [vals] : vals.split(@sourcesep)
      vals.each do |val|
        data << val
        type << @map.fetch(sourcefield, @default_type)
      end
    end
    row.delete(sourcefield) if @del
  end
  row[@df] = data.size.positive? ? data.join(@targetsep) : nil
  row[@tf] = type.size.positive? ? type.join(@targetsep) : nil
  row
end