gotascii-simple_importer 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/simple_importer.rb +8 -15
- data/test/test_simple_importer.rb +25 -0
- metadata +1 -1
data/History.txt
CHANGED
data/lib/simple_importer.rb
CHANGED
@@ -13,30 +13,23 @@ module SimpleImporter
|
|
13
13
|
file.close
|
14
14
|
end
|
15
15
|
|
16
|
-
def csv(path, &block)
|
16
|
+
def csv(path, *args, &block)
|
17
17
|
file(path) do |f|
|
18
18
|
parsed_file = CSV::Reader.parse(f)
|
19
|
-
process_rows(parsed_file, &block)
|
20
|
-
# parsed_file.each do |row|
|
21
|
-
# row.each {|v| v.trim! if v.respond_to? :trim! }
|
22
|
-
# yield row if block_given?
|
23
|
-
# end
|
19
|
+
process_rows(parsed_file, !args.empty?, &block)
|
24
20
|
end
|
25
21
|
end
|
26
22
|
|
27
|
-
def tsv(path, &block)
|
23
|
+
def tsv(path, *args, &block)
|
28
24
|
file(path) do |f|
|
29
25
|
parsed_file = CSV::Reader.parse(f, "\t")
|
30
|
-
process_rows(parsed_file, &block)
|
31
|
-
# parsed_file.each do |row|
|
32
|
-
# row.each {|v| v.trim! if v.respond_to? :trim! }
|
33
|
-
# yield row if block_given?
|
34
|
-
# end
|
26
|
+
process_rows(parsed_file, !args.empty?, &block)
|
35
27
|
end
|
36
28
|
end
|
37
29
|
|
38
|
-
def process_rows(parsed_file, &block)
|
39
|
-
parsed_file.
|
30
|
+
def process_rows(parsed_file, ignore_header, &block)
|
31
|
+
parsed_file.shift if ignore_header
|
32
|
+
parsed_file.each do |row|
|
40
33
|
row.each {|v| v.trim! if v.respond_to? :trim! }
|
41
34
|
yield row if block_given?
|
42
35
|
end
|
@@ -56,7 +49,7 @@ module SimpleImporter
|
|
56
49
|
end
|
57
50
|
|
58
51
|
# :stopdoc:
|
59
|
-
VERSION = '1.0.
|
52
|
+
VERSION = '1.0.1'
|
60
53
|
LIBPATH = ::File.expand_path(::File.dirname(__FILE__)) + ::File::SEPARATOR
|
61
54
|
PATH = ::File.dirname(LIBPATH) + ::File::SEPARATOR
|
62
55
|
# :startdoc:
|
@@ -6,13 +6,38 @@ end
|
|
6
6
|
|
7
7
|
context "CrazyImporter" do
|
8
8
|
specify "should parse a comma separated file" do
|
9
|
+
rows = []
|
9
10
|
CrazyImporter.csv(File.join(File.dirname(__FILE__), 'csv.txt')) do |row|
|
10
11
|
row.length.should.equal 3
|
12
|
+
rows << row
|
11
13
|
end
|
14
|
+
rows.length.should.equal 2
|
12
15
|
end
|
16
|
+
|
17
|
+
specify "should ignore header line in csv" do
|
18
|
+
rows = []
|
19
|
+
CrazyImporter.csv(File.join(File.dirname(__FILE__), 'csv.txt'), true) do |row|
|
20
|
+
row[0].should.equal "dude"
|
21
|
+
rows << row
|
22
|
+
end
|
23
|
+
rows.length.should.equal 1
|
24
|
+
end
|
25
|
+
|
13
26
|
specify "should parse a tab separated file" do
|
27
|
+
rows = []
|
14
28
|
CrazyImporter.tsv(File.join(File.dirname(__FILE__), 'tab.txt')) do |row|
|
15
29
|
row.length.should.equal 4
|
30
|
+
rows << row
|
31
|
+
end
|
32
|
+
rows.length.should.equal 2
|
33
|
+
end
|
34
|
+
|
35
|
+
specify "should ignore header line in tsv" do
|
36
|
+
rows = []
|
37
|
+
CrazyImporter.tsv(File.join(File.dirname(__FILE__), 'tab.txt'), true) do |row|
|
38
|
+
row[0].should.equal "seriously"
|
39
|
+
rows << row
|
16
40
|
end
|
41
|
+
rows.length.should.equal 1
|
17
42
|
end
|
18
43
|
end
|