Class: Kiba::Extend::Transforms::Fingerprint::Decode

Inherits:
Object
  • Object
show all
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 |      |

Since:

  • 2.7.1.65

Instance Method Summary collapse

Constructor Details

#initialize(fingerprint:, source_fields:, delim: "␟", prefix: "fp", delete_fp: false) ⇒ Decode

Returns a new instance of Decode.

Parameters:

  • fingerprint (Symbol)

    the name of the field containing fingerprint values

  • source_fields (Array<Symbol>)

    names of fields used to generate the fingerprint

  • delim (String) (defaults to: "␟")

    used to join/split fields before hashing/after decoding. The default value is U+241F / E2 90 9F / Symbol for Unit Separator.

  • prefix (String) (defaults to: "fp")

    added to the names of the decoded fields added to rows

  • delete_fp (Boolean) (defaults to: false)

    whether to delete the given fingerprint field

Since:

  • 2.7.1.65



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

Parameters:

  • row (Hash{ Symbol => String, nil })

Since:

  • 2.7.1.65



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