pbyrne-array-to-csv 1.0.1 → 1.1.0

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/README CHANGED
@@ -3,11 +3,11 @@ Adds Array#to_csv method that converts the contents of the array to CSV format.
3
3
  Examples:
4
4
 
5
5
  [['a', 'b'], ['c', 'd']].to_csv
6
- #=> "a,b\nc,d"
6
+ #=> "a","b"\n"c","d"
7
7
  ['Report on somesuch',
8
8
  ['name', 'value'],
9
9
  ['foo', 156],
10
10
  ['bar', 24]].to_csv
11
- #=> "Report on somesuch\nname,value\nfoo,156\nbar,24"
11
+ #=> "Report on somesuch"\n"name","value"\n"foo","156"\n"bar","24"
12
12
 
13
13
  This gem is released under a broad open-source license. See LICENSE for more details.
data/lib/array-to-csv.rb CHANGED
@@ -4,15 +4,23 @@ class Array
4
4
 
5
5
  for i in self
6
6
  # todo research case syntax, since these elsifs are ugly
7
+ # todo investigate switching to %Q syntax to make less ugly
8
+ # todo refactor out the duplicate gsubs
7
9
  if i.is_a? Array
8
- tmp << i.map{|x| "\"#{x}\""}.join(',')
10
+ tmp << i.map{|x| "\"#{x.to_s.escape_csv_quotes}\""}.join(',')
9
11
  elsif i.is_a? String
10
- tmp << "\"#{i}\""
12
+ tmp << "\"#{i.escape_csv_quotes}\""
11
13
  elsif i.is_a? Hash
12
- tmp << i.map{|k, v| "\"#{k}: #{v}\""}.join(',')
14
+ tmp << i.map{|k, v| "\"#{k.to_s.escape_csv_quotes}: #{v.to_s.escape_csv_quotes}\""}.join(',')
13
15
  end
14
16
  end
15
17
 
16
18
  tmp.join("\n")
17
19
  end
18
- end
20
+ end
21
+
22
+ class String
23
+ def escape_csv_quotes
24
+ gsub('"','""')
25
+ end
26
+ end
@@ -1,7 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  # TODO replace the it declarations with more meaningful and descriptive, er, descriptions
4
- # TODO would heredoc be better and more expressive than a bunch of escaped quotes?
5
4
  describe Array do
6
5
  context "managing converting itself to CSV format" do
7
6
  it "returns '' on an emtpy array" do
@@ -13,25 +12,61 @@ describe Array do
13
12
  it "returns 'a\nb\nc' for an array ['a', 'b', 'c']" do
14
13
  data = ['a', 'b', 'c']
15
14
 
16
- data.to_csv.should == "\"a\"\n\"b\"\n\"c\""
15
+ data.to_csv.should == %Q("a"\n"b"\n"c")
17
16
  end
18
17
 
19
18
  it "returns 'a,b\nc,d' for an array [['a', 'b'], ['c', 'd']]" do
20
19
  data = [['a', 'b'], ['c', 'd']]
21
20
 
22
- data.to_csv.should == "\"a\",\"b\"\n\"c\",\"d\""
21
+ data.to_csv.should == %Q("a","b"\n"c","d")
23
22
  end
24
23
 
25
24
  it "returns 'a: b\nc: d' for an array [{:a => 'b'}, {:c => 'd'}]" do
26
25
  data = [{:a => 'b'}, {:c => 'd'}]
27
26
 
28
- data.to_csv.should == "\"a: b\"\n\"c: d\""
27
+ data.to_csv.should == %Q("a: b"\n"c: d")
29
28
  end
30
29
 
31
30
  it "returns 'Patrick,2009-09-13\nAngela,2009-08-17' for an array [['Patrick', Date.new(2009,9,13)], ['Angela', Date.new(2009,8,17)]]" do
32
31
  data = [['Patrick', Date.new(2009,9,13)], ['Angela', Date.new(2009,8,17)]]
33
32
 
34
- data.to_csv.should == "\"Patrick\",\"2009-09-13\"\n\"Angela\",\"2009-08-17\""
33
+ data.to_csv.should == %Q("Patrick","2009-09-13"\n"Angela","2009-08-17")
34
+ end
35
+
36
+ context "managing the escaping of double quotes in the cells" do
37
+ it "correctly escapes double quotes in nested arrays" do
38
+ data = [["a href=\"foo\"", "div"], ["link rel=\"bar\"","blockquote"]]
39
+
40
+ data.to_csv.should == %Q("a href=""foo""","div"\n"link rel=""bar""","blockquote")
41
+ end
42
+
43
+ it "correctly escapes double quotes in strings" do
44
+ data = ["a href=\"foo\"", "link rel=\"bar\""]
45
+
46
+ data.to_csv.should == %Q("a href=""foo"""\n"link rel=""bar""")
47
+ end
48
+
49
+ it "correctly escapes double quotes in hashes" do
50
+ data = [{ :url => 'a href="foo"', :style => 'link rel="bar"'}]
51
+
52
+ data.to_csv.should == %Q("url: a href=""foo""","style: link rel=""bar""")
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ describe String do
59
+ context "handling escaping double quotes in CSV" do
60
+ it "returns empty string when escaping an empty string" do
61
+ "".escape_csv_quotes.should == ""
62
+ end
63
+
64
+ it "returns the origal string if doesn't have any double-quotes" do
65
+ "foo".escape_csv_quotes.should == "foo"
66
+ end
67
+
68
+ it "returns two double quotes for every double quote" do
69
+ 'a href="foo"'.escape_csv_quotes.should == 'a href=""foo""'
35
70
  end
36
71
  end
37
- end
72
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pbyrne-array-to-csv
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Byrne