Class: Kiba::Extend::Transforms::Rename::Fields

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/rename/fields.rb

Overview

Renames multiple fields at once, given a fieldmap where key is from field and value is to field

This transform works by creating a Field transform for each key/value pair specified in fieldmap, so see Field for examples demonstrating edge case behavior.

Examples:

# Used in pipeline as:
# transform Rename::Fields, fieldmap: {
#   name: :appellation,
#   sex: :gender,
#   color: :color
# }
xform = Rename::Fields.new(fieldmap: {
  name: :appellation,
  sex: :gender,
  color: :color
})
input = [
  {name: "Weddy", sex: "m", color: "pearl gray, greater, pied"},
  {name: "Kernel", sex: "f", color: "buff dundotte"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {appellation: "Weddy", gender: "m", color: "pearl gray, greater, pied"},
  {appellation: "Kernel", gender: "f", color: "buff dundotte"},
]
expect(result).to eq(expected)

Since:

  • 2.8.0

Instance Method Summary collapse

Constructor Details

#initialize(fieldmap:) ⇒ Fields

rubocop:enable Layout/LineLength

Parameters:

  • fieldmap (Hash(Symbol => Symbol))

    Keys are the from fields; values are the to fields

Since:

  • 2.8.0



47
48
49
50
51
52
# File 'lib/kiba/extend/transforms/rename/fields.rb', line 47

def initialize(fieldmap:)
  @fieldmap = fieldmap
  @renamers = fieldmap.map do |from, to|
    Rename::Field.new(from: from, to: to)
  end
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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

Since:

  • 2.8.0



55
56
57
58
# File 'lib/kiba/extend/transforms/rename/fields.rb', line 55

def process(row)
  renamers.each { |renamer| renamer.process(row) }
  row
end