csv_report_generator 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +8 -0
- data/README.rdoc +104 -0
- data/Rakefile +14 -0
- data/csv_report_generator.gemspec +31 -0
- data/example/example.rb +39 -0
- data/lib/csv_report_generator/csv_report_generator.rb +63 -0
- data/lib/csv_report_generator.rb +4 -0
- data/test/test_helper.rb +8 -0
- data/test/unit/csv_report_generator_test.rb +13 -0
- metadata +79 -0
data/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
= CSVReportGenerator
|
2
|
+
|
3
|
+
== Description
|
4
|
+
|
5
|
+
Allows for simple csv generation of reports where you need to transform data
|
6
|
+
|
7
|
+
== Installation
|
8
|
+
|
9
|
+
sudo gem install csv_report_generator
|
10
|
+
|
11
|
+
== Requirements
|
12
|
+
|
13
|
+
fastercsv
|
14
|
+
|
15
|
+
== Usage
|
16
|
+
|
17
|
+
headers = [{ :name => "Full Name" }, { :zip => "Zip Code" }, { :gender => "Gender" }, { :widget_count => "Widgets" }, { :comment => "Comments" }]
|
18
|
+
rows = [ { :name => 'joe blow', :zip => 12345, :gender => 0, :widget_count => 3, :comment => "This, has a comma" },
|
19
|
+
{ :name => 'jane doe', :zip => 56789, :gender => 1, :widget_count => 2, :comment => "\"This\" has quotes" },
|
20
|
+
{ :name => 'jane doe', :zip => 56789, :gender => 1, :widget_count => 2, :comment => "Hello there" },
|
21
|
+
{ :name => nil, :zip => nil, :gender => nil, :widget_count => 7, :comment => nil } ]
|
22
|
+
|
23
|
+
report = CSVReportGenerator.new(rows, headers, :includes_total => true) do |csv|
|
24
|
+
csv.row_transformer { |row|
|
25
|
+
row[:name].upcase!
|
26
|
+
row[:gender] = (row[:gender].to_i == 0 ? "Male" : "Female")
|
27
|
+
row[:comment] = row[:comment].tr "A-Za-z", "N-ZA-Mn-za-m" #rot13
|
28
|
+
row
|
29
|
+
}
|
30
|
+
|
31
|
+
csv.total_transformer { |row|
|
32
|
+
row[:name] = "TOTAL"
|
33
|
+
row
|
34
|
+
}
|
35
|
+
end
|
36
|
+
|
37
|
+
puts "with headers"
|
38
|
+
puts "============================"
|
39
|
+
puts report.as_csv
|
40
|
+
|
41
|
+
puts
|
42
|
+
puts "without headers"
|
43
|
+
puts "============================"
|
44
|
+
report.headers = nil
|
45
|
+
puts report.as_csv
|
46
|
+
|
47
|
+
report = CSVReportGenerator.new(rows, headers, :include_totals => true)
|
48
|
+
|
49
|
+
puts
|
50
|
+
puts "no transformations"
|
51
|
+
puts "============================"
|
52
|
+
puts report.as_csv
|
53
|
+
|
54
|
+
|
55
|
+
output:
|
56
|
+
|
57
|
+
with headers
|
58
|
+
============================
|
59
|
+
Full Name,Zip Code,Gender,Widgets,Comments
|
60
|
+
JOE BLOW,12345,Male,3,"Guvf, unf n pbzzn"
|
61
|
+
JANE DOE,56789,Female,2,"""Guvf"" unf dhbgrf"
|
62
|
+
JANE DOE,56789,Female,2,Uryyb gurer
|
63
|
+
TOTAL,,,7,
|
64
|
+
|
65
|
+
without headers
|
66
|
+
============================
|
67
|
+
Male,3,12345,"This, has a comma",JOE BLOW
|
68
|
+
Male,2,56789,"""This"" has quotes",JANE DOE
|
69
|
+
Male,2,56789,Hello there,JANE DOE
|
70
|
+
,7,,,TOTAL
|
71
|
+
|
72
|
+
no transformations
|
73
|
+
============================
|
74
|
+
Full Name,Zip Code,Gender,Widgets,Comments
|
75
|
+
JOE BLOW,12345,Male,3,"This, has a comma"
|
76
|
+
JANE DOE,56789,Male,2,"""This"" has quotes"
|
77
|
+
JANE DOE,56789,Male,2,Hello there
|
78
|
+
TOTAL,,,7,
|
79
|
+
|
80
|
+
|
81
|
+
== License
|
82
|
+
|
83
|
+
Copyright (c) 2010
|
84
|
+
|
85
|
+
Permission is hereby granted, free of charge, to any person
|
86
|
+
obtaining a copy of this software and associated documentation
|
87
|
+
files (the "Software"), to deal in the Software without
|
88
|
+
restriction, including without limitation the rights to use,
|
89
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
90
|
+
copies of the Software, and to permit persons to whom the
|
91
|
+
Software is furnished to do so, subject to the following
|
92
|
+
conditions:
|
93
|
+
|
94
|
+
The above copyright notice and this permission notice shall be
|
95
|
+
included in all copies or substantial portions of the Software.
|
96
|
+
|
97
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
98
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
99
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
100
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
101
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
102
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
103
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
104
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new('csv_report_generator', '0.4.0') do |p|
|
6
|
+
p.description = "Makes generating CSV report easier"
|
7
|
+
p.url = "http://github.com/ckhall/CSV-Report-Generator"
|
8
|
+
p.author = "Chris Hall"
|
9
|
+
p.email = "christopher.k.hall@gmail.com"
|
10
|
+
p.ignore_pattern = "pkg/*", "doc/*", "coverage/*"
|
11
|
+
p.development_dependencies = []
|
12
|
+
end
|
13
|
+
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{csv_report_generator}
|
5
|
+
s.version = "0.4.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Chris Hall"]
|
9
|
+
s.date = %q{2010-06-08}
|
10
|
+
s.description = %q{Makes generating CSV report easier}
|
11
|
+
s.email = %q{christopher.k.hall@gmail.com}
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/csv_report_generator.rb", "lib/csv_report_generator/csv_report_generator.rb"]
|
13
|
+
s.files = ["README.rdoc", "Rakefile", "example/example.rb", "lib/csv_report_generator.rb", "lib/csv_report_generator/csv_report_generator.rb", "test/test_helper.rb", "test/unit/csv_report_generator_test.rb", "Manifest", "csv_report_generator.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/ckhall/CSV-Report-Generator}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Csv_report_generator", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{csv_report_generator}
|
18
|
+
s.rubygems_version = %q{1.3.6}
|
19
|
+
s.summary = %q{Makes generating CSV report easier}
|
20
|
+
s.test_files = ["test/test_helper.rb", "test/unit/csv_report_generator_test.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
24
|
+
s.specification_version = 3
|
25
|
+
|
26
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
27
|
+
else
|
28
|
+
end
|
29
|
+
else
|
30
|
+
end
|
31
|
+
end
|
data/example/example.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.join(File.dirname(__FILE__), '../lib/csv_report_generator')
|
3
|
+
|
4
|
+
headers = [{ :name => "Full Name" }, { :zip => "Zip Code" }, { :gender => "Gender" }, { :widget_count => "Widgets" }, { :comment => "Comments" }]
|
5
|
+
rows = [ { :name => 'joe blow', :zip => 12345, :gender => 0, :widget_count => 3, :comment => "This, has a comma" },
|
6
|
+
{ :name => 'jane doe', :zip => 56789, :gender => 1, :widget_count => 2, :comment => "\"This\" has quotes" },
|
7
|
+
{ :name => 'jane doe', :zip => 56789, :gender => 1, :widget_count => 2, :comment => "Hello there" },
|
8
|
+
{ :name => nil, :zip => nil, :gender => nil, :widget_count => 7, :comment => nil } ]
|
9
|
+
|
10
|
+
report = CSVReportGenerator.new(rows, headers, :includes_total => true) do |csv|
|
11
|
+
csv.row_transformer { |row|
|
12
|
+
row[:name].upcase!
|
13
|
+
row[:gender] = (row[:gender].to_i == 0 ? "Male" : "Female")
|
14
|
+
row[:comment] = row[:comment].tr "A-Za-z", "N-ZA-Mn-za-m" #rot13
|
15
|
+
row
|
16
|
+
}
|
17
|
+
|
18
|
+
csv.total_transformer { |row|
|
19
|
+
row[:name] = "TOTAL"
|
20
|
+
row
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
puts "with headers"
|
25
|
+
puts "============================"
|
26
|
+
puts report.as_csv
|
27
|
+
|
28
|
+
puts
|
29
|
+
puts "without headers"
|
30
|
+
puts "============================"
|
31
|
+
report.headers = nil
|
32
|
+
puts report.as_csv
|
33
|
+
|
34
|
+
report = CSVReportGenerator.new(rows, headers, :include_totals => true)
|
35
|
+
|
36
|
+
puts
|
37
|
+
puts "no transformations"
|
38
|
+
puts "============================"
|
39
|
+
puts report.as_csv
|
@@ -0,0 +1,63 @@
|
|
1
|
+
class CSVReportGenerator
|
2
|
+
|
3
|
+
DEFAULT_ROW_TRANSFORMER = proc { |r| r }.freeze
|
4
|
+
DEFAULT_TOTAL_TRANSFORMER = proc { |r| r }.freeze
|
5
|
+
|
6
|
+
attr_writer :headers
|
7
|
+
attr_writer :rows
|
8
|
+
|
9
|
+
def row_transformer(&block)
|
10
|
+
@row_transformer = block
|
11
|
+
end
|
12
|
+
|
13
|
+
def total_transformer(&block)
|
14
|
+
@total_transformer = block
|
15
|
+
end
|
16
|
+
|
17
|
+
def transform(row, block)
|
18
|
+
block.call(row)
|
19
|
+
end
|
20
|
+
|
21
|
+
def initialize(rows, headers = nil, opts = {})
|
22
|
+
@headers = headers
|
23
|
+
@rows = rows
|
24
|
+
@includes_total = opts[:includes_total] || false
|
25
|
+
|
26
|
+
if block_given?
|
27
|
+
yield(self)
|
28
|
+
else
|
29
|
+
# setup default transformers
|
30
|
+
@row_transformer = DEFAULT_ROW_TRANSFORMER
|
31
|
+
@total_transformer = DEFAULT_TOTAL_TRANSFORMER
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def as_csv
|
36
|
+
FasterCSV.generate do |csv|
|
37
|
+
unless @headers.nil?
|
38
|
+
csv << @headers.map { |h| h.values.first }
|
39
|
+
end
|
40
|
+
|
41
|
+
# transform if passed block, otherwise leave as is
|
42
|
+
# don't mess with original row data in case we want
|
43
|
+
# to redefine
|
44
|
+
rows = @rows.collect do |row|
|
45
|
+
if(@includes_total && @rows.last == row)
|
46
|
+
self.transform(row, @total_transformer)
|
47
|
+
else
|
48
|
+
self.transform(row, @row_transformer)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# need to do it this way so headers and data are in sync
|
53
|
+
unless @headers.nil?
|
54
|
+
rows.map { |r| @headers.map { |h| h.keys.first }.map { |h| r.send(:[], h.to_sym) } }.each { |row| csv << row }
|
55
|
+
else
|
56
|
+
unless rows.empty?
|
57
|
+
cols = rows[0].keys
|
58
|
+
rows.map { |r| cols.map { |c| r.send(:[], c.to_sym) } }.each { |row| csv << row }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.expand_path(File.join(File.dirname(__FILE__), %w(.. test_helper)))
|
2
|
+
|
3
|
+
class CSVReportGeneratorTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context "An instance of the CSVReportGenerator class" do
|
6
|
+
|
7
|
+
should "flunk" do
|
8
|
+
flunk "Please provide some tests"
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_report_generator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 4
|
8
|
+
- 0
|
9
|
+
version: 0.4.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Chris Hall
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-08 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Makes generating CSV report easier
|
22
|
+
email: christopher.k.hall@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
- lib/csv_report_generator.rb
|
30
|
+
- lib/csv_report_generator/csv_report_generator.rb
|
31
|
+
files:
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- example/example.rb
|
35
|
+
- lib/csv_report_generator.rb
|
36
|
+
- lib/csv_report_generator/csv_report_generator.rb
|
37
|
+
- test/test_helper.rb
|
38
|
+
- test/unit/csv_report_generator_test.rb
|
39
|
+
- Manifest
|
40
|
+
- csv_report_generator.gemspec
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/ckhall/CSV-Report-Generator
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --line-numbers
|
48
|
+
- --inline-source
|
49
|
+
- --title
|
50
|
+
- Csv_report_generator
|
51
|
+
- --main
|
52
|
+
- README.rdoc
|
53
|
+
require_paths:
|
54
|
+
- lib
|
55
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 2
|
69
|
+
version: "1.2"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: csv_report_generator
|
73
|
+
rubygems_version: 1.3.6
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Makes generating CSV report easier
|
77
|
+
test_files:
|
78
|
+
- test/test_helper.rb
|
79
|
+
- test/unit/csv_report_generator_test.rb
|