Skip to content

Commit fcf3e39

Browse files
committed
add csvyaml
1 parent 8233751 commit fcf3e39

14 files changed

+626
-2
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Comma-Separated Values (CSV) Tabular Data Readers Incl. CSV <3 JSON And More
1+
# Comma-Separated Values (CSV) Tabular Data Readers Incl. CSV <3 JSON, CSV <3 YAML And More
22

33

44
Gem Family
@@ -7,7 +7,7 @@ Gem Family
77

88
[csvjson](csvjson) - read tabular data in the CSV <3 JSON format, that is, comma-separated values CSV (line-by-line) records with javascript object notation (JSON) encoding rules
99

10-
10+
[csvyaml](csvyaml) - read tabular data in the CSV <3 YAML format, that is, comma-separated values (CSV) line-by-line records with yaml ain't markup language (YAML) encoding rules
1111

1212

1313

csvyaml/.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
*.gem
2+
*.rbc
3+
/.config
4+
/coverage/
5+
/InstalledFiles
6+
/pkg/
7+
/spec/reports/
8+
/test/tmp/
9+
/test/version_tmp/
10+
/tmp/
11+
12+
## Specific to RubyMotion:
13+
.dat*
14+
.repl_history
15+
build/
16+
17+
## Documentation cache and generated files:
18+
/.yardoc/
19+
/_yardoc/
20+
/doc/
21+
/rdoc/
22+
23+
## Environment normalisation:
24+
/.bundle/
25+
/vendor/bundle
26+
/lib/bundler/man/
27+
28+
# for a library or gem, you might want to ignore these files since the code is
29+
# intended to run in multiple environments; otherwise, check them in:
30+
# Gemfile.lock
31+
# .ruby-version
32+
# .ruby-gemset
33+
34+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
35+
.rvmrc

csvyaml/HISTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### 0.0.1 / 2018-10-18
2+
3+
* Everything is new. First release

csvyaml/Manifest.txt

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
HISTORY.md
2+
LICENSE.md
3+
Manifest.txt
4+
README.md
5+
Rakefile
6+
datasets/hello.yaml.csv
7+
datasets/hello11.yaml.csv
8+
lib/csvyaml.rb
9+
lib/csvyaml/parser.rb
10+
lib/csvyaml/version.rb
11+
test/helper.rb
12+
test/test_parser.rb
13+
test/test_parser_misc.rb

csvyaml/README.md

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
# CSV <3 YAML Parser / Reader
2+
3+
csvyaml library / gem - read tabular data in the CSV <3 YAML format, that is, comma-separated values (CSV) line-by-line records with yaml ain't markup language (YAML) encoding rules
4+
5+
* home :: [github.com/csvreader/csvyaml](https://github.com/csvreader/csvyaml)
6+
* bugs :: [github.com/csvreader/csvyaml/issues](https://github.com/csvreader/csvyaml/issues)
7+
* gem :: [rubygems.org/gems/csvyaml](https://rubygems.org/gems/csvyaml)
8+
* rdoc :: [rubydoc.info/gems/csvyaml](http://rubydoc.info/gems/csvyaml)
9+
* forum :: [wwwmake](http://groups.google.com/group/wwwmake)
10+
11+
12+
## What's CSV <3 YAML?
13+
14+
CSV <3 YAML is a Comma-Separated Values (CSV)
15+
variant / format / dialect
16+
where the line-by-line records follow the
17+
YAML Ain't Markup Language (YAML) encoding rules.
18+
It's a modern (simple) tabular data format that
19+
includes arrays, numbers, booleans, nulls, nested structures, comments and more.
20+
Example:
21+
22+
23+
```
24+
# "Vanilla" CSV <3 YAML
25+
26+
1,John,12 Totem Rd. Aspen,true
27+
2,Bob,null,false
28+
3,Sue,"Bigsby, 345 Carnival, WA 23009",false
29+
```
30+
31+
or
32+
33+
```
34+
# CSV <3 YAML with array values
35+
36+
1,directions,[north,south,east,west]
37+
2,colors,[red,green,blue]
38+
3,drinks,[soda,water,tea,coffe]
39+
4,spells,[]
40+
```
41+
42+
For more see the [official CSV <3 YAML Format documentation »](https://github.com/csvspecs/csv-yaml)
43+
44+
45+
46+
## Usage
47+
48+
``` ruby
49+
txt <<=TXT
50+
# "Vanilla" CSV <3 YAML
51+
52+
1,John,12 Totem Rd. Aspen,true
53+
2,Bob,null,false
54+
3,Sue,"Bigsby, 345 Carnival, WA 23009",false
55+
TXT
56+
57+
records = CsvYaml.parse( txt ) ## or CSV_YAML.parse or CSVYAML.parse
58+
pp records
59+
# => [[1,"John","12 Totem Rd. Aspen",true],
60+
# [2,"Bob",nil,false],
61+
# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]]
62+
63+
# -or-
64+
65+
records = CsvYaml.read( "values.yaml.csv" ) ## or CSV_YAML.read or CSVYAML.read
66+
pp records
67+
# => [[1,"John","12 Totem Rd. Aspen",true],
68+
# [2,"Bob",nil,false],
69+
# [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]]
70+
71+
# -or-
72+
73+
CsvYaml.foreach( "values.yaml.csv" ) do |rec| ## or CSV_YAML.foreach or CSVYAML.foreach
74+
pp rec
75+
end
76+
# => [1,"John","12 Totem Rd. Aspen",true]
77+
# => [2,"Bob",nil,false]
78+
# => [3,"Sue","Bigsby, 345 Carnival, WA 23009",false]
79+
```
80+
81+
82+
83+
### What about Enumerable?
84+
85+
Yes, the reader / parser includes `Enumerable` and runs on `each`.
86+
Use `new` or `open` without a block
87+
to get the enumerator (iterator).
88+
Example:
89+
90+
91+
``` ruby
92+
csv = CsvYaml.new( "1,2,3" ) ## or CSV_YAML.new or CSVYAML.new
93+
it = csv.to_enum
94+
pp it.next
95+
# => [1,2,3]
96+
97+
# -or-
98+
99+
csv = CsvYaml.open( "values.yaml.csv" ) ## or CSV_YAML.open or CSVYAML.open
100+
it = csv.to_enum
101+
pp it.next
102+
# => [1,"John","12 Totem Rd. Aspen",true]
103+
pp it.next
104+
# => [2,"Bob",nil,false]
105+
```
106+
107+
108+
109+
### What about headers?
110+
111+
Yes, you can. Use the `CsvHash`
112+
from the csvreader library / gem
113+
if the first line is a header (or if missing pass in the headers
114+
as an array) and you want your records as hashes instead of arrays of strings.
115+
Example:
116+
117+
``` ruby
118+
txt <<=TXT
119+
id,name,address,regular
120+
1,John,"12 Totem Rd., Aspen",true
121+
2,Bob,null,false
122+
3,Sue,"\"Bigsby\", 345 Carnival, WA 23009",false
123+
TXT
124+
125+
records = CsvHash.yaml.parse( txt )
126+
pp records
127+
128+
# => [{"id": 1,
129+
# "name": "John",
130+
# "address": "12 Totem Rd. Aspen",
131+
# "regular": true},
132+
# {"id": 2,
133+
# "name": "Bob",
134+
# "address": null,
135+
# "regular": false},
136+
# ... ]
137+
```
138+
139+
For more see the [official CsvHash documentation in the csvreader library / gem »](https://github.com/csvreader/csvreader)
140+
141+
142+
143+
## License
144+
145+
The `csvyaml` scripts are dedicated to the public domain.
146+
Use it as you please with no restrictions whatsoever.
147+
148+
149+
## Questions? Comments?
150+
151+
Send them along to the [wwwmake forum](http://groups.google.com/group/wwwmake).
152+
Thanks!

csvyaml/Rakefile

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
require 'hoe'
2+
require './lib/csvyaml/version.rb'
3+
4+
Hoe.spec 'csvyaml' do
5+
6+
self.version = CsvYaml::VERSION
7+
8+
self.summary = "csvyaml - read tabular data in the CSV <3 YAML format, that is, comma-separated values CSV (line-by-line) records with yaml ain't markup language (YAML) encoding rules"
9+
self.description = summary
10+
11+
self.urls = ['https://github.com/csvreader/csvyaml']
12+
13+
self.author = 'Gerald Bauer'
14+
self.email = '[email protected]'
15+
16+
# switch extension to .markdown for gihub formatting
17+
self.readme_file = 'README.md'
18+
self.history_file = 'HISTORY.md'
19+
20+
self.extra_deps = [
21+
]
22+
23+
self.licenses = ['Public Domain']
24+
25+
self.spec_extras = {
26+
required_ruby_version: '>= 2.2.2'
27+
}
28+
29+
end

csvyaml/datasets/hello.yaml.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1,John,12 Totem Rd. Aspen,true
2+
2,Bob,null,false
3+
3,Sue,"Bigsby, 345 Carnival, WA 23009",false

csvyaml/datasets/hello11.yaml.csv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# hello world
2+
3+
1, John, 12 Totem Rd. Aspen, true
4+
2, Bob, null, false
5+
3, Sue, "Bigsby, 345 Carnival, WA 23009", false

csvyaml/lib/csvyaml.rb

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# encoding: utf-8
2+
3+
require 'pp'
4+
require 'yaml'
5+
require 'logger'
6+
7+
## our own code
8+
require 'csvyaml/version' # note: let version always go first
9+
require 'csvyaml/parser'
10+
11+
12+
13+
## add some "alternative" shortcut aliases
14+
CSV_YAML = CsvYaml
15+
CSVYAML = CsvYaml
16+
CSVY = CsvYaml
17+
CsvY = CsvYaml
18+
19+
20+
# say hello
21+
puts CsvYaml.banner if $DEBUG || (defined?($RUBYCOCO_DEBUG) && $RUBYCOCO_DEBUG)

0 commit comments

Comments
 (0)