Class: Itiel::Transform::MapValues

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

Overview

Maps a field value to different values

Usage:

@transformation = Itiel::Transform::MapValues.new(
  {
    "active" => { true => "yes", false => "no" }
  }
)

This would map all the values on the active column, true to yes and false to no

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(mapping) ⇒ MapValues

Returns a new instance of MapValues.



22
23
24
# File 'lib/itiel/transform/map_values.rb', line 22

def initialize(mapping)
  self.mapping = mapping
end

Instance Attribute Details

#mappingObject

Returns the value of attribute mapping.



20
21
22
# File 'lib/itiel/transform/map_values.rb', line 20

def mapping
  @mapping
end

Instance Method Details

#transform!(input_stream) ⇒ Object



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

def transform!(input_stream)
  output = []
  input_stream.each do |stream_row|
    new_row = {}
    stream_row.each do |column, value|
      if self.mapping.keys.include?(column)
        new_row[column] = self.mapping[column][value] || value
      else
        new_row[column] = value
      end
    end
    output << new_row
  end
  output
end