Class: Itiel::Transform::RenameColumn

Inherits:
Object
  • Object
show all
Includes:
Nameable, ChainedStep
Defined in:
lib/itiel/transform/rename_column.rb

Overview

Renames a column in the data stream

Usage:

@transformer = Itiel::Transform::RenameColumn.new("order_id" => "id")

This would rename the order_id column in the input stream to id

Instance Attribute Summary collapse

Attributes included from Nameable

#debug, #step_name

Attributes included from ChainedStep

#next_step

Instance Method Summary collapse

Methods included from ChainedStep

#input=

Constructor Details

#initialize(*args) ⇒ RenameColumn

Returns a new instance of RenameColumn.



18
19
20
# File 'lib/itiel/transform/rename_column.rb', line 18

def initialize(*args)
  self.mappings = args
end

Instance Attribute Details

#mappingsObject

Returns the value of attribute mappings.



16
17
18
# File 'lib/itiel/transform/rename_column.rb', line 16

def mappings
  @mappings
end

Instance Method Details

#transform!(input_stream) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/itiel/transform/rename_column.rb', line 22

def transform!(input_stream)
  old_keys = self.mappings.first.keys
  all_keys = input_stream.first.keys

  transformed_output = []
  input_stream.each do |object|
    element = {}
    all_keys.each do |k|
      if old_keys.include?(k)
        element[self.mappings.first[k]] = object[k]
      else
        element[k] = object[k]
      end
    end
    transformed_output << element
  end

  transformed_output
end