csv_export 0.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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +35 -0
- data/Rakefile +9 -0
- data/csv_export.gemspec +25 -0
- data/lib/csv_export.rb +13 -0
- data/lib/csv_export/base.rb +39 -0
- data/lib/csv_export/version.rb +3 -0
- data/test/csv_export_test.rb +91 -0
- metadata +158 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Sergey Pchelincev
|
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/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# CsvExport
|
2
|
+
|
3
|
+
Painless csv export in Ruby on Rails
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'csv_export'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install csv_export
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
That gem provide `export_to_csv(filename, content, [options = {}])` method in your controller.
|
22
|
+
|
23
|
+
- *filename* - String, which define output file's name. NOTE: method doesn't add ".csv" extention automaticly
|
24
|
+
- *content* - Items' enumerable. By default expected that item is some kind of array. If item responds to "to_csv_row", gem will use it. Also if item isn't array and doesn't have "to_csv_row" it will wrap item in to the array
|
25
|
+
- *options* - Optional argument. Expects it will be some kind of hash. Processed keys:
|
26
|
+
- :separator - sets custom separator(by default is ",")
|
27
|
+
- :headers - sets header row. Expected array with columns' names
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/csv_export.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/csv_export/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Sergey Pchelincev"]
|
6
|
+
gem.email = ["jalkoby91@gmail.com"]
|
7
|
+
gem.description = %q{Painless csv export in Ruby on Rails}
|
8
|
+
gem.summary = %q{}
|
9
|
+
gem.homepage = "https://github.com/jalkoby/csv_export"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "csv_export"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = CsvExport::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'activesupport'
|
19
|
+
gem.add_dependency 'actionpack'
|
20
|
+
gem.add_dependency 'fastercsv', '1.5.5'
|
21
|
+
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
gem.add_development_dependency 'minitest'
|
24
|
+
gem.add_development_dependency 'debugger' if RUBY_VERSION =~ /1.9/
|
25
|
+
end
|
data/lib/csv_export.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
module CsvExport
|
2
|
+
if RUBY_VERSION =~ /1.8/
|
3
|
+
require 'fastercsv'
|
4
|
+
CSV = FasterCSV
|
5
|
+
else
|
6
|
+
require 'csv'
|
7
|
+
end
|
8
|
+
|
9
|
+
module Base
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def export_to_csv(filename, content, options = {})
|
14
|
+
csv_options = {}
|
15
|
+
csv_options[:col_sep] = options[:separator] || ','
|
16
|
+
headers = options[:headers]
|
17
|
+
csv_options[:headers] = headers.present?
|
18
|
+
|
19
|
+
data = CSV.generate(csv_options) do |csv_data|
|
20
|
+
csv_data << headers if csv_options[:headers]
|
21
|
+
|
22
|
+
content.each do |item|
|
23
|
+
case
|
24
|
+
when item.respond_to?(:to_csv_row)
|
25
|
+
csv_data << item.to_csv_row
|
26
|
+
when item.respond_to?(:to_a)
|
27
|
+
csv_data << item.to_a
|
28
|
+
else
|
29
|
+
csv_data << [item]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
send_data(data, :filename => filename,:type => 'text/csv')
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
if RUBY_VERSION =~ /1.8/
|
2
|
+
require 'test/unit'
|
3
|
+
end
|
4
|
+
|
5
|
+
require 'minitest/autorun'
|
6
|
+
require 'minitest/mock'
|
7
|
+
require 'action_controller'
|
8
|
+
require 'action_dispatch'
|
9
|
+
require 'csv_export'
|
10
|
+
|
11
|
+
SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
|
12
|
+
SharedTestRoutes.draw do
|
13
|
+
get ':controller(/:action)'
|
14
|
+
end
|
15
|
+
|
16
|
+
class CsvExportTest < ActionController::TestCase
|
17
|
+
include ActionDispatch::TestProcess
|
18
|
+
|
19
|
+
class TestController < ActionController::Base
|
20
|
+
include SharedTestRoutes.url_helpers
|
21
|
+
include SharedTestRoutes.mounted_helpers
|
22
|
+
|
23
|
+
def export_base
|
24
|
+
content = 5.times.map { |i| 3.times.map { |j| i * j } }
|
25
|
+
export_to_csv 'test.csv', content
|
26
|
+
end
|
27
|
+
|
28
|
+
def export_csv_rows
|
29
|
+
content = 2.times.map do
|
30
|
+
item = MiniTest::Mock.new
|
31
|
+
item.expect :to_csv_row, [1, 2, 3]
|
32
|
+
item
|
33
|
+
end
|
34
|
+
export_to_csv 'test.csv', content
|
35
|
+
end
|
36
|
+
|
37
|
+
def export_with_options
|
38
|
+
content = [[ 1, 2, 3]]
|
39
|
+
export_to_csv 'foo', content, :headers => ['One', 'Two', 'Three'], :separator => ";"
|
40
|
+
end
|
41
|
+
|
42
|
+
def export_slim_array
|
43
|
+
content = [2, 3]
|
44
|
+
export_to_csv 'slim', content
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
tests TestController
|
50
|
+
|
51
|
+
setup do
|
52
|
+
@routes = SharedTestRoutes
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_header_response
|
56
|
+
get :export_base
|
57
|
+
|
58
|
+
assert_equal 200, @response.status
|
59
|
+
assert_equal 'text/csv', @response.content_type
|
60
|
+
assert @response.headers['Content-Disposition'].include?('test.csv')
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_base_content
|
64
|
+
get :export_base
|
65
|
+
|
66
|
+
assert_equal 5, @response.body.lines.count
|
67
|
+
assert_equal "0,0,0\n", @response.body.lines.to_a[0]
|
68
|
+
assert_equal "0,4,8\n", @response.body.lines.to_a[-1]
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_using_csv_rows
|
72
|
+
get :export_csv_rows
|
73
|
+
|
74
|
+
assert_equal "1,2,3\n", @response.body.lines.to_a[0]
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_using_options
|
78
|
+
get :export_with_options
|
79
|
+
|
80
|
+
assert_equal "One;Two;Three\n", @response.body.lines.to_a[0]
|
81
|
+
assert_equal "1;2;3\n", @response.body.lines.to_a[1]
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_slim_array_case
|
85
|
+
get :export_slim_array
|
86
|
+
|
87
|
+
assert_equal "2\n", @response.body.lines.to_a[0]
|
88
|
+
assert_equal "3\n", @response.body.lines.to_a[1]
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_export
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sergey Pchelincev
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: actionpack
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: fastercsv
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 1.5.5
|
54
|
+
type: :runtime
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.5.5
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rake
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: minitest
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
81
|
+
none: false
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
type: :development
|
87
|
+
prerelease: false
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ! '>='
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: debugger
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
description: Painless csv export in Ruby on Rails
|
111
|
+
email:
|
112
|
+
- jalkoby91@gmail.com
|
113
|
+
executables: []
|
114
|
+
extensions: []
|
115
|
+
extra_rdoc_files: []
|
116
|
+
files:
|
117
|
+
- .gitignore
|
118
|
+
- Gemfile
|
119
|
+
- LICENSE
|
120
|
+
- README.md
|
121
|
+
- Rakefile
|
122
|
+
- csv_export.gemspec
|
123
|
+
- lib/csv_export.rb
|
124
|
+
- lib/csv_export/base.rb
|
125
|
+
- lib/csv_export/version.rb
|
126
|
+
- test/csv_export_test.rb
|
127
|
+
homepage: https://github.com/jalkoby/csv_export
|
128
|
+
licenses: []
|
129
|
+
post_install_message:
|
130
|
+
rdoc_options: []
|
131
|
+
require_paths:
|
132
|
+
- lib
|
133
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ! '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
segments:
|
140
|
+
- 0
|
141
|
+
hash: 3626536889106548116
|
142
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
|
+
none: false
|
144
|
+
requirements:
|
145
|
+
- - ! '>='
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
segments:
|
149
|
+
- 0
|
150
|
+
hash: 3626536889106548116
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 1.8.24
|
154
|
+
signing_key:
|
155
|
+
specification_version: 3
|
156
|
+
summary: ''
|
157
|
+
test_files:
|
158
|
+
- test/csv_export_test.rb
|