-
Notifications
You must be signed in to change notification settings - Fork 0
/
fields.rb
67 lines (62 loc) · 2.08 KB
/
fields.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# frozen_string_literal: true
module Kiba
module Extend
module Transforms
module Rename
# rubocop:disable Layout/LineLength
# Renames multiple fields at once, given a fieldmap where key
# is `from` field and value is `to` field
#
# This transform works by creating a {Rename::Field} transform for each
# key/value pair specified in `fieldmap`, so see {Rename::Field} for
# examples demonstrating edge case behavior.
#
#
# @example
# # Used in pipeline as:
# # transform Rename::Fields, fieldmap: {
# # name: :appellation,
# # sex: :gender,
# # color: :color
# # }
# xform = Rename::Fields.new(fieldmap: {
# name: :appellation,
# sex: :gender,
# color: :color
# })
# 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 = [
# {appellation: "Weddy", gender: "m", color: "pearl gray, greater, pied"},
# {appellation: "Kernel", gender: "f", color: "buff dundotte"},
# ]
# expect(result).to eq(expected)
#
#
# @since 2.8.0
class Fields
# rubocop:enable Layout/LineLength
# @param fieldmap [Hash(Symbol => Symbol)] Keys are the `from` fields;
# values are the `to` fields
def initialize(fieldmap:)
@fieldmap = fieldmap
@renamers = fieldmap.map do |from, to|
Rename::Field.new(from: from, to: to)
end
end
# @param row [Hash{ Symbol => String, nil }]
def process(row)
renamers.each { |renamer| renamer.process(row) }
row
end
private
attr_reader :fieldmap, :renamers
end
end
end
end
end