iostreams 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/io_streams/tabular/header.rb +12 -12
- data/lib/io_streams/version.rb +1 -1
- data/test/tabular_test.rb +7 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b84445b48ac6697a255b5fdacd16783b8573e2cdb75ad395c3b4167ffdd6e01b
|
4
|
+
data.tar.gz: a37969afc9635fdac60ea45717d64b9bdd8fbb7c6d853a04c0a02cd798274606
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 685d6a23dfc176f3abe922fab1879c76572b73dc878864af3e38dfa5824e0eb8c115f855537dcf3c3f2d181b3381512a2c9a20c065beb62cfd91642913c84b30
|
7
|
+
data.tar.gz: db6e6fcb72c07fe502e64f9ab4d18c79334cde6c1622d1b2ac2a22361ed1f38967adc19bb83959e56fc2039ee595f078c1a92d6277a5b8d39d3802e020df0a1a
|
@@ -2,6 +2,9 @@ module IOStreams
|
|
2
2
|
class Tabular
|
3
3
|
# Process files / streams that start with a header.
|
4
4
|
class Header
|
5
|
+
# Column names that begin with this prefix have been rejected and should be ignored.
|
6
|
+
IGNORE_PREFIX = "__rejected__".freeze
|
7
|
+
|
5
8
|
attr_accessor :columns, :allowed_columns, :required_columns, :skip_unknown
|
6
9
|
|
7
10
|
# Header
|
@@ -17,8 +20,8 @@ module IOStreams
|
|
17
20
|
# List of columns to allow.
|
18
21
|
# Default: nil ( Allow all columns )
|
19
22
|
# Note:
|
20
|
-
#
|
21
|
-
#
|
23
|
+
# * So that rejected columns can be identified in subsequent steps, they will be prefixed with `__rejected__`.
|
24
|
+
# For example, `Unknown Column` would be cleansed as `__rejected__Unknown Column`.
|
22
25
|
#
|
23
26
|
# required_columns [Array<String>]
|
24
27
|
# List of columns that must be present, otherwise an Exception is raised.
|
@@ -44,8 +47,10 @@ module IOStreams
|
|
44
47
|
# - Spaces and '-' are converted to '_'.
|
45
48
|
# - All characters except for letters, digits, and '_' are stripped.
|
46
49
|
#
|
47
|
-
# Notes
|
48
|
-
# *
|
50
|
+
# Notes:
|
51
|
+
# * So that rejected columns can be identified in subsequent steps, they will be prefixed with `__rejected__`.
|
52
|
+
# For example, `Unknown Column` would be cleansed as `__rejected__Unknown Column`.
|
53
|
+
# * Raises Tabular::InvalidHeader when there are no rejected columns left after cleansing.
|
49
54
|
def cleanse!
|
50
55
|
return [] if columns.nil? || columns.empty?
|
51
56
|
|
@@ -56,7 +61,7 @@ module IOStreams
|
|
56
61
|
cleansed
|
57
62
|
else
|
58
63
|
ignored_columns << column
|
59
|
-
|
64
|
+
"#{IGNORE_PREFIX}#{column}"
|
60
65
|
end
|
61
66
|
end
|
62
67
|
|
@@ -122,7 +127,7 @@ module IOStreams
|
|
122
127
|
|
123
128
|
def array_to_hash(row)
|
124
129
|
h = {}
|
125
|
-
columns.each_with_index { |col, i| h[col] = row[i] unless IOStreams::Utils.blank?(col) }
|
130
|
+
columns.each_with_index { |col, i| h[col] = row[i] unless IOStreams::Utils.blank?(col) || col.start_with?(IGNORE_PREFIX) }
|
126
131
|
h
|
127
132
|
end
|
128
133
|
|
@@ -134,12 +139,7 @@ module IOStreams
|
|
134
139
|
hash = hash.dup
|
135
140
|
unmatched.each { |name| hash[cleanse_column(name)] = hash.delete(name) }
|
136
141
|
end
|
137
|
-
|
138
|
-
if hash.respond_to?(:slice)
|
139
|
-
hash.slice(*columns)
|
140
|
-
else
|
141
|
-
columns.each_with_object({}) { |column, new_hash| new_hash[column] = hash[column] }
|
142
|
-
end
|
142
|
+
hash.slice(*columns)
|
143
143
|
end
|
144
144
|
|
145
145
|
def cleanse_column(name)
|
data/lib/io_streams/version.rb
CHANGED
data/test/tabular_test.rb
CHANGED
@@ -58,12 +58,12 @@ class TabularTest < Minitest::Test
|
|
58
58
|
assert_equal header, tabular.header.columns
|
59
59
|
end
|
60
60
|
|
61
|
-
it "
|
61
|
+
it "allowed list snake cased alphanumeric columns" do
|
62
62
|
tabular = IOStreams::Tabular.new(
|
63
|
-
columns: ["Ard Vark", "
|
63
|
+
columns: ["Ard Vark", "Password", "robot version", "$$$"],
|
64
64
|
allowed_columns: %w[ard_vark robot_version]
|
65
65
|
)
|
66
|
-
expected_header = ["ard_vark",
|
66
|
+
expected_header = ["ard_vark", "__rejected__Password", "robot_version", "__rejected__$$$"]
|
67
67
|
cleansed_header = tabular.cleanse_header!
|
68
68
|
assert_equal(expected_header, cleansed_header)
|
69
69
|
end
|
@@ -82,13 +82,13 @@ class TabularTest < Minitest::Test
|
|
82
82
|
assert_equal @allowed_columns, tabular.header.allowed_columns
|
83
83
|
end
|
84
84
|
|
85
|
-
it "nils columns not in the
|
85
|
+
it "nils columns not in the allowed list" do
|
86
86
|
tabular = IOStreams::Tabular.new(columns: [" first ", "Unknown Column", "thirD "], allowed_columns: @allowed_columns)
|
87
87
|
header = tabular.cleanse_header!
|
88
|
-
assert_equal ["first",
|
88
|
+
assert_equal ["first", "__rejected__Unknown Column", "third"], header
|
89
89
|
end
|
90
90
|
|
91
|
-
it "raises exception for columns not in the
|
91
|
+
it "raises exception for columns not in the allowed list" do
|
92
92
|
tabular = IOStreams::Tabular.new(columns: [" first ", "Unknown Column", "thirD "], allowed_columns: @allowed_columns, skip_unknown: false)
|
93
93
|
exc = assert_raises IOStreams::Errors::InvalidHeader do
|
94
94
|
tabular.cleanse_header!
|
@@ -218,7 +218,7 @@ class TabularTest < Minitest::Test
|
|
218
218
|
end
|
219
219
|
end
|
220
220
|
|
221
|
-
it "skips columns not in the
|
221
|
+
it "skips columns not in the allowed list" do
|
222
222
|
tabular.header.allowed_columns = %w[first second third fourth fifth]
|
223
223
|
tabular.cleanse_header!
|
224
224
|
assert hash = tabular.record_parse("1,2,3")
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: iostreams
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Reid Morrison
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
132
132
|
- !ruby/object:Gem::Version
|
133
133
|
version: '0'
|
134
134
|
requirements: []
|
135
|
-
rubygems_version: 3.2.
|
135
|
+
rubygems_version: 3.2.22
|
136
136
|
signing_key:
|
137
137
|
specification_version: 4
|
138
138
|
summary: Input and Output streaming for Ruby.
|