csv 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/lib/core_ext/array.rb +9 -0
- data/lib/core_ext/string.rb +9 -0
- data/lib/csv.rb +79 -46
- data/news.md +49 -0
- metadata +21 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 05b9101e168faf6acec7dd2c841e6a30084e3585
|
4
|
+
data.tar.gz: ad75132c6ed63c2cf95c16b6dbf948d76d83d69f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45c5498cf70618cd26ddf0acd17db083beec0b9a532adf7279cc5cb830f400a1ae0b291e6492f4191020b97a427c46c019aa97fbfb83aaded9fc66b920015c26
|
7
|
+
data.tar.gz: 5dd1c842d29df9e2c89da50b2ebbbed42e6386d05f3e5f76403b5d977e13a485b9978c815d418246798e4a4e72bf62995476ca4e9ea40095cc80f73319b30d7b
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# CSV
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/ruby/csv.svg?branch=master)](https://travis-ci.org/ruby/csv)
|
4
|
+
|
5
|
+
This library provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'csv'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install csv
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require "csv"
|
27
|
+
|
28
|
+
CSV.foreach("path/to/file.csv") do |row|
|
29
|
+
# use row here...
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
## Development
|
34
|
+
|
35
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
36
|
+
|
37
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/csv.
|
42
|
+
|
43
|
+
|
44
|
+
## License
|
45
|
+
|
46
|
+
The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause).
|
data/lib/csv.rb
CHANGED
@@ -208,7 +208,7 @@ require "stringio"
|
|
208
208
|
#
|
209
209
|
class CSV
|
210
210
|
# The version of the installed library.
|
211
|
-
VERSION = "
|
211
|
+
VERSION = "1.0.1"
|
212
212
|
|
213
213
|
#
|
214
214
|
# A CSV::Row is part Array and part Hash. It retains an order for the fields
|
@@ -434,7 +434,7 @@ class CSV
|
|
434
434
|
# If no block is given, an Enumerator is returned.
|
435
435
|
#
|
436
436
|
def delete_if(&block)
|
437
|
-
|
437
|
+
return enum_for(__method__) { size } unless block_given?
|
438
438
|
|
439
439
|
@row.delete_if(&block)
|
440
440
|
|
@@ -513,7 +513,7 @@ class CSV
|
|
513
513
|
# Support for Enumerable.
|
514
514
|
#
|
515
515
|
def each(&block)
|
516
|
-
|
516
|
+
return enum_for(__method__) { size } unless block_given?
|
517
517
|
|
518
518
|
@row.each(&block)
|
519
519
|
|
@@ -547,6 +547,24 @@ class CSV
|
|
547
547
|
end
|
548
548
|
alias_method :to_s, :to_csv
|
549
549
|
|
550
|
+
#
|
551
|
+
# Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
|
552
|
+
# returning nil if any intermediate step is nil.
|
553
|
+
#
|
554
|
+
def dig(index_or_header, *indexes)
|
555
|
+
value = field(index_or_header)
|
556
|
+
if value.nil?
|
557
|
+
nil
|
558
|
+
elsif indexes.empty?
|
559
|
+
value
|
560
|
+
else
|
561
|
+
unless value.respond_to?(:dig)
|
562
|
+
raise TypeError, "#{value.class} does not have \#dig method"
|
563
|
+
end
|
564
|
+
value.dig(*indexes)
|
565
|
+
end
|
566
|
+
end
|
567
|
+
|
550
568
|
# A summary of fields, by header, in an ASCII compatible String.
|
551
569
|
def inspect
|
552
570
|
str = ["#<", self.class.to_s]
|
@@ -808,16 +826,26 @@ class CSV
|
|
808
826
|
end
|
809
827
|
|
810
828
|
#
|
811
|
-
# Removes and returns the indicated
|
829
|
+
# Removes and returns the indicated columns or rows. In the default mixed
|
812
830
|
# mode indices refer to rows and everything else is assumed to be a column
|
813
|
-
#
|
831
|
+
# headers. Use by_col!() or by_row!() to force the lookup.
|
814
832
|
#
|
815
|
-
def delete(
|
816
|
-
if
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
@
|
833
|
+
def delete(*indexes_or_headers)
|
834
|
+
if indexes_or_headers.empty?
|
835
|
+
raise ArgumentError, "wrong number of arguments (given 0, expected 1+)"
|
836
|
+
end
|
837
|
+
deleted_values = indexes_or_headers.map do |index_or_header|
|
838
|
+
if @mode == :row or # by index
|
839
|
+
(@mode == :col_or_row and index_or_header.is_a? Integer)
|
840
|
+
@table.delete_at(index_or_header)
|
841
|
+
else # by header
|
842
|
+
@table.map { |row| row.delete(index_or_header).last }
|
843
|
+
end
|
844
|
+
end
|
845
|
+
if indexes_or_headers.size == 1
|
846
|
+
deleted_values[0]
|
847
|
+
else
|
848
|
+
deleted_values
|
821
849
|
end
|
822
850
|
end
|
823
851
|
|
@@ -832,14 +860,14 @@ class CSV
|
|
832
860
|
# If no block is given, an Enumerator is returned.
|
833
861
|
#
|
834
862
|
def delete_if(&block)
|
835
|
-
|
863
|
+
return enum_for(__method__) { @mode == :row or @mode == :col_or_row ? size : headers.size } unless block_given?
|
836
864
|
|
837
865
|
if @mode == :row or @mode == :col_or_row # by index
|
838
866
|
@table.delete_if(&block)
|
839
867
|
else # by header
|
840
868
|
deleted = []
|
841
869
|
headers.each do |header|
|
842
|
-
deleted << delete(header) if
|
870
|
+
deleted << delete(header) if yield([header, self[header]])
|
843
871
|
end
|
844
872
|
end
|
845
873
|
|
@@ -858,10 +886,10 @@ class CSV
|
|
858
886
|
# If no block is given, an Enumerator is returned.
|
859
887
|
#
|
860
888
|
def each(&block)
|
861
|
-
|
889
|
+
return enum_for(__method__) { @mode == :col ? headers.size : size } unless block_given?
|
862
890
|
|
863
891
|
if @mode == :col
|
864
|
-
headers.each { |header|
|
892
|
+
headers.each { |header| yield([header, self[header]]) }
|
865
893
|
else
|
866
894
|
@table.each(&block)
|
867
895
|
end
|
@@ -903,6 +931,24 @@ class CSV
|
|
903
931
|
end
|
904
932
|
alias_method :to_s, :to_csv
|
905
933
|
|
934
|
+
#
|
935
|
+
# Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step,
|
936
|
+
# returning nil if any intermediate step is nil.
|
937
|
+
#
|
938
|
+
def dig(index_or_header, *index_or_headers)
|
939
|
+
value = self[index_or_header]
|
940
|
+
if value.nil?
|
941
|
+
nil
|
942
|
+
elsif index_or_headers.empty?
|
943
|
+
value
|
944
|
+
else
|
945
|
+
unless value.respond_to?(:dig)
|
946
|
+
raise TypeError, "#{value.class} does not have \#dig method"
|
947
|
+
end
|
948
|
+
value.dig(*index_or_headers)
|
949
|
+
end
|
950
|
+
end
|
951
|
+
|
906
952
|
# Shows the mode and size of this table in a US-ASCII String.
|
907
953
|
def inspect
|
908
954
|
"#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII")
|
@@ -930,7 +976,11 @@ class CSV
|
|
930
976
|
# A Regexp used to find and convert some common DateTime formats.
|
931
977
|
DateTimeMatcher =
|
932
978
|
/ \A(?: (\w+,?\s+)?\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2},?\s+\d{2,4} |
|
933
|
-
\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}
|
979
|
+
\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} |
|
980
|
+
# ISO-8601
|
981
|
+
\d{4}-\d{2}-\d{2}
|
982
|
+
(?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?(?:[+-]\d{2}(?::\d{2})|Z)?)?)?
|
983
|
+
)\z /x
|
934
984
|
|
935
985
|
# The encoding used by all converters.
|
936
986
|
ConverterEncoding = Encoding.find("UTF-8")
|
@@ -1137,7 +1187,7 @@ class CSV
|
|
1137
1187
|
# but transcode it to UTF-8 before CSV parses it.
|
1138
1188
|
#
|
1139
1189
|
def self.foreach(path, **options, &block)
|
1140
|
-
return to_enum(__method__, path, options) unless
|
1190
|
+
return to_enum(__method__, path, options) unless block_given?
|
1141
1191
|
open(path, options) do |csv|
|
1142
1192
|
csv.each(&block)
|
1143
1193
|
end
|
@@ -1164,8 +1214,8 @@ class CSV
|
|
1164
1214
|
def self.generate(str=nil, **options)
|
1165
1215
|
# add a default empty String, if none was given
|
1166
1216
|
if str
|
1167
|
-
|
1168
|
-
|
1217
|
+
str = StringIO.new(str)
|
1218
|
+
str.seek(0, IO::SEEK_END)
|
1169
1219
|
else
|
1170
1220
|
encoding = options[:encoding]
|
1171
1221
|
str = String.new
|
@@ -1309,14 +1359,14 @@ class CSV
|
|
1309
1359
|
#
|
1310
1360
|
def self.parse(*args, &block)
|
1311
1361
|
csv = new(*args)
|
1312
|
-
|
1313
|
-
|
1314
|
-
|
1315
|
-
|
1316
|
-
|
1317
|
-
|
1318
|
-
|
1319
|
-
csv.
|
1362
|
+
|
1363
|
+
return csv.each(&block) if block_given?
|
1364
|
+
|
1365
|
+
# slurp contents, if no block is given
|
1366
|
+
begin
|
1367
|
+
csv.read
|
1368
|
+
ensure
|
1369
|
+
csv.close
|
1320
1370
|
end
|
1321
1371
|
end
|
1322
1372
|
|
@@ -2334,22 +2384,5 @@ def CSV(*args, &block)
|
|
2334
2384
|
CSV.instance(*args, &block)
|
2335
2385
|
end
|
2336
2386
|
|
2337
|
-
|
2338
|
-
|
2339
|
-
#
|
2340
|
-
# ["CSV", "data"].to_csv
|
2341
|
-
# #=> "CSV,data\n"
|
2342
|
-
def to_csv(**options)
|
2343
|
-
CSV.generate_line(self, options)
|
2344
|
-
end
|
2345
|
-
end
|
2346
|
-
|
2347
|
-
class String # :nodoc:
|
2348
|
-
# Equivalent to CSV::parse_line(self, options)
|
2349
|
-
#
|
2350
|
-
# "CSV,data".parse_csv
|
2351
|
-
# #=> ["CSV", "data"]
|
2352
|
-
def parse_csv(**options)
|
2353
|
-
CSV.parse_line(self, options)
|
2354
|
-
end
|
2355
|
-
end
|
2387
|
+
require_relative "core_ext/array"
|
2388
|
+
require_relative "core_ext/string"
|
data/news.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# News
|
2
|
+
|
3
|
+
## 1.0.1 - 2018-02-09
|
4
|
+
|
5
|
+
### Improvements
|
6
|
+
|
7
|
+
* `CSV::Table#delete`: Added bulk delete support. You can delete
|
8
|
+
multiple rows and columns at once.
|
9
|
+
[GitHub#4][Patch by Vladislav]
|
10
|
+
|
11
|
+
* Updated Gem description.
|
12
|
+
[GitHub#11][Patch by Marcus Stollsteimer]
|
13
|
+
|
14
|
+
* Code cleanup.
|
15
|
+
[GitHub#12][Patch by Marcus Stollsteimer]
|
16
|
+
[GitHub#14][Patch by Steven Daniels]
|
17
|
+
[GitHub#18][Patch by takkanm]
|
18
|
+
|
19
|
+
* `CSV::Table#dig`: Added.
|
20
|
+
[GitHub#15][Patch by Tomohiro Ogoke]
|
21
|
+
|
22
|
+
* `CSV::Row#dig`: Added.
|
23
|
+
[GitHub#15][Patch by Tomohiro Ogoke]
|
24
|
+
|
25
|
+
* Added ISO 8601 support to date time converter.
|
26
|
+
[GitHub#16]
|
27
|
+
|
28
|
+
### Fixes
|
29
|
+
|
30
|
+
* Fixed wrong `CSV::VERSION`.
|
31
|
+
[GitHub#10][Reported by Marcus Stollsteimer]
|
32
|
+
|
33
|
+
* `CSV.generate`: Fixed a regression bug that `String` argument is
|
34
|
+
ignored.
|
35
|
+
[GitHub#13][Patch by pavel]
|
36
|
+
|
37
|
+
### Thanks
|
38
|
+
|
39
|
+
* Vladislav
|
40
|
+
|
41
|
+
* Marcus Stollsteimer
|
42
|
+
|
43
|
+
* Steven Daniels
|
44
|
+
|
45
|
+
* takkanm
|
46
|
+
|
47
|
+
* Tomohiro Ogoke
|
48
|
+
|
49
|
+
* pavel
|
metadata
CHANGED
@@ -1,51 +1,60 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Edward Gray II
|
8
|
+
- Kouhei Sutou
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2018-02-08 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: bundler
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
|
-
- - "
|
18
|
+
- - ">="
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
20
|
+
version: '0'
|
20
21
|
type: :development
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
|
-
- - "
|
25
|
+
- - ">="
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
+
version: '0'
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
29
|
name: rake
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
|
-
- - "
|
32
|
+
- - ">="
|
32
33
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
34
|
+
version: '0'
|
34
35
|
type: :development
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
|
-
- - "
|
39
|
+
- - ">="
|
39
40
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
41
|
-
description:
|
41
|
+
version: '0'
|
42
|
+
description: The CSV library provides a complete interface to CSV files and data.
|
43
|
+
It offers tools to enable you to read and write to and from Strings or IO objects,
|
44
|
+
as needed.
|
42
45
|
email:
|
43
46
|
-
|
47
|
+
- kou@cozmixng.org
|
44
48
|
executables: []
|
45
49
|
extensions: []
|
46
50
|
extra_rdoc_files: []
|
47
51
|
files:
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- lib/core_ext/array.rb
|
55
|
+
- lib/core_ext/string.rb
|
48
56
|
- lib/csv.rb
|
57
|
+
- news.md
|
49
58
|
homepage: https://github.com/ruby/csv
|
50
59
|
licenses:
|
51
60
|
- BSD-2-Clause
|
@@ -66,7 +75,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
66
75
|
version: '0'
|
67
76
|
requirements: []
|
68
77
|
rubyforge_project:
|
69
|
-
rubygems_version: 2.
|
78
|
+
rubygems_version: 2.5.2.2
|
70
79
|
signing_key:
|
71
80
|
specification_version: 4
|
72
81
|
summary: CSV Reading and Writing
|