enum_csv 1.0.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.
- checksums.yaml +7 -0
- data/CHANGELOG +3 -0
- data/MIT-LICENSE +18 -0
- data/README.rdoc +46 -0
- data/Rakefile +69 -0
- data/lib/enum_csv.rb +44 -0
- data/spec/enum_csv_spec.rb +43 -0
- metadata +61 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 5adcc47fbd7b34a09baac9c8ac6c54f86c316ba1
|
|
4
|
+
data.tar.gz: 47d03538cf37af19be66993d618526b6bc03dddb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 532750b7c690dd1960462a18ddb610915cc70effa99c46719e1ffc4108503b3715e9f4ec26f62ff591765bda336a7f22add9438e6f43ef367659cf9319aadacd
|
|
7
|
+
data.tar.gz: e72e5b42e72913a3840f297f4a6684f26424e77aaff93136bc2174aaf37d3a110e8d87066130e410cd47e131f8fc23a1ba6b5a81b76b892d2be7774f77fa3bee
|
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Copyright (c) 2013 Jeremy Evans
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
|
5
|
+
deal in the Software without restriction, including without limitation the
|
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
|
8
|
+
furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
|
11
|
+
all copies or substantial portions of the Software.
|
|
12
|
+
|
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
= EnumCSV
|
|
2
|
+
|
|
3
|
+
EnumCSV exposes a single method, csv, for easily creating CSV
|
|
4
|
+
output/files from enumerable objects. It is a super simple wrapper
|
|
5
|
+
class for ruby's csv library.
|
|
6
|
+
|
|
7
|
+
= Installation
|
|
8
|
+
|
|
9
|
+
sudo gem install enum_csv
|
|
10
|
+
|
|
11
|
+
= Source Code
|
|
12
|
+
|
|
13
|
+
Source code is available on GitHub at https://github.com/jeremyevans/enum_csv
|
|
14
|
+
|
|
15
|
+
= Examples
|
|
16
|
+
|
|
17
|
+
The default behavior just expects an enumerable of arrays (such as an array of
|
|
18
|
+
arrays), and returns a string containing the CSV output:
|
|
19
|
+
|
|
20
|
+
EnumCSV.csv([[1, 2]]) => "1,2\n"
|
|
21
|
+
|
|
22
|
+
You can use the :headers option to set custom headers:
|
|
23
|
+
|
|
24
|
+
EnumCSV.csv([[1, 2], [3,"4,5"]], :headers=>['A', 'B']) => "A,B\n1,2\n3,\"4,5\"\n"
|
|
25
|
+
|
|
26
|
+
The :file option can be used to output to a file:
|
|
27
|
+
|
|
28
|
+
EnumCSV.csv([[1, 2]], :file=>'foo.csv') => nil # output written to foo.csv
|
|
29
|
+
|
|
30
|
+
If a block is passed to the method, all items in the enumerable are yielded to
|
|
31
|
+
the block, and the block should return an array with the data to use in the CSV
|
|
32
|
+
output:
|
|
33
|
+
|
|
34
|
+
EnumCSV.csv([{:a=>1, :b=>2}, {:a=>3, b=>4}]){|line| [l[:b], l[:a] + 10]} => "2,11\n4,13\n"
|
|
35
|
+
|
|
36
|
+
= Ruby 1.8 Usage
|
|
37
|
+
|
|
38
|
+
On Ruby 1.8, you need to install the fastercsv gem to use this library.
|
|
39
|
+
|
|
40
|
+
= License
|
|
41
|
+
|
|
42
|
+
MIT
|
|
43
|
+
|
|
44
|
+
= Author
|
|
45
|
+
|
|
46
|
+
Jeremy Evans <code@jeremyevans.net>
|
data/Rakefile
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require "rake"
|
|
2
|
+
require "rake/clean"
|
|
3
|
+
|
|
4
|
+
CLEAN.include ["enum_csv-*.gem", "rdoc", "coverage"]
|
|
5
|
+
|
|
6
|
+
desc "Build enum_csv gem"
|
|
7
|
+
task :package=>[:clean] do |p|
|
|
8
|
+
sh %{#{FileUtils::RUBY} -S gem build enum_csv.gemspec}
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
### Specs
|
|
12
|
+
|
|
13
|
+
begin
|
|
14
|
+
begin
|
|
15
|
+
# RSpec 1
|
|
16
|
+
require "spec/rake/spectask"
|
|
17
|
+
spec_class = Spec::Rake::SpecTask
|
|
18
|
+
spec_files_meth = :spec_files=
|
|
19
|
+
rescue LoadError
|
|
20
|
+
# RSpec 2
|
|
21
|
+
require "rspec/core/rake_task"
|
|
22
|
+
spec_class = RSpec::Core::RakeTask
|
|
23
|
+
spec_files_meth = :pattern=
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
spec = lambda do |name, files, d|
|
|
27
|
+
lib_dir = File.join(File.dirname(File.expand_path(__FILE__)), 'lib')
|
|
28
|
+
ENV['RUBYLIB'] ? (ENV['RUBYLIB'] += ":#{lib_dir}") : (ENV['RUBYLIB'] = lib_dir)
|
|
29
|
+
desc d
|
|
30
|
+
spec_class.new(name) do |t|
|
|
31
|
+
t.send(spec_files_meth, files)
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
task :default => [:spec]
|
|
36
|
+
spec.call("spec", Dir["spec/*_spec.rb"], "Run specs")
|
|
37
|
+
rescue LoadError
|
|
38
|
+
task :default do
|
|
39
|
+
puts "Must install rspec >=2.0 to run the default task (which runs specs)"
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
### RDoc
|
|
44
|
+
|
|
45
|
+
RDOC_DEFAULT_OPTS = ["--quiet", "--line-numbers", "--inline-source", '--title', 'EnumCSV: Create CSV from Enumerables']
|
|
46
|
+
|
|
47
|
+
begin
|
|
48
|
+
gem 'rdoc', '= 3.12.2'
|
|
49
|
+
gem 'hanna-nouveau'
|
|
50
|
+
RDOC_DEFAULT_OPTS.concat(['-f', 'hanna'])
|
|
51
|
+
rescue Gem::LoadError
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
rdoc_task_class = begin
|
|
55
|
+
require "rdoc/task"
|
|
56
|
+
RDoc::Task
|
|
57
|
+
rescue LoadError
|
|
58
|
+
require "rake/rdoctask"
|
|
59
|
+
Rake::RDocTask
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
RDOC_OPTS = RDOC_DEFAULT_OPTS + ['--main', 'README.rdoc']
|
|
63
|
+
|
|
64
|
+
rdoc_task_class.new do |rdoc|
|
|
65
|
+
rdoc.rdoc_dir = "rdoc"
|
|
66
|
+
rdoc.options += RDOC_OPTS
|
|
67
|
+
rdoc.rdoc_files.add %w"README.rdoc CHANGELOG MIT-LICENSE lib/**/*.rb"
|
|
68
|
+
end
|
|
69
|
+
|
data/lib/enum_csv.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# EnumCSV exposes a single method, csv, for easily creating CSV
|
|
2
|
+
# output/files from enumerable objects.
|
|
3
|
+
module EnumCSV
|
|
4
|
+
if RUBY_VERSION >= '1.9'
|
|
5
|
+
require 'csv'
|
|
6
|
+
else
|
|
7
|
+
require 'fastercsv'
|
|
8
|
+
# Use FasterCSV on ruby 1.8
|
|
9
|
+
CSV = ::FasterCSV
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Create CSV from the given Enumerable object. If a block is given,
|
|
13
|
+
# each item in the enumerable is yielded to the block, and the block
|
|
14
|
+
# should return an array of of items to use for the CSV line.
|
|
15
|
+
#
|
|
16
|
+
# Options:
|
|
17
|
+
# :headers :: Should be an array of headers to use as the first
|
|
18
|
+
# line of the CSV output.
|
|
19
|
+
# :file :: Output to the given file and return nil instead of
|
|
20
|
+
# returning a string with the CSV output.
|
|
21
|
+
# all other options :: Passed to CSV.open or CSV.generate
|
|
22
|
+
def csv(enum, opts={})
|
|
23
|
+
if opts[:headers].is_a?(Array) && !opts.has_key?(:write_headers)
|
|
24
|
+
opts = opts.merge(:write_headers=>true)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
csv_proc = proc do |csv|
|
|
28
|
+
enum.each do |line|
|
|
29
|
+
line = yield(line) if block_given?
|
|
30
|
+
csv << line
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
if opts[:file]
|
|
35
|
+
opts = opts.dup
|
|
36
|
+
file = opts.delete(:file)
|
|
37
|
+
CSV.open(file, 'wb', opts, &csv_proc)
|
|
38
|
+
nil
|
|
39
|
+
else
|
|
40
|
+
CSV.generate(opts, &csv_proc)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
module_function :csv
|
|
44
|
+
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require File.join(File.dirname(File.expand_path(__FILE__)), '../lib/enum_csv')
|
|
2
|
+
|
|
3
|
+
describe "EnumCSV.csv" do
|
|
4
|
+
TEST_FILE = File.join(File.dirname(File.expand_path(__FILE__)), 'test.csv')
|
|
5
|
+
after(:all) do
|
|
6
|
+
File.delete(TEST_FILE)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "should return string of CSV data for enumerable" do
|
|
10
|
+
EnumCSV.csv([[1, 2]]).should == "1,2\n"
|
|
11
|
+
EnumCSV.csv([[1, 2], [3, 4]]).should == "1,2\n3,4\n"
|
|
12
|
+
EnumCSV.csv([[1, '2,3'], [3, 4]]).should == "1,\"2,3\"\n3,4\n"
|
|
13
|
+
EnumCSV.csv([[1, nil], [3, 4]]).should == "1,\n3,4\n"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "should support :headers option for a headers string" do
|
|
17
|
+
EnumCSV.csv([[1, 2]], :headers=>['a', 'b']).should == "a,b\n1,2\n"
|
|
18
|
+
EnumCSV.csv([[1, 2], [3, 4]], :headers=>['a', 'b']).should == "a,b\n1,2\n3,4\n"
|
|
19
|
+
EnumCSV.csv([[1, '2,3'], [3, 4]], :headers=>['a,b', 'c']).should == "\"a,b\",c\n1,\"2,3\"\n3,4\n"
|
|
20
|
+
EnumCSV.csv([[1, nil], [3, 4]], :headers=>['a', nil]).should == "a,\n1,\n3,4\n"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "should support :file option for writing to a file" do
|
|
24
|
+
EnumCSV.csv([[1, 2]], :file=>TEST_FILE).should be_nil
|
|
25
|
+
File.read(TEST_FILE).should == "1,2\n"
|
|
26
|
+
EnumCSV.csv([[1, 2]], :file=>TEST_FILE, :headers=>['a', 'b'])
|
|
27
|
+
File.read(TEST_FILE).should == "a,b\n1,2\n"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "should support other csv options" do
|
|
31
|
+
EnumCSV.csv([[1, 2]], :row_sep=>'|').should == "1,2|"
|
|
32
|
+
EnumCSV.csv([[1, 2], [3, 4]], :col_sep=>'^').should == "1^2\n3^4\n"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "should yield elements to the block if given" do
|
|
36
|
+
EnumCSV.csv([[1, 2]]){|l| l.map{|i| i*2}}.should == "2,4\n"
|
|
37
|
+
EnumCSV.csv([[1, 2]], :headers=>['a', 'b']){|l| l.map{|i| i*2}}.should == "a,b\n2,4\n"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "should be callable as a method if including the module" do
|
|
41
|
+
Class.new{include EnumCSV}.new.send(:csv, [[1, 2]]).should == "1,2\n"
|
|
42
|
+
end
|
|
43
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: enum_csv
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jeremy Evans
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-12-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: |
|
|
14
|
+
EnumCSV exposes a single method, csv, for easily creating CSV
|
|
15
|
+
output/files from enumerable objects.
|
|
16
|
+
email: code@jeremyevans.net
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files:
|
|
20
|
+
- README.rdoc
|
|
21
|
+
- CHANGELOG
|
|
22
|
+
- MIT-LICENSE
|
|
23
|
+
files:
|
|
24
|
+
- MIT-LICENSE
|
|
25
|
+
- CHANGELOG
|
|
26
|
+
- README.rdoc
|
|
27
|
+
- Rakefile
|
|
28
|
+
- spec/enum_csv_spec.rb
|
|
29
|
+
- lib/enum_csv.rb
|
|
30
|
+
homepage: http://gihub.com/jeremyevans/enum_csv
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata: {}
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options:
|
|
36
|
+
- --quiet
|
|
37
|
+
- --line-numbers
|
|
38
|
+
- --inline-source
|
|
39
|
+
- --title
|
|
40
|
+
- 'EnumCSV: Create CSV from Enumerables'
|
|
41
|
+
- --main
|
|
42
|
+
- README.rdoc
|
|
43
|
+
require_paths:
|
|
44
|
+
- lib
|
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
46
|
+
requirements:
|
|
47
|
+
- - '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - '>='
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
requirements: []
|
|
56
|
+
rubyforge_project:
|
|
57
|
+
rubygems_version: 2.0.14
|
|
58
|
+
signing_key:
|
|
59
|
+
specification_version: 4
|
|
60
|
+
summary: Create CSV from Enumerables
|
|
61
|
+
test_files: []
|