Class: Kiba::Extend::Transforms::Delete::EmptyFieldValues

Inherits:
Object
  • Object
show all
Includes:
Allable
Defined in:
lib/kiba/extend/transforms/delete/empty_field_values.rb

Overview

Note:

Only useful for multi-valued fields

Deletes any empty values from the field. Supports usenull = true to treat the value of Kiba::Extend.nullvalue as empty

Examples

Assuming Kiba::Extend.nullvalue = %NULLVALUE%, and input table:

| data             |
|------------------|
| abc;;;d e f      |
| ;;abc            |
| def;;;;          |
| ;;;;;            |
| ;;;%NULLVALUE%;; |
|                  |
| nil              |

Used in pipeline as:

transform Delete::EmptyFieldValues, fields: [:data], sep: ';'

Results in:

| data        |
|-------------|
| abc;d e f   |
| abc         |
| def         |
|             |
| %NULLVALUE% |
|             |
| nil         |

Used in pipeline as:

transform Delete::EmptyFieldValues, fields: [:data], sep: ';', usenull: true

Results in:

| data      |
|-----------|
| abc;d e f |
| abc       |
| def       |
|           |
|           |
|           |
| nil       |

Instance Method Summary collapse

Constructor Details

#initialize(fields:, sep: nil, delim: nil, usenull: false) ⇒ EmptyFieldValues

Note:

sep will be removed in a future version. DO NOT USE

Returns a new instance of EmptyFieldValues.

Parameters:

  • fields (Array<Symbol>, Symbol)

    field(s) to delete from

  • sep (String) (defaults to: nil)

    DEPRECATED; DO NOT USE

  • delim (String) (defaults to: nil)

    on which to split multivalued fields. Defaults to Kiba::Extend.delim if not provided.

  • usenull (Boolean) (defaults to: false)

    whether to treat Kiba::Extend.nullvalue string as an empty value



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/kiba/extend/transforms/delete/empty_field_values.rb', line 77

def initialize(fields:, sep: nil, delim: nil, usenull: false)
  @fields = [fields].flatten
  @usenull = usenull
  if sep && delim
    puts %(#{Kiba::Extend.warning_label}: Do not use both `sep` and `delim`. Prefer `delim`)
  elsif sep
    puts %(#{Kiba::Extend.warning_label}: The `sep` keyword is being deprecated in a future version. Change it to `delim` in your ETL code.)
    @delim = sep
  else
    @delim = delim || Kiba::Extend.delim
  end
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kiba/extend/transforms/delete/empty_field_values.rb', line 92

def process(row)
  finalize_fields(row) unless fields_set

  fields.each do |field|
    val = row.fetch(field)
    next if val.nil?

    row[field] = val.split(delim)
      .compact
      .reject { |str| Helpers.empty?(str, usenull) }
      .join(delim)
  end
  row
end