array-to-csv 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +13 -0
- data/lib/array-to-csv.rb +26 -0
- data/spec/array-to-csv_spec.rb +72 -0
- metadata +56 -0
data/README
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Adds Array#to_csv method that converts the contents of the array to CSV format.
|
2
|
+
|
3
|
+
Examples:
|
4
|
+
|
5
|
+
[['a', 'b'], ['c', 'd']].to_csv
|
6
|
+
#=> "a","b"\n"c","d"
|
7
|
+
['Report on somesuch',
|
8
|
+
['name', 'value'],
|
9
|
+
['foo', 156],
|
10
|
+
['bar', 24]].to_csv
|
11
|
+
#=> "Report on somesuch"\n"name","value"\n"foo","156"\n"bar","24"
|
12
|
+
|
13
|
+
This gem is released under a broad open-source license. See LICENSE for more details.
|
data/lib/array-to-csv.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
class Array
|
2
|
+
def to_csv
|
3
|
+
tmp = []
|
4
|
+
|
5
|
+
for i in self
|
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
|
9
|
+
if i.is_a? Array
|
10
|
+
tmp << i.map{|x| "\"#{x.to_s.escape_csv_quotes}\""}.join(',')
|
11
|
+
elsif i.is_a? String
|
12
|
+
tmp << "\"#{i.escape_csv_quotes}\""
|
13
|
+
elsif i.is_a? Hash
|
14
|
+
tmp << i.map{|k, v| "\"#{k.to_s.escape_csv_quotes}: #{v.to_s.escape_csv_quotes}\""}.join(',')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
tmp.join("\n")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class String
|
23
|
+
def escape_csv_quotes
|
24
|
+
gsub('"','""')
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
# TODO replace the it declarations with more meaningful and descriptive, er, descriptions
|
4
|
+
describe Array do
|
5
|
+
context "managing converting itself to CSV format" do
|
6
|
+
it "returns '' on an emtpy array" do
|
7
|
+
data = []
|
8
|
+
|
9
|
+
data.to_csv.should == ""
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns 'a\nb\nc' for an array ['a', 'b', 'c']" do
|
13
|
+
data = ['a', 'b', 'c']
|
14
|
+
|
15
|
+
data.to_csv.should == %Q("a"\n"b"\n"c")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns 'a,b\nc,d' for an array [['a', 'b'], ['c', 'd']]" do
|
19
|
+
data = [['a', 'b'], ['c', 'd']]
|
20
|
+
|
21
|
+
data.to_csv.should == %Q("a","b"\n"c","d")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns 'a: b\nc: d' for an array [{:a => 'b'}, {:c => 'd'}]" do
|
25
|
+
data = [{:a => 'b'}, {:c => 'd'}]
|
26
|
+
|
27
|
+
data.to_csv.should == %Q("a: b"\n"c: d")
|
28
|
+
end
|
29
|
+
|
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
|
31
|
+
data = [['Patrick', Date.new(2009,9,13)], ['Angela', Date.new(2009,8,17)]]
|
32
|
+
|
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""'
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array-to-csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Byrne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-19 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: code@patrickbyrne.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- lib/array-to-csv.rb
|
26
|
+
- README
|
27
|
+
has_rdoc: true
|
28
|
+
homepage: http://github.com/pbyrne/array-to-csv/
|
29
|
+
licenses: []
|
30
|
+
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.3.5
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Adds Array#to_csv method that converts the contents of the array to CSV format.
|
55
|
+
test_files:
|
56
|
+
- spec/array-to-csv_spec.rb
|