pbyrne-array-to-csv 1.0.1
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 +13 -0
- data/lib/array-to-csv.rb +18 -0
- data/spec/array-to-csv_spec.rb +37 -0
- metadata +54 -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\nc,d"
|
7
|
+
['Report on somesuch',
|
8
|
+
['name', 'value'],
|
9
|
+
['foo', 156],
|
10
|
+
['bar', 24]].to_csv
|
11
|
+
#=> "Report on somesuch\nname,value\nfoo,156\nbar,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,18 @@
|
|
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
|
+
if i.is_a? Array
|
8
|
+
tmp << i.map{|x| "\"#{x}\""}.join(',')
|
9
|
+
elsif i.is_a? String
|
10
|
+
tmp << "\"#{i}\""
|
11
|
+
elsif i.is_a? Hash
|
12
|
+
tmp << i.map{|k, v| "\"#{k}: #{v}\""}.join(',')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
tmp.join("\n")
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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
|
+
# TODO would heredoc be better and more expressive than a bunch of escaped quotes?
|
5
|
+
describe Array do
|
6
|
+
context "managing converting itself to CSV format" do
|
7
|
+
it "returns '' on an emtpy array" do
|
8
|
+
data = []
|
9
|
+
|
10
|
+
data.to_csv.should == ""
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns 'a\nb\nc' for an array ['a', 'b', 'c']" do
|
14
|
+
data = ['a', 'b', 'c']
|
15
|
+
|
16
|
+
data.to_csv.should == "\"a\"\n\"b\"\n\"c\""
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns 'a,b\nc,d' for an array [['a', 'b'], ['c', 'd']]" do
|
20
|
+
data = [['a', 'b'], ['c', 'd']]
|
21
|
+
|
22
|
+
data.to_csv.should == "\"a\",\"b\"\n\"c\",\"d\""
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns 'a: b\nc: d' for an array [{:a => 'b'}, {:c => 'd'}]" do
|
26
|
+
data = [{:a => 'b'}, {:c => 'd'}]
|
27
|
+
|
28
|
+
data.to_csv.should == "\"a: b\"\n\"c: d\""
|
29
|
+
end
|
30
|
+
|
31
|
+
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
|
+
data = [['Patrick', Date.new(2009,9,13)], ['Angela', Date.new(2009,8,17)]]
|
33
|
+
|
34
|
+
data.to_csv.should == "\"Patrick\",\"2009-09-13\"\n\"Angela\",\"2009-08-17\""
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pbyrne-array-to-csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Patrick Byrne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07: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
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: "0"
|
39
|
+
version:
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
requirements: []
|
47
|
+
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.2.0
|
50
|
+
signing_key:
|
51
|
+
specification_version: 2
|
52
|
+
summary: Adds Array#to_csv method that converts the contents of the array to CSV format.
|
53
|
+
test_files:
|
54
|
+
- spec/array-to-csv_spec.rb
|