Class: Kiba::Extend::Transforms::CombineValues::FromFieldsWithDelimiter
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::CombineValues::FromFieldsWithDelimiter
- Includes:
- Allable, SepDeprecatable
- Defined in:
- lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb
Overview
Combine values from given fields into the target field.
This is like the CONCATENATE function in many spreadsheets. The given
delim
value is used as a separator between the combined values.
Note: Used with defaults, this has the same function as FullRecord, but deletes the source fields. FullRecord retains source fields by default.
If target field has the same name as one of the source fields, and
delete_sources
= true, no values are lost. The target field
is not deleted.
Blank/nil values are dropped. If prepend_source_field_name = true
,
names of blank/nil fields are omitted
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(sources: :all, target: :index, sep: nil, delim: nil, prepend_source_field_name: false, delete_sources: true) ⇒ FromFieldsWithDelimiter
constructor
A new instance of FromFieldsWithDelimiter.
-
#process(row) ⇒ Object
param row [Hash{ Symbol => String, nil }].
Methods included from SepDeprecatable
Constructor Details
#initialize(sources: :all, target: :index, sep: nil, delim: nil, prepend_source_field_name: false, delete_sources: true) ⇒ FromFieldsWithDelimiter
Returns a new instance of FromFieldsWithDelimiter.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb', line 89 def initialize(sources: :all, target: :index, sep: nil, delim: nil, prepend_source_field_name: false, delete_sources: true) @fields = [sources].flatten @target = target @delim = usedelim( sepval: sep, delimval: delim, calledby: self, default: " " ) @del = delete_sources @prepend = prepend_source_field_name end |
Instance Method Details
#process(row) ⇒ Object
param row [Hash{ Symbol => String, nil }]
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb', line 104 def process(row) finalize_fields(row) unless fields_set vals = fields.map { |src| row.fetch(src, nil) } .map { |v| v.blank? ? nil : v } if prepend pvals = [] vals.each_with_index do |val, i| val = "#{fields[i]}: #{val}" unless val.nil? pvals << val end vals = pvals end val = vals.compact.join(delim) row[target] = val.empty? ? nil : val fields.each { |src| row.delete(src) unless src == target } if del row end |