array_to_csv 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/Rakefile +9 -0
- data/array_to_csv.gemspec +22 -0
- data/lib/array_to_csv.rb +53 -0
- data/lib/array_to_csv/core_ext.rb +9 -0
- data/spec/array_to_csv_spec.rb +44 -0
- data/spec/core_ext_spec.rb +19 -0
- data/test_helper.rb +33 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c1a14acec1c38d14bc166fcc2e3c885a00664747
|
4
|
+
data.tar.gz: fb3982fb10d8bec759d05fa61de02ed2a9bad320
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 54da663d1c117d226a8f616ef198a6e899f2bad3fa9e2f039482966f189286e90df7869077961db12e00f6b39f448e152cfeefda82c2709c618238d188163dbd
|
7
|
+
data.tar.gz: f781a7c56fc1bb835649113d704c1efe71e47c361bd692d0efb383047d94b94f0bc368df5af9092b5b04e2fac103879e7e4c16671ad2031e8cb4d56e018a5c27
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Joel Plane
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "array_to_csv"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Joel Plane"]
|
9
|
+
spec.email = ["joel.plane@gmail.com"]
|
10
|
+
spec.description = %q{Adds convenience method Array#to_csv for converting an array of hashes to CSV.}
|
11
|
+
spec.summary = %q{Convert array of hashes to CSV}
|
12
|
+
spec.homepage = "https://github.com/joelplane/array_to_csv"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.test_files = spec.files.grep(%r{^spec/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
20
|
+
spec.add_development_dependency "rake"
|
21
|
+
spec.add_development_dependency "rspec"
|
22
|
+
end
|
data/lib/array_to_csv.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
class ArrayToCsv
|
2
|
+
|
3
|
+
def initialize array, csv_lib=choose_csv_lib
|
4
|
+
@array = array
|
5
|
+
@csv = csv_lib
|
6
|
+
end
|
7
|
+
|
8
|
+
def to_csv
|
9
|
+
@csv.generate do |csv|
|
10
|
+
each_row do |row|
|
11
|
+
csv << row
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def each_row
|
19
|
+
yield head
|
20
|
+
@array.each do |hash|
|
21
|
+
yield row_from_hash hash
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def head
|
26
|
+
@head_from_array ||= [].tap do |keys|
|
27
|
+
seen_keys = {}
|
28
|
+
@array.each do |hash|
|
29
|
+
hash.keys.each do |key|
|
30
|
+
unless seen_keys[key]
|
31
|
+
seen_keys[key] = true
|
32
|
+
keys << key
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def row_from_hash hash
|
40
|
+
hash.values_at(*head)
|
41
|
+
end
|
42
|
+
|
43
|
+
# TODO: return FasterCSV in old ruby
|
44
|
+
def choose_csv_lib
|
45
|
+
if RUBY_VERSION =~ /^1\.8/
|
46
|
+
FasterCSV
|
47
|
+
else
|
48
|
+
CSV
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
if RUBY_VERSION =~ /^1\.8/
|
2
|
+
require 'fastercsv'
|
3
|
+
else
|
4
|
+
require 'csv'
|
5
|
+
end
|
6
|
+
require 'array_to_csv'
|
7
|
+
|
8
|
+
describe ArrayToCsv do
|
9
|
+
|
10
|
+
describe ".to_csv" do
|
11
|
+
|
12
|
+
context "given an empty array" do
|
13
|
+
it "should produce an empty CSV" do
|
14
|
+
expect(ArrayToCsv.new([]).to_csv).to eq("\n")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "given an array of one empty hash" do
|
19
|
+
it "should produce an empty CSV" do
|
20
|
+
expect(ArrayToCsv.new([{}]).to_csv).to eq("\n\n")
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "given an array of one hash" do
|
25
|
+
it "should produce a one-value-row CSV" do
|
26
|
+
expect(ArrayToCsv.new([{:key => 'value'}]).to_csv).to eq("key\nvalue\n")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "given an array of two hashes" do
|
31
|
+
it "should produce a CSV with two value rows" do
|
32
|
+
expect(ArrayToCsv.new([{:key => 'value1'}, {:key => 'value2'}]).to_csv).to eq("key\nvalue1\nvalue2\n")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "given an array of two hashes with different keys" do
|
37
|
+
it "should produce a CSV with column headings combined from both hash keys, and two value rows" do
|
38
|
+
expect(ArrayToCsv.new([{:key1 => 'value1'}, {:key2 => 'value2'}]).to_csv).to eq("key1,key2\nvalue1,\n,value2\n")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
if RUBY_VERSION =~ /^1\.8/
|
2
|
+
require 'fastercsv'
|
3
|
+
else
|
4
|
+
require 'csv'
|
5
|
+
end
|
6
|
+
require 'array_to_csv/core_ext'
|
7
|
+
|
8
|
+
describe "array_to_csv/core_ext" do
|
9
|
+
|
10
|
+
it "should add to_csv method to Array class" do
|
11
|
+
expect([]).to respond_to :to_csv
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should cause Array#to_csv to delegate to ArrayToCsv#to_csv" do
|
15
|
+
array = [{:key => 'value'}]
|
16
|
+
expect(array.to_csv).to eq(ArrayToCsv.new(array).to_csv)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
data/test_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
class TestHelper
|
2
|
+
|
3
|
+
# @return [Boolean] successful
|
4
|
+
def self.test_against_rubies ruby_versions_without_patchlevel
|
5
|
+
successes = []
|
6
|
+
File.rename "Gemfile.lock", "Gemfile.lock.original"
|
7
|
+
resolve_versions(ruby_versions_without_patchlevel).each do |version|
|
8
|
+
puts "Running tests against ruby #{version}"
|
9
|
+
File.delete "Gemfile.lock" rescue nil
|
10
|
+
success = system(%{/bin/bash -l -c 'source "$HOME/.rvm/scripts/rvm"; rvm use #{version}; bundle install && bundle exec rspec'})
|
11
|
+
successes << success
|
12
|
+
unless success
|
13
|
+
puts "Failed against ruby #{version}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
File.rename "Gemfile.lock.original", "Gemfile.lock"
|
17
|
+
successes.all?{|s| s}
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# map versions to patch level installed, if any
|
23
|
+
def self.resolve_versions versions_without_patchlevel
|
24
|
+
paths = Dir["#{ENV['HOME']}/.rvm/rubies/ruby-*"]
|
25
|
+
versions_with_patchlevel = paths.collect{|p|p.split('ruby-').last}.sort.reverse
|
26
|
+
versions_without_patchlevel.collect do |version|
|
27
|
+
versions_with_patchlevel.detect do |version_with_patchlevel|
|
28
|
+
version_with_patchlevel.start_with? "#{version}-"
|
29
|
+
end || version
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array_to_csv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joel Plane
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Adds convenience method Array#to_csv for converting an array of hashes
|
56
|
+
to CSV.
|
57
|
+
email:
|
58
|
+
- joel.plane@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- .gitignore
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- Rakefile
|
67
|
+
- array_to_csv.gemspec
|
68
|
+
- lib/array_to_csv.rb
|
69
|
+
- lib/array_to_csv/core_ext.rb
|
70
|
+
- spec/array_to_csv_spec.rb
|
71
|
+
- spec/core_ext_spec.rb
|
72
|
+
- test_helper.rb
|
73
|
+
homepage: https://github.com/joelplane/array_to_csv
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.5
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Convert array of hashes to CSV
|
97
|
+
test_files:
|
98
|
+
- spec/array_to_csv_spec.rb
|
99
|
+
- spec/core_ext_spec.rb
|