simplecsv 0.1.3 → 0.1.4
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.
- data/History.txt +5 -0
- data/Manifest.txt +4 -0
- data/README.txt +1 -1
- data/Rakefile +1 -1
- data/lib/simplecsv/extension.rb +23 -0
- data/lib/simplecsv/version.rb +2 -2
- data/test/data/01.csv +1 -0
- data/test/data/02.csv +2 -0
- data/test/data/03.csv +5 -0
- data/test/simplecsv/simplecsv_test.rb +63 -0
- metadata +6 -2
data/History.txt
CHANGED
data/Manifest.txt
CHANGED
@@ -11,6 +11,10 @@ ext/simplecsv/extconf.rb
|
|
11
11
|
ext/simplecsv/rb_simplecsv.c
|
12
12
|
ext/simplecsv/rb_simplecsv.h
|
13
13
|
lib/simplecsv.rb
|
14
|
+
lib/simplecsv/extension.rb
|
14
15
|
lib/simplecsv/version.rb
|
15
16
|
setup.rb
|
17
|
+
test/data/01.csv
|
18
|
+
test/data/02.csv
|
19
|
+
test/data/03.csv
|
16
20
|
test/simplecsv/simplecsv_test.rb
|
data/README.txt
CHANGED
data/Rakefile
CHANGED
@@ -52,7 +52,7 @@ hoe = Hoe.new(GEM_NAME, VERS) do |p|
|
|
52
52
|
#p.extra_deps - An array of rubygem dependencies.
|
53
53
|
#p.spec_extras - A hash of extra values to set in the gemspec.
|
54
54
|
|
55
|
-
p.rdoc_pattern = /^(bin|ext)|txt$/
|
55
|
+
p.rdoc_pattern = /^(lib|bin|ext)|txt$/
|
56
56
|
p.spec_extras = {
|
57
57
|
:extensions => ['ext/simplecsv/extconf.rb']
|
58
58
|
}
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class SimpleCSV
|
2
|
+
# All methods of module SimpleCSV::Extension are
|
3
|
+
# extended & included by SimpleCSV.
|
4
|
+
# For example, you can use SimpleCSV.parse_file SimpleCSV#parse_file.
|
5
|
+
module Extension
|
6
|
+
# Parse csv file.
|
7
|
+
# See SimpleCSV.parse for details.
|
8
|
+
#
|
9
|
+
# ==== Parameters
|
10
|
+
# [file_name] <code>String</code> : csv file name.
|
11
|
+
# [options] <code>Integer</code> : parser options.
|
12
|
+
# [row] <code>Array</code> of <code>String</code>s : parsed data.
|
13
|
+
def parse_file(file_name, options = nil, &block)
|
14
|
+
File.foreach(file_name, options) do |line|
|
15
|
+
parse(line, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
extend(Extension)
|
21
|
+
include(Extension)
|
22
|
+
end
|
23
|
+
|
data/lib/simplecsv/version.rb
CHANGED
data/test/data/01.csv
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0, 1, 2, 3
|
data/test/data/02.csv
ADDED
@@ -1,4 +1,5 @@
|
|
1
1
|
$:.unshift(File.join(File.dirname(__FILE__), '..', '..', 'ext', 'simplecsv'))
|
2
|
+
$:.unshift(File.join(File.dirname(__FILE__), '..', '..', 'lib'))
|
2
3
|
|
3
4
|
require 'simplecsv'
|
4
5
|
require 'test/unit'
|
@@ -368,4 +369,66 @@ _CSV_
|
|
368
369
|
assert_equal ["L", "c"], row
|
369
370
|
end
|
370
371
|
end
|
372
|
+
|
373
|
+
class DefaultParser < SimpleCSV
|
374
|
+
def initialize
|
375
|
+
@row = []
|
376
|
+
end
|
377
|
+
|
378
|
+
def on_field(str, pr)
|
379
|
+
@row << str
|
380
|
+
end
|
381
|
+
|
382
|
+
def on_row(str, pr)
|
383
|
+
pr.call(@row)
|
384
|
+
@row = []
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_parse_file
|
389
|
+
csv_file = File.join(data_path, '01.csv')
|
390
|
+
SimpleCSV.parse_file(csv_file) do |row|
|
391
|
+
assert_equal ["0", "1", "2", "3"], row
|
392
|
+
end
|
393
|
+
|
394
|
+
simplecsv = SimpleCSV.new
|
395
|
+
simplecsv.parse_file(csv_file) do |row|
|
396
|
+
assert_equal ["0", "1", "2", "3"], row
|
397
|
+
end
|
398
|
+
|
399
|
+
DefaultParser.parse_file(csv_file) do |row|
|
400
|
+
assert_equal ["0", "1", "2", "3"], row
|
401
|
+
end
|
402
|
+
|
403
|
+
csv_file = File.join(data_path, '02.csv')
|
404
|
+
rows = []
|
405
|
+
SimpleCSV.parse_file(csv_file) do |row|
|
406
|
+
rows << row
|
407
|
+
end
|
408
|
+
assert_equal [ ["ab", "cde", "fg"], ["hijk", "lmn"] ], rows
|
409
|
+
|
410
|
+
rows = []
|
411
|
+
DefaultParser.parse_file(csv_file) do |row|
|
412
|
+
rows << row
|
413
|
+
end
|
414
|
+
assert_equal [ ["ab", "cde", "fg"], ["hijk", "lmn"] ], rows
|
415
|
+
|
416
|
+
csv_file = File.join(data_path, '03.csv')
|
417
|
+
rows = []
|
418
|
+
SimpleCSV.parse_file(csv_file) do |row|
|
419
|
+
rows << row
|
420
|
+
end
|
421
|
+
assert_equal [ ["1"], ["5"] ], rows
|
422
|
+
|
423
|
+
rows = []
|
424
|
+
DefaultParser.parse_file(csv_file) do |row|
|
425
|
+
rows << row
|
426
|
+
end
|
427
|
+
assert_equal [ ["1"], ["5"] ], rows
|
428
|
+
end
|
429
|
+
|
430
|
+
private
|
431
|
+
def data_path
|
432
|
+
File.join(File.dirname(__FILE__), '..', 'data')
|
433
|
+
end
|
371
434
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: simplecsv
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.1.
|
7
|
-
date: 2007-
|
6
|
+
version: 0.1.4
|
7
|
+
date: 2007-04-01 00:00:00 +09:00
|
8
8
|
summary: SimpleCSV privides simple csv reading/writing api.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -42,8 +42,12 @@ files:
|
|
42
42
|
- ext/simplecsv/rb_simplecsv.c
|
43
43
|
- ext/simplecsv/rb_simplecsv.h
|
44
44
|
- lib/simplecsv.rb
|
45
|
+
- lib/simplecsv/extension.rb
|
45
46
|
- lib/simplecsv/version.rb
|
46
47
|
- setup.rb
|
48
|
+
- test/data/01.csv
|
49
|
+
- test/data/02.csv
|
50
|
+
- test/data/03.csv
|
47
51
|
- test/simplecsv/simplecsv_test.rb
|
48
52
|
test_files:
|
49
53
|
- test/simplecsv/simplecsv_test.rb
|