Class: Kiba::Extend::Transforms::Collapse::FieldsToTypedFieldPair
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Collapse::FieldsToTypedFieldPair
- 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
andphonetype
are kept even - The data in the target fields is in the order of the keys in the
sourcefieldmap
: home, work, mobile, other.
Instance Method Summary collapse
-
#initialize(sourcefieldmap:, datafield:, typefield:, targetsep:, sourcesep: nil, delete_sources: true) ⇒ FieldsToTypedFieldPair
constructor
A new instance of FieldsToTypedFieldPair.
-
#process(row) ⇒ Object
Constructor Details
#initialize(sourcefieldmap:, datafield:, typefield:, targetsep:, sourcesep: nil, delete_sources: true) ⇒ FieldsToTypedFieldPair
Returns a new instance of FieldsToTypedFieldPair.
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
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 |