philiprehberger-csv_kit 0.8.0 → 0.9.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: c48c099081a08bc9be83c75fb8879f9cb876ce2cc6e8978a44b8c18d3cd43776
4
- data.tar.gz: fe72a612c3a6e5b0166c5eb24694f5593cbb0baca3eb541081dc84414939019b
3
+ metadata.gz: 85d915adb35a580821d1055d966fe76b802598d6422571ad1e727694dd604a4f
4
+ data.tar.gz: 66cc520a86bae535668b77aa56ae7d154ddc1f5df22cca7b4578d4b12a7958e8
5
5
  SHA512:
6
- metadata.gz: bfff72e557a3deef83f104118ec4dddf00e5855b2d29ea1ac09a023f767a3b557f7415a9b8fd953ddca68a5cc08155b4b37b14a1800b3f2672db059b1a8a02da
7
- data.tar.gz: a2bd950d995457677e70c2398efb4decb742597bc032e5f0f0454ac5a9025d4a41c2d05f4bcf18988f506c46ffdd0b20b354b7620748587c3de7e32da75387d7
6
+ metadata.gz: 0b719464ccea551cb56975fe78985ffa2f49480f2439e9a528f091afa53b5b24dd55219dbab3e65a8f30ec1f326959749ad4162e52d36471b1ee3bfd1272afcd
7
+ data.tar.gz: 7f98968c2c5063109053f2a4a8f4508f6544e5238f783440969f237f593b66f4ac2a2ace4e6b495862621944fe891c44107a247366d3c1cedd59be6208a88df9
data/CHANGELOG.md CHANGED
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.9.0] - 2026-04-19
11
+
12
+ ### Added
13
+ - `Processor#default(key, value)` — fill nil or empty cells at `key` with a default value during transform; chains naturally with `type:` coercion
14
+
10
15
  ## [0.8.0] - 2026-04-17
11
16
 
12
17
  ### Added
@@ -107,7 +112,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
107
112
  - Type coercion and row validation
108
113
  - Quick load and filtering convenience methods
109
114
 
110
- [Unreleased]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.8.0...HEAD
115
+ [Unreleased]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.9.0...HEAD
116
+ [0.9.0]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.8.0...v0.9.0
111
117
  [0.8.0]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.7.0...v0.8.0
112
118
  [0.7.0]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.6.0...v0.7.0
113
119
  [0.6.0]: https://github.com/philiprehberger/rb-csv-kit/compare/v0.5.0...v0.6.0
data/README.md CHANGED
@@ -106,6 +106,17 @@ rows = Philiprehberger::CsvKit.process("data.csv") do |p|
106
106
  end
107
107
  ```
108
108
 
109
+ ### Default Values for Missing Cells
110
+
111
+ Fill nil or empty-string cells with a default value before any `type` coercion runs:
112
+
113
+ ```ruby
114
+ Philiprehberger::CsvKit.process("users.csv") do |p|
115
+ p.default(:country, "US")
116
+ p.type(:age, :integer)
117
+ end
118
+ ```
119
+
109
120
  ### Date/Time Type Coercions
110
121
 
111
122
  ```ruby
@@ -211,6 +222,7 @@ delimiter = Philiprehberger::CsvKit::Detector.detect("data.tsv")
211
222
  | `Processor#headers(*names)` | Override header names |
212
223
  | `Processor#transform(key, &block)` | Register column transform |
213
224
  | `Processor#type(key, type, **opts)` | Register built-in type coercion (:integer, :float, :string, :date, :datetime) |
225
+ | `Processor#default(key, value)` | Fill nil or empty cells at `key` with `value` (runs before `type` coercion) |
214
226
  | `Processor#validate(key, &block)` | Register column validation (skip invalid) |
215
227
  | `Processor#skip(n)` | Skip the first N data rows |
216
228
  | `Processor#limit(n)` | Stop after processing N rows |
@@ -31,6 +31,7 @@ module Philiprehberger
31
31
  @path_or_io = path_or_io
32
32
  @dialect = dialect ? Dialect.new(dialect) : nil
33
33
  @transforms = {}
34
+ @defaults = {}
34
35
  @validations = {}
35
36
  @reject_block = nil
36
37
  @each_block = nil
@@ -63,6 +64,22 @@ module Philiprehberger
63
64
  @transforms[key] = ->(v) { coercion.call(v, opts) }
64
65
  end
65
66
 
67
+ # Register a default value for a column.
68
+ #
69
+ # Cells where the value is `nil` or an empty string are replaced with
70
+ # the provided default during transform. Defaults run BEFORE `type`
71
+ # coercions and `transform` blocks, so callers can default a missing
72
+ # cell to a string and then coerce it (e.g. default to "0" then cast
73
+ # to :integer).
74
+ #
75
+ # @param key [Symbol] column name
76
+ # @param value [Object] value to use when the cell is nil or empty
77
+ # @return [self]
78
+ def default(key, value)
79
+ @defaults[key] = value
80
+ self
81
+ end
82
+
66
83
  # Register a validation for a specific column.
67
84
  def validate(key, &block)
68
85
  @validations[key] = block
@@ -122,6 +139,7 @@ module Philiprehberger
122
139
  return unless valid?(row)
123
140
  return if rejected?(row)
124
141
 
142
+ apply_defaults!(row)
125
143
  apply_transforms!(row)
126
144
  apply_renames!(row)
127
145
  @each_block&.call(row)
@@ -165,6 +183,13 @@ module Philiprehberger
165
183
  @reject_block&.call(row) || false
166
184
  end
167
185
 
186
+ def apply_defaults!(row)
187
+ @defaults.each do |key, value|
188
+ current = row[key]
189
+ row[key] = value if current.nil? || current.to_s.empty?
190
+ end
191
+ end
192
+
168
193
  def apply_transforms!(row)
169
194
  @transforms.each { |key, blk| row[key] = blk.call(row[key]) }
170
195
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Philiprehberger
4
4
  module CsvKit
5
- VERSION = '0.8.0'
5
+ VERSION = '0.9.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: philiprehberger-csv_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philip Rehberger
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-04-17 00:00:00.000000000 Z
11
+ date: 2026-04-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Streaming CSV processor with row-by-row transforms, validations, column
14
14
  plucking, streaming each_hash iteration, filtering, writing, error recovery, and