Class: Kiba::Extend::Transforms::Extract::Fields

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

Overview

Note:

This will collapse any source data to a one or two column result. It runs in-memory, so for very large sources, it may take a long time or fail

Extracts the values of the given fields to a single :value column

Inserts a :from_field column recording original field name for the value in each row. This can be turned off, resulting in a single-column result.

Optionally, if given :sep value, splits multi-val fields to separate rows.

Input table:

| foo | bar | baz | boo|
|----------------------|
| a:b | e   | f   |    |
| c   | nil | g   | h  |
| :d  | i:  | j   | k  |

Used in pipeline as:

transform Extract::Fields, fields: %i[foo bar], sep: ':'

Results in:

| value | from_field |
|--------------------|
| a     | foo        |
| b     | foo        |
| e     | bar        |
| c     | foo        |
| d     | foo        |
| i     | bar        |

Used in pipeline as:

transform Extract::Fields, fields: %i[foo bar], source_field_track: false

Results in:

| value |
|--------
| a:b   |
| e     |
| c     |
| :d    |
| i:    |

Since:

  • 2.2.0

Instance Method Summary collapse

Constructor Details

#initialize(fields:, sep: nil, source_field_track: true) ⇒ Fields

Returns a new instance of Fields.

Since:

  • 2.2.0



70
71
72
73
74
75
# File 'lib/kiba/extend/transforms/extract.rb', line 70

def initialize(fields:, sep: nil, source_field_track: true)
  @fields = [fields].flatten
  @sep = sep
  @track = source_field_track
  @rows = []
end

Instance Method Details

#closeObject

Since:

  • 2.2.0



82
83
84
# File 'lib/kiba/extend/transforms/extract.rb', line 82

def close
  @rows.each { |row| yield row }
end

#process(row) ⇒ Object

Since:

  • 2.2.0



77
78
79
80
# File 'lib/kiba/extend/transforms/extract.rb', line 77

def process(row)
  @fields.each { |field| extract_field_value(row, field) }
  nil
end