Class: Kiba::Extend::Transforms::Explode::ColumnsRemappedInNewRows

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/explode.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.

Example

Input table:

| f1           | c1          | f2         | c2    | season | f3          |
|--------------+-------------+------------+-------+--------+-------------|
| strawberry   | red         | blueberry  | blue  | spring | cherry      |
| fig;honeydew | brown;green | watermelon | green | summer | nil         |
| nil          | nil         | nil        | nil   | winter | grapefruit  |
| nil          | nil         | nil        | nil   | autumn | nil         |

Used in pipeline as:

transform Explode::ColumnsRemappedInNewRows,
  remap_groups: [
    [:f1, :c1],
    [:f2, :c2],
    [:f3]
  ],
 map_to: [:fruit, :color]

Results in:

| fruit        | color       | season |
|--------------+-------------+--------|
| strawberry   | red         | spring |
| blueberry    | blue        | spring |
| cherry       | nil         | spring |
| fig;honeydew | brown;green | summer |
| watermelon   | green       | summer |
| grapefruit   | nil         | winter |
| nil          | nil         | autumn |

Things to notice

  • 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

Constructor Details

#initialize(remap_groups:, map_to:) ⇒ ColumnsRemappedInNewRows

Returns a new instance of ColumnsRemappedInNewRows.

Parameters:

  • remap_groups (Array(Array(Symbol)))

    The existing field groups that should be



64
65
66
67
# File 'lib/kiba/extend/transforms/explode.rb', line 64

def initialize(remap_groups:, map_to:)
  @groups = remap_groups
  @map = map_to
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/kiba/extend/transforms/explode.rb', line 70

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