-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal_reversible.rb
More file actions
74 lines (70 loc) · 2.38 KB
/
Copy pathglobal_reversible.rb
File metadata and controls
74 lines (70 loc) · 2.38 KB
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
68
69
70
71
72
73
74
# frozen_string_literal: true
module Kiba
module Extend
module Transforms
module Replace
# Replaces all
# {Kiba::Extend::ProjectConfig.global_reversible_replacements}
# key matches with the corresponding `:replace` value.
# @note Runs on **all** fields in the job where it is used.
# @note Has no effect if
# {Kiba::Extend::ProjectConfig.global_reversible_replacements}
# is not populated
# @see GlobalReverse
# @example
# # Used in pipeline as:
# # transform Replace::GlobalReversible
# Kiba::Extend::ProjectConfig
# .config.global_reversible_replacements = {
# /(?:\n|\r)/ => {replace: "%CR%", reversed: "\n"},
# /\t/ => {replace: "%TAB%", reversed: " "},
# / +/ => {replace: " ", reversed: " "}
# }
# xform = Replace::GlobalReversible.new
# input = [{
# ant: "a\nb",
# bat: nil,
# cow: "",
# dog: "c\rd",
# eel: "e\tf",
# fawn: "g o"
# }]
# result = Kiba::StreamingRunner.transform_stream(input, xform)
# .map{ |row| row }
# expected = [{
# ant: "a%CR%b",
# bat: nil,
# cow: nil,
# dog: "c%CR%d",
# eel: "e%TAB%f",
# fawn: "g o"
# }]
# expect(result).to eq(expected)
# Kiba::Extend::ProjectConfig.reset_config
class GlobalReversible
# @param omit_from_all_fields [Array<Symbol>] fields to omit from
# inclusion in "all" fields
def initialize(omit_from_all_fields: [])
@omit_from_all_fields = omit_from_all_fields
@replacers =
Kiba::Extend::ProjectConfig.global_reversible_replacements
.map do |pattern, config|
Clean::RegexpFindReplaceFieldVals.new(
fields: :all,
omit_from_all_fields: omit_from_all_fields,
find: pattern,
replace: config[:replace]
)
end
end
def process(row)
replacers.each { |replacer| replacer.process(row) }
row
end
private
attr_reader :replacers
end
end
end
end
end