Class: Kiba::Extend::Transforms::Helpers::FieldValueGetter

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

Overview

Returns values of specfied fields that meet the specified criteria

Since:

  • 2.9.0

Instance Method Summary collapse

Constructor Details

#initialize(fields:, delim: Kiba::Extend.delim, discard: %i[nil empty delim], treat_as_null: nil) ⇒ FieldValueGetter

Returns a new instance of FieldValueGetter.

Parameters:

  • fields (Array(Symbol))

    from which to return values

  • delim (String) (defaults to: Kiba::Extend.delim)
  • discard (Array(:nil, :empty, :delim)) (defaults to: %i[nil empty delim])

    values to discard from returned results. :nil and :empty are self-explanatory. :delim causes delimiter-only fields to be discarded. See DelimOnlyChecker for how delimiter-only status is determined.

  • treat_as_null (nil, String, Array(String)) (defaults to: nil)

    value(s) to treat as null/empty when determining what to discard

Since:

  • 2.9.0



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kiba/extend/transforms/helpers/field_value_getter.rb', line 20

def initialize(fields:, delim: Kiba::Extend.delim,
  discard: %i[nil empty delim], treat_as_null: nil)
  @fields = [fields].flatten
  @delim = delim
  @discard = discard
  @null_vals = treat_as_null ? [treat_as_null].flatten.sort_by do |v|
                                 v.length
                               end.reverse : []
  @delim_only_checker = DelimOnlyChecker.new(delim: delim,
    treat_as_null: treat_as_null, blank_result: false)
end

Instance Method Details

#call(row) ⇒ Object

Since:

  • 2.9.0



32
33
34
35
36
37
38
39
# File 'lib/kiba/extend/transforms/helpers/field_value_getter.rb', line 32

def call(row)
  raw = fields.map { |field| [field, row[field]] }.to_h
  null_cleaned = null_vals.empty? ? raw : clean_nulls(raw)
  nil_removed = discard.any?(:nil) ? remove_nils(null_cleaned) : null_cleaned
  empty_removed = discard.any?(:empty) ? remove_empty(nil_removed) : nil_removed
  delim_only_removed = discard.any?(:delim) ? remove_delim_only(empty_removed) : empty_removed
  delim_only_removed.keys.map { |field| [field, row[field]] }.to_h
end