Created
January 16, 2018 09:26
-
-
Save umar-siddiqui/52d5c1d1d9660decae0458e165275433 to your computer and use it in GitHub Desktop.
Transpose transformer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module TransformationEngine | |
## | |
# This class is for transpose the rows | |
class TransposeTransformer < BaseTransformer | |
attr_accessor :fixed_cols, :varying_cols | |
def initialize(rule_hash) | |
super(rule_hash) | |
self.type = 'collection' | |
self.fixed_cols = rule_hash[:fixed_cols].split(',').map(&:to_sym) | |
self.varying_cols = rule_hash[:varying_cols] | |
end | |
def process(rows) | |
rows.map { |row| process_row(row) }.flatten | |
end | |
def process_row(row) | |
fixed_row = row.select { |key, value| fixed_cols.include?(key) } | |
varying_cols.map do |cols| | |
cols = cols.split(',').map(&:to_sym) | |
result_row = fixed_row.clone | |
cols.each do |col| | |
result_row = row[col] | |
end | |
result_row | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment