smarter_csv 1.14.1 → 1.14.2

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: c4619533008fc05b02a009b1409d1368d56245584baf52134511b45ff505f011
4
- data.tar.gz: 051fa13106a0dfe41486accfee972a07c7d80670a65dd1066f07dfc600489be3
3
+ metadata.gz: 4b6c0ad6a61721dac33f4ef31cf34da3cdd221804aaa45ff8e49cf7b5894b539
4
+ data.tar.gz: 8c1307aa7a74fc4f434eec362519dcb4392774aa35f563fc2674d542f6b4ea62
5
5
  SHA512:
6
- metadata.gz: 5ee274a0a485e87f356940eeaf5e68d6af3d83dd72cd5199eca9729f6c760d0528ac2db8931c2d741521edb69251a4ff3d0c4d458f449a824f2b8110e7ad3a51
7
- data.tar.gz: 87ac821280845041514a0418b94389296e6b03faf5c5fded8e2b0a0840015c03c477ad57d6a8268316b110adffb295882c81b7876c6f9f76a444531faed3c431
6
+ metadata.gz: 8b7cef2ec65c990d3f6c8b05acaefa328952e70571c5868293531ac51c44fd13306dfad5e976f786eb61d206214bc7eae91cf8c2e00023a26c75d689769ed684
7
+ data.tar.gz: e204946071d76d264b0c8206b1d9170eb7b5c89bb25f22975322fbdc0c4d52869f4b7cf06245d5143298c6d3a89b8c2bc6aa6529fc32f0895e57e1d2ddef97d1
data/CHANGELOG.md CHANGED
@@ -1,9 +1,13 @@
1
1
 
2
2
  # SmarterCSV 1.x Change Log
3
3
 
4
+ ## 1.14.2 (2025-04-10)
5
+ * bugfix: SmarterCSV::Writer fixing corner case with `quote_headers: true`
6
+ * new option: `header_converter` allows to programatically modify the headers
7
+
4
8
  ## 1.14.1 (2025-04-09)
5
- * bugfix: empty hash results in a blank line ([issue 299](https://github.com/tilo/smarter_csv/issues/299))
6
- * bugfix: automatically quote problematic headers ([issue #300](https://github.com/tilo/smarter_csv/issues/300))
9
+ * bugfix: SmarterCSV::Writer empty hash results in a blank line ([issue 299](https://github.com/tilo/smarter_csv/issues/299))
10
+ * bugfix: SmarterCSV::Writer need to automatically quote problematic headers ([issue #300](https://github.com/tilo/smarter_csv/issues/300))
7
11
  * new option: `quote_headers` allows to explicitly quote all headers
8
12
 
9
13
  ## 1.14.0 (2025-04-07)
data/README.md CHANGED
@@ -47,6 +47,7 @@ Or install it yourself as:
47
47
 
48
48
  # Articles
49
49
  * [Parsing CSV Files in Ruby with SmarterCSV](https://tilo-sloboda.medium.com/parsing-csv-files-in-ruby-with-smartercsv-6ce66fb6cf38)
50
+ * [CSV Writing with SmarterCSV](https://tilo-sloboda.medium.com/csv-writing-with-smartercsv-26136d47ad0c)
50
51
  * [Processing 1.4 Million CSV Records in Ruby, fast ](https://lcx.wien/blog/processing-14-million-csv-records-in-ruby/)
51
52
  * [Faster Parsing CSV with Parallel Processing](http://xjlin0.github.io/tech/2015/05/25/faster-parsing-csv-with-parallel-processing) by [Jack lin](https://github.com/xjlin0/)
52
53
  * The original [Stackoverflow Question](https://stackoverflow.com/questions/7788618/update-mongodb-with-array-from-csv-join-table/7788746#7788746) that inspired SmarterCSV
data/docs/options.md CHANGED
@@ -28,10 +28,15 @@
28
28
  | | | ⚠️ This disables automatic header detection! |
29
29
  | :map_headers | {} | Similar to `headers`, but also maps each desired key to a user-specified value that is uesd as the header. |
30
30
  | | | ⚠️ This disables automatic header detection! |
31
+ | :value_converters | nil | allows to define lambdas to programmatically modify values |
32
+ | | | * either for specific `key` names |
33
+ | | | * or using `_all` for all fields |
34
+ | :header_converter | nil | allows to define one lambda to programmatically modify the headers |
31
35
  | :discover_headers | true | Automatically detects all keys in the input before writing the header |
32
36
  | | | Do not manually set this to `false`. ⚠️ |
33
37
  | | | But you can set this to `true` when using `map_headers` option. |
34
38
  | :disable_auto_quoting | false | To manually disable auto-quoting of special characters. ⚠️ Be careful with this! |
39
+ | :quote_headers | false | To force quoting all headers (only needed in rare cases) |
35
40
 
36
41
 
37
42
  ## CSV Reading
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmarterCSV
4
- VERSION = "1.14.1"
4
+ VERSION = "1.14.2"
5
5
  end
@@ -54,6 +54,7 @@ module SmarterCSV
54
54
  @value_converters = options[:value_converters] || {}
55
55
  @map_all_keys = @value_converters.has_key?(:_all)
56
56
  @mapped_keys = @value_converters.keys - [:_all]
57
+ @header_converter = options[:header_converter]
57
58
 
58
59
  @discover_headers = true
59
60
  if options.has_key?(:discover_headers)
@@ -89,6 +90,9 @@ module SmarterCSV
89
90
 
90
91
  def finalize
91
92
  mapped_headers = @headers.map { |header| @map_headers[header] || header }
93
+
94
+ mapped_headers = @headers.map { |header| @header_converter.call(header) } if @header_converter
95
+
92
96
  force_quotes = @quote_headers || @force_quotes
93
97
  mapped_headers = mapped_headers.map { |x| escape_csv_field(x, force_quotes) }
94
98
 
@@ -136,7 +140,7 @@ module SmarterCSV
136
140
 
137
141
  def escape_csv_field(field, force_quotes = false)
138
142
  str = field.to_s
139
- return str if @disable_auto_quoting
143
+ return str if @disable_auto_quoting && !force_quotes
140
144
 
141
145
  # double-quote fields if we force that, or if the field contains the comma, new-line, or quote character
142
146
  contains_special_char = str.to_s.match(@quote_regex)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smarter_csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.14.1
4
+ version: 1.14.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tilo Sloboda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-09 00:00:00.000000000 Z
11
+ date: 2025-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print