Class: Kiba::Extend::Transforms::CombineValues::FromFieldsWithDelimiter
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::CombineValues::FromFieldsWithDelimiter
- Includes:
- Allable
- Defined in:
- lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb
Overview
Do not use with both prepend_source_field_name and deduplicate set to true. There is no way to safely interpret the desired behavior with this combination of options.
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, delim: " ", prepend_source_field_name: false, delete_sources: true, deduplicate: false, dedupe_delim: nil) ⇒ FromFieldsWithDelimiter
constructor
A new instance of FromFieldsWithDelimiter.
-
#process(row) ⇒ Object
Constructor Details
#initialize(sources: :all, target: :index, delim: " ", prepend_source_field_name: false, delete_sources: true, deduplicate: false, dedupe_delim: nil) ⇒ FromFieldsWithDelimiter
Returns a new instance of FromFieldsWithDelimiter.
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb', line 187 def initialize(sources: :all, target: :index, delim: " ", prepend_source_field_name: false, delete_sources: true, deduplicate: false, dedupe_delim: nil) @fields = [sources].flatten @target = target @delim = delim @del = delete_sources @prepend = prepend_source_field_name @deduplicate = deduplicate @dedupe_delim = dedupe_delim if prepend && deduplicate raise Kiba::Extend::UnsafeParameterComboError, "Do not run #{self.class.name} with both deduplicate and "\ "prepend_source_field_name set to true" end end |
Instance Method Details
#process(row) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/kiba/extend/transforms/combine_values/from_fields_with_delimiter.rb', line 206 def process(row) finalize_fields(row) unless fields_set fieldvals = fields.map { |field| field_and_value(row, field) } .compact .to_h fields.each { |src| row.delete(src) } if del row[target] = combined_value(fieldvals) # 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 row end |