Class: Kiba::Extend::Transforms::Helpers::FieldValueGetter
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Helpers::FieldValueGetter
- Defined in:
- lib/kiba/extend/transforms/helpers/field_value_getter.rb
Overview
Returns values of specfied fields that meet the specified criteria
Instance Method Summary collapse
-
#call(row) ⇒ Object
-
#initialize(fields:, delim: Kiba::Extend.delim, discard: %i[nil empty delim], treat_as_null: nil) ⇒ FieldValueGetter
constructor
A new instance of FieldValueGetter.
Constructor Details
#initialize(fields:, delim: Kiba::Extend.delim, discard: %i[nil empty delim], treat_as_null: nil) ⇒ FieldValueGetter
Returns a new instance of FieldValueGetter.
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
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 |