sane_csv 0.1.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 +7 -0
- data/.rspec +3 -0
- data/README.md +71 -0
- data/Rakefile +8 -0
- data/lib/sane_csv/version.rb +5 -0
- data/lib/sane_csv.rb +16 -0
- data/sig/sane_csv.rbs +4 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 901d7ee21ff88beaf531f04d88cf1ba3d65dcda99146da3b2f413d80827c0c7e
|
4
|
+
data.tar.gz: 0e751d5445fd584b58a28261c5bfc9b0370131f3d675a0864b26dcd3ed7a86ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 75abe38fa86e8d9832fe14e19cfa32ae4d12f4e0260735f0dba2bb43a8f0c211b7c9086df72a050b1adeaa79cb1664e05b54f4b243916483074566c961f5a914
|
7
|
+
data.tar.gz: 76cf8f7fd30c5551d6f229fe9166439af75efb8294a9416766b8f1e155f08d2415abd07b2c56658e45c1dd69ebfa639f97396cb06eb507a7c64ebf518784a237
|
data/.rspec
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# SaneCsv
|
2
|
+
|
3
|
+
This ensures that the `CSV` never returns `nil` when parsing.
|
4
|
+
Additionally, empty strings will not be quoted when generating CSV.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
$ bundle add sane_csv
|
11
|
+
|
12
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
13
|
+
|
14
|
+
$ gem install sane_csv
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
require 'sane_csv'
|
20
|
+
```
|
21
|
+
|
22
|
+
`CSV` may return `nil`, but using `SaneCsv` ensures it never returns `nil`.
|
23
|
+
|
24
|
+
The default behavior of `CSV`:
|
25
|
+
```ruby
|
26
|
+
require 'csv'
|
27
|
+
|
28
|
+
CSV.parse('a,,b')[0]
|
29
|
+
#=> ["a", nil, "b"]
|
30
|
+
```
|
31
|
+
|
32
|
+
To avoid returning `nil`, you need to specify the `nil_value` option:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'csv'
|
36
|
+
|
37
|
+
CSV.parse('a,,b', nil_value: "")[0]
|
38
|
+
#=> ["a", "", "b"]
|
39
|
+
```
|
40
|
+
|
41
|
+
When using `SaneCsv`:
|
42
|
+
```ruby
|
43
|
+
require 'sane_csv'
|
44
|
+
|
45
|
+
CSV.parse('a,,b')[0]
|
46
|
+
#=> ["a", "", "b"]
|
47
|
+
```
|
48
|
+
|
49
|
+
If you have a special situation where you want to return `nil`, you can specify the `nil_value` option:
|
50
|
+
```ruby
|
51
|
+
require 'sane_csv'
|
52
|
+
|
53
|
+
CSV.parse('a,,b', nil_value: nil)[0]
|
54
|
+
#=> ["a", nil, "b"]
|
55
|
+
```
|
56
|
+
|
57
|
+
Additionally, when generating `CSV`, empty strings are not unnecessarily quoted:
|
58
|
+
```ruby
|
59
|
+
require 'sane_csv'
|
60
|
+
|
61
|
+
['a', '', 'b'].to_csv
|
62
|
+
#=> "a,,b\n"
|
63
|
+
```
|
64
|
+
|
65
|
+
If you have a specific reason to quote empty strings, you can specify `quote_empty`:
|
66
|
+
```ruby
|
67
|
+
require 'sane_csv'
|
68
|
+
|
69
|
+
['a', '', 'b'].to_csv(quote_empty: true)
|
70
|
+
#=> "a,\"\",b\n"
|
71
|
+
```
|
data/Rakefile
ADDED
data/lib/sane_csv.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
require_relative "sane_csv/version"
|
5
|
+
|
6
|
+
# This ensures that the CSV never returns nil when parsing.
|
7
|
+
# Additionally, empty strings will not be quoted when generating CSV.
|
8
|
+
module SaneCsv
|
9
|
+
def initialize(data, nil_value: "", quote_empty: false, **options)
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class CSV
|
15
|
+
prepend SaneCsv
|
16
|
+
end
|
data/sig/sane_csv.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sane_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- TOMITA Masahiro
|
8
|
+
bindir: exe
|
9
|
+
cert_chain: []
|
10
|
+
date: 2024-07-14 00:00:00.000000000 Z
|
11
|
+
dependencies:
|
12
|
+
- !ruby/object:Gem::Dependency
|
13
|
+
name: csv
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: 3.1.2
|
19
|
+
type: :runtime
|
20
|
+
prerelease: false
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
requirements:
|
23
|
+
- - ">="
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 3.1.2
|
26
|
+
description: This ensures that the `CSV` never returns `nil` when parsing.Additionally,
|
27
|
+
empty strings will not be quoted when generating CSV.
|
28
|
+
email:
|
29
|
+
- tommy@tmtm.org
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".rspec"
|
35
|
+
- README.md
|
36
|
+
- Rakefile
|
37
|
+
- lib/sane_csv.rb
|
38
|
+
- lib/sane_csv/version.rb
|
39
|
+
- sig/sane_csv.rbs
|
40
|
+
homepage: https://gitlab.com/tmtms/sane_csv
|
41
|
+
licenses:
|
42
|
+
- MIT
|
43
|
+
metadata:
|
44
|
+
homepage_uri: https://gitlab.com/tmtms/sane_csv
|
45
|
+
source_code_uri: https://gitlab.com/tmtms/sane_csv
|
46
|
+
changelog_uri: https://gitlab.com/tmtms/sane_csv/-/releases
|
47
|
+
rubygems_mfa_required: 'true'
|
48
|
+
rdoc_options: []
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 2.7.0
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubygems_version: 3.6.0.dev
|
63
|
+
specification_version: 4
|
64
|
+
summary: This ensures that the `CSV` never returns `nil` when parsing.
|
65
|
+
test_files: []
|