Class: Kiba::Extend::Transforms::Rename::Field
- Inherits:
-
Object
- Object
- Kiba::Extend::Transforms::Rename::Field
- 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
Instance Method Summary collapse
-
#initialize(from:, to:) ⇒ Field
constructor
A new instance of Field.
-
#process(row) ⇒ Object
Methods included from SingleWarnable
Constructor Details
#initialize(from:, to:) ⇒ Field
Returns a new instance of Field.
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
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 |