Class: Kiba::Extend::Transforms::Clean::AlphabetizeFieldValues

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/kiba/extend/transforms/clean.rb

Overview

Note:

This transformation does NOT sort the ROWS in a dataset. It sorts values within individual fields of a row

Sorts the multiple values within a field alphabetically

Examples

Input table:

| type                         |
|------------------------------|
| Person;unmapped;Organization |
| ;                            |
| nil                          |
|                              |
| Person;notmapped             |
| %NULLVALUE%;apple            |
| oatmeal;%NULLVALUE%          |

Used in pipeline as:

 transform Clean::AlphabetizeFieldValues,
   fields: %i[type],
   delim: ';',
   usenull: false,
   direction: :asc

Results in:

| type                         |
|------------------------------|
| Organization;Person;unmapped |
| ;                            |
| nil                          |
|                              |
| notmapped;Person             |
| apple;%NULLVALUE%            |
| %NULLVALUE%;oatmeal          |

Used in pipeline as:

 transform Clean::AlphabetizeFieldValues,
   fields: %i[type],
   delim: ';',
   usenull: false,
   direction: :desc

Results in:

| type                         |
|------------------------------|
| unmapped;Person;Organization |
| ;                            |
| nil                          |
|                              |
| Person;notmapped             |
| %NULLVALUE%;apple            |
| oatmeal;%NULLVALUE%          |

Used in pipeline as:

 transform Clean::AlphabetizeFieldValues,
   fields: %i[type],
   delim: ';',
   usenull: true,
   direction: :asc

Results in:

| type                         |
|------------------------------|
| Organization;Person;unmapped |
| ;                            |
| nil                          |
|                              |
| notmapped;Person             |
| apple;%NULLVALUE%            |
| oatmeal;%NULLVALUE%          |

Used in pipeline as:

 transform Clean::AlphabetizeFieldValues,
   fields: %i[type],
   delim: ';',
   usenull: true,
   direction: :desc

Results in:

| type                         |
|------------------------------|
| unmapped;Person;Organization |
| ;                            |
| nil                          |
|                              |
| Person;notmapped             |
| %NULLVALUE%;apple            |
| %NULLVALUE%;oatmeal          |

Instance Method Summary collapse

Methods included from Helpers

empty?

Constructor Details

#initialize(fields:, delim:, usenull: false, direction: :asc) ⇒ AlphabetizeFieldValues

Returns a new instance of AlphabetizeFieldValues.

Parameters:

  • fields (Array(Symbol))

    names of fields to sort

  • delim (String)

    Character(s) on which to split field values

  • usenull (Boolean) (defaults to: false)

    Whether to treat Kiba::Extend.nullvalue as a blank in processing

  • direction (:asc, :desc) (defaults to: :asc)

    Direction in which to sort field values



137
138
139
140
141
142
143
144
145
# File 'lib/kiba/extend/transforms/clean.rb', line 137

def initialize(fields:, delim:, usenull: false, direction: :asc)
  @fields = [fields].flatten
  @delim = delim
  @usenull = usenull
  @direction = direction
  nv = usenull ? Kiba::Extend.nullvalue : nil
  @value_getter = Helpers::FieldValueGetter.new(fields: fields,
    delim: delim, treat_as_null: nv)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


148
149
150
151
152
153
154
155
# File 'lib/kiba/extend/transforms/clean.rb', line 148

def process(row)
  value_getter.call(row).each do |field, val|
    next unless val[delim]

    row[field] = sort_values(val.split(delim)).join(delim)
  end
  row
end