Class: Kiba::Extend::Transforms::Rename::Field

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

Overview

Renames one field

Example notes

### 1 - :from field exists

:from field renamed to :to field. Not much to see here.

2 - :to field already exists

:sex is renamed to :gender, overwriting the existing value of :gender (unknown) with value of :sex (m). The transform emits a warning about this

3 - :from field does not exist

Row is passed through unchanged, and the transform emits a warning about not being able to rename a field that doesn’t exist.

4 - :from and :to field are the same

This seems like a real weird edge case from the perspective of defining transforms manually, but it can happen when transform definitions are programmatically generated from configuration.

rubocop:enable Layout/LineLength

Examples:

1 - :from field exists

# Used in pipeline as:
# transform Rename::Field,
#   from: :sex,
#   to: :gender
xform = Rename::Field.new(
  from: :sex,
  to: :gender
)
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 = [
  {name: "Weddy", gender: "m", color: "pearl gray, greater, pied"},
  {name: "Kernel", gender: "f", color: "buff dundotte"},
]
expect(result).to eq(expected)

2 - :to field already exists

# Used in pipeline as:
# transform Rename::Field,
#   from: :sex,
#   to: :gender
xform = Rename::Field.new(
  from: :sex,
  to: :gender
)
input = [
  {name: "Weddy", sex: "m", gender: "unknown"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {name: "Weddy", gender: "m"}
]
expect(xform.send(:single_warnings)).to include(
  "Renaming `sex` to `gender` overwrites existing `gender` field data"
)
expect(result).to eq(expected)

3 - :from field does not exist

# Used in pipeline as:
# transform Rename::Field,
#   from: :sex,
#   to: :gender
xform = Rename::Field.new(
  from: :sex,
  to: :gender
)
input = [
  {name: "Weddy", gender: "unknown"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {name: "Weddy", gender: "unknown"}
]
expect(xform.send(:single_warnings)).to include(
  "Cannot rename field: `sex` does not exist in row"
)
expect(result).to eq(expected)

4 - :from and :to field are the same

# Used in pipeline as:
# transform Rename::Field,
#   from: :gender,
#   to: :gender
xform = Rename::Field.new(
  from: :gender,
  to: :gender
)
input = [
  {name: "Weddy", gender: "unknown"}
]
result = Kiba::StreamingRunner.transform_stream(input, xform)
  .map{ |row| row }
expected = [
  {name: "Weddy", gender: "unknown"}
]
expect(result).to eq(expected)

Instance Method Summary collapse

Methods included from SingleWarnable

single_warnings

Constructor Details

#initialize(from:, to:) ⇒ Field

Returns a new instance of Field.

Parameters:

  • from (Symbol)

    current field name

  • to (Symbol)

    target field name



124
125
126
127
128
# File 'lib/kiba/extend/transforms/rename/field.rb', line 124

def initialize(from:, to:)
  @from = from
  @to = to
  setup_single_warning
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

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


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kiba/extend/transforms/rename/field.rb', line 131

def process(row)
  return row if from == to

  unless row.key?(from)
    add_single_warning("Cannot rename field: `#{from}` does not "\
                       "exist in row")
    return row
  end

  if row.key?(to)
    add_single_warning("Renaming `#{from}` to `#{to}` overwrites "\
                       "existing `#{to}` field data")
  end

  row[to] = row.fetch(from)
  row.delete(from)
  row
end