iostreams 1.7.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 849ceda63eb30f95762a7c985cd215d424e62afd68ab20776e8d16c188dd6aed
4
- data.tar.gz: 8e26af86c40bb673ce36855a7fb30d1c4b401edc3eac0b27a71b9760cfe865dd
3
+ metadata.gz: b84445b48ac6697a255b5fdacd16783b8573e2cdb75ad395c3b4167ffdd6e01b
4
+ data.tar.gz: a37969afc9635fdac60ea45717d64b9bdd8fbb7c6d853a04c0a02cd798274606
5
5
  SHA512:
6
- metadata.gz: 99318c4c64e0133df57b84429b1c2f9caa064abb1405ace5d55208e41b6bf8bb8fa83a75db8ae46d53753f10d566bab53971d95871ed4011bab4571d31bebe8a
7
- data.tar.gz: bfba3a033c753e3fe05f798177b8f3c7ee8f566eabaf9223984fa902b288cb3515154f621a4e97f75b6c5bc31da88f284390c2d82c5015d7682ecf08c2a671d3
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
- # When supplied any columns that are rejected will be returned in the cleansed columns
21
- # as nil so that they can be ignored during processing.
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
- # * Raises Tabular::InvalidHeader when there are no non-nil columns left after cleansing.
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
- nil
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
- # Hash#slice as of Ruby 2.5
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)
@@ -1,3 +1,3 @@
1
1
  module IOStreams
2
- VERSION = "1.7.0".freeze
2
+ VERSION = "1.8.0".freeze
3
3
  end
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 "white listed snake cased alphanumeric columns" do
61
+ it "allowed list snake cased alphanumeric columns" do
62
62
  tabular = IOStreams::Tabular.new(
63
- columns: ["Ard Vark", "password", "robot version", "$$$"],
63
+ columns: ["Ard Vark", "Password", "robot version", "$$$"],
64
64
  allowed_columns: %w[ard_vark robot_version]
65
65
  )
66
- expected_header = ["ard_vark", nil, "robot_version", nil]
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 whitelist" do
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", nil, "third"], header
88
+ assert_equal ["first", "__rejected__Unknown Column", "third"], header
89
89
  end
90
90
 
91
- it "raises exception for columns not in the whitelist" do
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 whitelist" do
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.7.0
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-06-23 00:00:00.000000000 Z
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.15
135
+ rubygems_version: 3.2.22
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Input and Output streaming for Ruby.