csv-hash 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/csv-hash.gemspec +1 -1
- data/lib/csv-hash.rb +4 -4
- data/spec/assets/clean_test.csv +3 -3
- data/spec/assets/test.csv +3 -3
- data/spec/csv-hash_spec.rb +11 -4
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.2
|
data/csv-hash.gemspec
CHANGED
data/lib/csv-hash.rb
CHANGED
@@ -11,15 +11,15 @@ module CSVHash
|
|
11
11
|
FasterCSV.foreach(file) do |row|
|
12
12
|
num += 1
|
13
13
|
if num == 1
|
14
|
-
columns = row.collect {|c| c.strip}
|
14
|
+
columns = row.collect {|c| c.strip if c}
|
15
15
|
next
|
16
16
|
end
|
17
17
|
|
18
18
|
hash = {}
|
19
19
|
columns.each_with_index do |col, i|
|
20
20
|
next unless col
|
21
|
-
col = col.strip
|
22
|
-
val = row[i].strip
|
21
|
+
col = col ? col.strip : nil
|
22
|
+
val = row[i] ? row[i].strip : nil
|
23
23
|
|
24
24
|
setter = hash
|
25
25
|
sp = col.split('__')
|
@@ -38,7 +38,7 @@ module CSVHash
|
|
38
38
|
data << hash
|
39
39
|
end
|
40
40
|
|
41
|
-
return data, columns
|
41
|
+
return data, columns.compact
|
42
42
|
end
|
43
43
|
|
44
44
|
def to_string hashes, columns
|
data/spec/assets/clean_test.csv
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
one,two,foo__bar__three,foo__bar__four,foo__five,bar__six,bar__seven
|
2
|
-
1,2,3,4,5,6
|
3
|
-
14,13,12,11,10,9
|
1
|
+
one,two,foo__bar__three,foo__bar__four,foo__five,bar__six,nil,bar__seven
|
2
|
+
1,2,3,4,5,6,,7
|
3
|
+
14,13,12,11,10,9,,8
|
data/spec/assets/test.csv
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
one,two,foo__bar__three,foo__bar__four, foo__five,bar__six,bar__seven
|
2
|
-
1,2,3,4,5, 6
|
3
|
-
14,13,12,11,10,9
|
1
|
+
one,two,foo__bar__three,foo__bar__four, foo__five,bar__six,nil,,bar__seven
|
2
|
+
1,2,3,4,5, 6,,,7
|
3
|
+
14,13,12,11,10,9,,,8
|
data/spec/csv-hash_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe CSVHash do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
it "should get correct column names (no spaces)" do
|
15
|
-
@columns.should == "one,two,foo__bar__three,foo__bar__four,foo__five,bar__six,bar__seven"
|
15
|
+
@columns.should == ["one", "two", "foo__bar__three", "foo__bar__four", "foo__five", "bar__six", "nil", "bar__seven"]
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should only include data rows" do
|
@@ -27,7 +27,8 @@ describe CSVHash do
|
|
27
27
|
"bar" => {"three" => "3", "four" => "4"},
|
28
28
|
"five" => "5"
|
29
29
|
},
|
30
|
-
"bar" => {"six" => "6", "seven" => "7"}
|
30
|
+
"bar" => {"six" => "6", "seven" => "7"},
|
31
|
+
"nil" => nil
|
31
32
|
}
|
32
33
|
@array.first.should == first
|
33
34
|
|
@@ -38,14 +39,20 @@ describe CSVHash do
|
|
38
39
|
"bar" => {"three" => "12", "four" => "11"},
|
39
40
|
"five" => "10"
|
40
41
|
},
|
41
|
-
"bar" => {"six" => "9", "seven" => "8"}
|
42
|
+
"bar" => {"six" => "9", "seven" => "8"},
|
43
|
+
"nil" => nil
|
42
44
|
}
|
43
45
|
@array.last.should == second
|
44
46
|
end
|
45
47
|
end
|
46
48
|
|
47
49
|
|
48
|
-
it "should
|
50
|
+
it "should generate an expected file" do
|
49
51
|
CSVHash.to_string(*CSVHash.from_file(@csv_path)).chomp.should == File.open(@clean_csv_path).read.chomp
|
50
52
|
end
|
53
|
+
|
54
|
+
it "should do a round trip" do
|
55
|
+
pending "Desire to add nil columns"
|
56
|
+
CSVHash.to_string(*CSVHash.from_file(@csv_path)).chomp.should == File.open(@csv_path).read.chomp
|
57
|
+
end
|
51
58
|
end
|