Class: Kiba::Extend::Transforms::Fingerprint::Decode
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Fingerprint::Decode
- Defined in:
- lib/kiba/extend/transforms/fingerprint/decode.rb
Overview
Decodes a fingerprint string and expands it into its source fields See Add for how the :fp field in the examples below was derived.
Examples
Input table:
| a | b | c | d | e | fp |
|-----+-----+-----+------+---+----------------------------------|
| ant | bee | nil | deer | | YmVlOzs7bmlsOzs7ZGVlcjs7O2VtcHR5 |
Used in pipeline as:
transform Fingerprint::Decode,
fingerprint: :fp,
source_fields: %i[b c d e],
delim: ';;;',
prefix: 'fp'
Results in:
| a | b | c | d | e | fp | fp_b | fp_c | fp_d | fp_e |
|-----+-----+-----+------+---+----------------------------------+------+------+------+------|
| ant | bee | nil | deer | | YmVlOzs7bmlsOzs7ZGVlcjs7O2VtcHR5 | bee | nil | deer | |
Used in pipeline as:
transform Fingerprint::Decode,
fingerprint: :fp,
source_fields: %i[b c d e],
delim: ';;;',
prefix: 'fp',
delete_fp: true
Results in:
| a | b | c | d | e | fp_b | fp_c | fp_d | fp_e |
|-----+-----+-----+------+---+------+------+------+------|
| ant | bee | nil | deer | | bee | nil | deer | |
Instance Method Summary collapse
-
#initialize(fingerprint:, source_fields:, delim: "␟", prefix: "fp", delete_fp: false) ⇒ Decode
constructor
A new instance of Decode.
-
#process(row) ⇒ Object
Constructor Details
#initialize(fingerprint:, source_fields:, delim: "␟", prefix: "fp", delete_fp: false) ⇒ Decode
Returns a new instance of Decode.
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/kiba/extend/transforms/fingerprint/decode.rb', line 76 def initialize(fingerprint:, source_fields:, delim: "␟", prefix: "fp", delete_fp: false) @fingerprint = fingerprint @source_fields = source_fields @delim = delim @prefix = prefix @delete = delete_fp @num_fields = source_fields.length @target_fields = source_fields.map do |field| "#{prefix}_#{field}".to_sym end @row_ct = 0 end |
Instance Method Details
#process(row) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/kiba/extend/transforms/fingerprint/decode.rb', line 91 def process(row) @row_ct += 1 target_fields.each { |field| row[field] = nil } fpval = row.fetch(fingerprint, nil) row.delete(fingerprint) if delete return row if fpval.blank? decoded = decode(fpval) parts = split(decoded) reconstituted = reconstitute(parts) check_length(reconstituted) target_fields.each_with_index do |target, idx| row[target] = reconstituted[idx] end row end |