Class: Kiba::Extend::Transforms::Explode::ColumnsRemappedInNewRows
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Explode::ColumnsRemappedInNewRows
- Defined in:
- lib/kiba/extend/transforms/explode/columns_remapped_in_new_rows.rb
Overview
This one is hard to succintly describe! Use it if you have specific fields grouped together in a row, and you want to abstract them so the fields are broader, and you reduce the number of fields.
In the example, 3 fields in each row represent fruit names and 2 fields in each row represent colors. We want more rows, but each row should have only one fruit column and color column.
Things to notice in example:
- The fact that some field values are multivalued is completely ignored
- If all the values for a given remap group are blank, no row is added
- Values in fields not included in
remap_groups
are copied to every row created
Instance Method Summary collapse
-
#initialize(remap_groups:, map_to:) ⇒ ColumnsRemappedInNewRows
constructor
A new instance of ColumnsRemappedInNewRows.
-
#process(row) ⇒ Object
Constructor Details
#initialize(remap_groups:, map_to:) ⇒ ColumnsRemappedInNewRows
Returns a new instance of ColumnsRemappedInNewRows.
69 70 71 72 |
# File 'lib/kiba/extend/transforms/explode/columns_remapped_in_new_rows.rb', line 69 def initialize(remap_groups:, map_to:) @groups = remap_groups @map = map_to end |
Instance Method Details
#process(row) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/kiba/extend/transforms/explode/columns_remapped_in_new_rows.rb', line 75 def process(row) to_new_rows = new_row_groups(row) if to_new_rows.empty? newrow = @map.map do |field| [field, nil] end.to_h.merge(other_fields(row)) yield(newrow) else to_new_rows.each do |grp_data| newrow = @map.zip(grp_data).to_h.merge(other_fields(row)) yield(newrow) end end nil end |