to-csv 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +15 -9
- data/MIT-LICENSE +20 -20
- data/README.rdoc +212 -191
- data/Rakefile +52 -52
- data/lib/to_csv.rb +63 -59
- data/lib/to_csv/csv_converter.rb +174 -175
- data/test/database.yml +3 -3
- data/test/fixtures/movie.rb +6 -1
- data/test/fixtures/movies.yml +18 -18
- data/test/fixtures/people.yml +6 -6
- data/test/fixtures/person.rb +2 -2
- data/test/fixtures/schema.rb +11 -11
- data/test/lib/activerecord_test_case.rb +19 -19
- data/test/lib/activerecord_test_connector.rb +31 -31
- data/test/lib/load_fixtures.rb +8 -8
- data/test/locales/en-US.yml +27 -27
- data/test/locales/pt-BR.yml +147 -147
- data/test/tasks.rake +7 -7
- data/test/to_csv_test.rb +163 -150
- metadata +10 -10
data/CHANGELOG.rdoc
CHANGED
@@ -1,9 +1,15 @@
|
|
1
|
-
== 1.0.
|
2
|
-
|
3
|
-
* Now
|
4
|
-
*
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
== 1.0.2 2010-01-04
|
2
|
+
|
3
|
+
* Changed use of OpenStruct in favor of Struct, which supports any attribute name. Now a block yields a Struct and the object itself
|
4
|
+
* Support for Rails scopes
|
5
|
+
* Improved documentation
|
6
|
+
|
7
|
+
== 1.0.1 2009-12-24
|
8
|
+
|
9
|
+
* Now compatible with Ruby 1.9.1
|
10
|
+
* Updated documentation to reflect Ruby 1.9.1 support
|
11
|
+
|
12
|
+
== 1.0.0 2009-12-23
|
13
|
+
|
14
|
+
* First release
|
15
|
+
|
data/MIT-LICENSE
CHANGED
@@ -1,20 +1,20 @@
|
|
1
|
-
Copyright (c) 2009 Ícaro Leopoldino da Motta
|
2
|
-
|
3
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
-
a copy of this software and associated documentation files (the
|
5
|
-
"Software"), to deal in the Software without restriction, including
|
6
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
-
permit persons to whom the Software is furnished to do so, subject to
|
9
|
-
the following conditions:
|
10
|
-
|
11
|
-
The above copyright notice and this permission notice shall be
|
12
|
-
included in all copies or substantial portions of the Software.
|
13
|
-
|
14
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1
|
+
Copyright (c) 2009 Ícaro Leopoldino da Motta
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
CHANGED
@@ -1,191 +1,212 @@
|
|
1
|
-
= ToCSV
|
2
|
-
|
3
|
-
|
4
|
-
ToCSV is a gem for converting arrays to CSV by calling +to_csv+.
|
5
|
-
These arrays can contain different data structures, as long as they are homogeneous, like the ones
|
6
|
-
described below:
|
7
|
-
|
8
|
-
* A simple array of anything that responds to <tt>to_s</tt>: <tt>['Date', Time.now].to_csv</tt>
|
9
|
-
* An array of hashes: <tt>[ {'Name' => 'Icaro', 'Age' => 23}, {'Name' => 'Gabriel', 'Age' => 16} ].to_csv</tt>
|
10
|
-
* A matrix: <tt>[['Name', 'Age'], ['Icaro', 23], ['Gabriel', 16]].to_csv</tt>
|
11
|
-
* A hash like array: <tt>[ [['Name', 'Icaro'], ['Age', 23]], [['Name', 'Gabriel'], ['Age', 16]] ].to_csv</tt>
|
12
|
-
* An array of ActiveRecord objects: <tt>@users.to_csv(:except => [:password, :phone], :timestamps => true)</tt>
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
ToCSV.
|
39
|
-
ToCSV.
|
40
|
-
ToCSV.
|
41
|
-
ToCSV.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
['
|
56
|
-
['
|
57
|
-
['
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
{ 'Name' => '
|
67
|
-
{ 'Name' => '
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
[ ['Name', '
|
79
|
-
[ ['Name', '
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
users
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
#
|
99
|
-
#
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
... | ... | ..
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
<
|
125
|
-
|
126
|
-
|
127
|
-
* If you pass
|
128
|
-
*
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
'
|
146
|
-
'
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
#
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
1
|
+
= ToCSV
|
2
|
+
|
3
|
+
|
4
|
+
ToCSV is a gem for converting arrays or scopes (Rails) to CSV by calling +to_csv+.
|
5
|
+
These arrays can contain different data structures, as long as they are homogeneous, like the ones
|
6
|
+
described below:
|
7
|
+
|
8
|
+
* A simple array of anything that responds to <tt>to_s</tt>: <tt>['Date', Time.now].to_csv</tt>
|
9
|
+
* An array of hashes: <tt>[ {'Name' => 'Icaro', 'Age' => 23}, {'Name' => 'Gabriel', 'Age' => 16} ].to_csv</tt>
|
10
|
+
* A matrix: <tt>[['Name', 'Age'], ['Icaro', 23], ['Gabriel', 16]].to_csv</tt>
|
11
|
+
* A hash like array: <tt>[ [['Name', 'Icaro'], ['Age', 23]], [['Name', 'Gabriel'], ['Age', 16]] ].to_csv</tt>
|
12
|
+
* An array of ActiveRecord objects: <tt>@users.to_csv(:except => [:password, :phone], :timestamps => true)</tt>
|
13
|
+
* Scopes in Rails
|
14
|
+
|
15
|
+
|
16
|
+
=== Requirements
|
17
|
+
|
18
|
+
ToCSV has been tested with Ruby 1.8.6/1.8.7/1.9.1.
|
19
|
+
|
20
|
+
If you are using Ruby 1.9 the only dependency to use the basic features is +ActiveSupport+.
|
21
|
+
Otherwise you will need +fastercsv+. You will receive a warning if you don't meet the requirements.
|
22
|
+
|
23
|
+
# If you don't have Rails installed...
|
24
|
+
$ sudo gem install activesupport
|
25
|
+
|
26
|
+
# And if your Ruby version is lower than 1.9
|
27
|
+
$ sudo gem install fastercsv
|
28
|
+
|
29
|
+
|
30
|
+
=== Configuration
|
31
|
+
|
32
|
+
If you want to use this gem with Rails, put the following requirement in your environment.rb:
|
33
|
+
|
34
|
+
config.gem 'to-csv', :lib => 'to_csv', :source => 'http://gemcutter.org'
|
35
|
+
|
36
|
+
After that, if you need to globally configure the gem, just create a <i>to_csv.rb</i> file in <i>initializers</i>.
|
37
|
+
|
38
|
+
ToCSV.byte_order_marker = true
|
39
|
+
ToCSV.timestamps = true
|
40
|
+
ToCSV.locale = 'en-US'
|
41
|
+
ToCSV.primary_key = false
|
42
|
+
ToCSV.csv_options = { :col_sep => ',', :row_sep => "\r\n" }
|
43
|
+
|
44
|
+
|
45
|
+
== Examples
|
46
|
+
|
47
|
+
Let's start with the most simple example.
|
48
|
+
|
49
|
+
['Alfred Hitchcock', 'Robert Mitchum', 'Lucille Ball'].to_csv
|
50
|
+
#=> "Alfred Hitchcock;Robert Mitchum;Lucille Ball\n"
|
51
|
+
|
52
|
+
|
53
|
+
Or, if we have an array of arrays (i.e. a matrix) we can create tabular data.
|
54
|
+
[
|
55
|
+
['Name', 'Gender'],
|
56
|
+
['Alfred', 'M'],
|
57
|
+
['Robert', 'M'],
|
58
|
+
['Lucille', 'F']
|
59
|
+
].to_csv #=> "Name;Gender\nAlfred;M\nRobert;M\nLucille;F\n"
|
60
|
+
|
61
|
+
|
62
|
+
Almost always, when we generate CSV files, we want it to have appropriate
|
63
|
+
headers, so a better approach might be to use an array of hashes.
|
64
|
+
|
65
|
+
[
|
66
|
+
{ 'Name' => 'Alfred', 'Gender' => 'M' },
|
67
|
+
{ 'Name' => 'Robert', 'Gender' => 'M' },
|
68
|
+
{ 'Name' => 'Lucille', 'Gender' => 'F' }
|
69
|
+
].to_csv #=> "Gender;Name\nM;Alfred\nM;Robert\nF;Lucille\n"
|
70
|
+
|
71
|
+
|
72
|
+
Look carefully to the above output. You can see that when we use hashes we can
|
73
|
+
no longer be sure of the headers' order (true for Ruby versions older than 1.9).
|
74
|
+
When we are working with tabular data the headers' order can be very important,
|
75
|
+
thus we can use a somewhat similar data structure:
|
76
|
+
|
77
|
+
[
|
78
|
+
[ ['Name', 'Alfred'], ['Gender', 'M'] ],
|
79
|
+
[ ['Name', 'Robert'], ['Gender', 'M'] ],
|
80
|
+
[ ['Name', 'Lucille'], ['Gender', 'F'] ]
|
81
|
+
].to_csv #=> "Name;Gender\nAlfred;M\nRobert;M\nLucille;F\n"
|
82
|
+
|
83
|
+
That's a lot to type... The first example was much simpler...
|
84
|
+
|
85
|
+
There is the <tt>headers</tt> option. You can use it in all the examples above
|
86
|
+
to enable/disable headers from the output. Default is to show (true).
|
87
|
+
|
88
|
+
users = [{ 'Name' => 'Alfred', 'Gender' => 'M' }]
|
89
|
+
users.to_csv(:headers => false)
|
90
|
+
|
91
|
+
|
92
|
+
==== Active Record Objects
|
93
|
+
|
94
|
+
When we're building our data like the previous examples we have very few options
|
95
|
+
compared to what can be passed when converting an array of AR objects. Again,
|
96
|
+
the easiest way:
|
97
|
+
|
98
|
+
# Anywhere in your app.
|
99
|
+
# By default, all available model attributes (DB columns) are going to be used
|
100
|
+
# except timestamps and the primary key of the record
|
101
|
+
@users = User.all
|
102
|
+
File.open('path/to/file.csv', 'w') { |io| io.puts @users.to_csv }
|
103
|
+
|
104
|
+
|
105
|
+
==== Headers
|
106
|
+
|
107
|
+
You can control the order and the text of any header. You can accomplish that
|
108
|
+
in various ways.
|
109
|
+
|
110
|
+
By default all attribute/method names will be sorted in alphabetical order. So
|
111
|
+
imagine a person model have +name+, +age+ and +email+ as attributes, and you
|
112
|
+
want to get the following output:
|
113
|
+
|
114
|
+
Name | E-mail | Age
|
115
|
+
... | ... | ..
|
116
|
+
... | ... | ..
|
117
|
+
|
118
|
+
You can tell <i>to-csv</i> to use a specific locale. If you don't, it uses
|
119
|
+
your app current locale. It will try to translate attributes to a
|
120
|
+
more friendly text by using the scope <tt>[:activerecord, :attributes, <model name>]</tt>.
|
121
|
+
If the translation doesn't exist the header's text is going to be humanized.
|
122
|
+
|
123
|
+
The order of columns can be changed with the option +headers+. The way this
|
124
|
+
option works is very similar to the <tt>plugins</tt> method in your Rails
|
125
|
+
<i>environment.rb</i> file.
|
126
|
+
|
127
|
+
* If you pass +nil+ (default) then headers/columns will be in alphabetical order.
|
128
|
+
* If you pass an empty array or +false+, no headers will be shown.
|
129
|
+
* Instead, if you pass a non empty array, headers will be sorted in the order specified. <tt>:all</tt> can be used as a placeholder for all attributes not explicitly named.
|
130
|
+
|
131
|
+
So, in our example above, we can say:
|
132
|
+
|
133
|
+
@users.to_csv(:headers => [:name, :email, :age])
|
134
|
+
|
135
|
+
Or, using the placeholder +all+, which is not very useful here:
|
136
|
+
|
137
|
+
@users.to_csv(:headers => [:name, :email, :all])
|
138
|
+
|
139
|
+
If you want a completely different result you could, for instance, map all
|
140
|
+
users to a hash. Example:
|
141
|
+
|
142
|
+
# This makes use of a hash to completely change the CSV output.
|
143
|
+
@users.map do |user|
|
144
|
+
{
|
145
|
+
'Name' => user.name,
|
146
|
+
'Age' => user.age,
|
147
|
+
'Total Comments' => user.comments.count
|
148
|
+
}
|
149
|
+
end.to_csv
|
150
|
+
|
151
|
+
|
152
|
+
===== Passing a Block
|
153
|
+
|
154
|
+
Sometimes you may want to change just one value out of six for example. The best
|
155
|
+
way to go is to pass a block so that you don't have to repeat yourself writing
|
156
|
+
five headers and it's obvious values and also loosing I18n header translations.
|
157
|
+
|
158
|
+
# The block yields a new Struct for each object in the list. By calling
|
159
|
+
# methods on this struct you can change their default values.
|
160
|
+
@users.to_csv do |row, user|
|
161
|
+
row.date_of_birth = user.date_of_birth.to_s(:long)
|
162
|
+
end
|
163
|
+
|
164
|
+
|
165
|
+
===== A More Complete Example
|
166
|
+
|
167
|
+
# users/index.html.haml
|
168
|
+
= link_to 'export (CSV)', users_url(:csv)
|
169
|
+
|
170
|
+
# UsersController#index
|
171
|
+
class UsersController < ApplicationController
|
172
|
+
def index
|
173
|
+
@users = User.most_active
|
174
|
+
respond_to do |format|
|
175
|
+
format.html
|
176
|
+
format.csv do
|
177
|
+
send_data User.csv(@users), :filename => 'users_report.csv'
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# User model
|
184
|
+
class User < ActiveRecord::Base
|
185
|
+
def self.csv(users)
|
186
|
+
users.csv(:headers => [:id, :all], :primary_key => true, :except => :password) do |row, user|
|
187
|
+
row.id = "%04d" % user.id
|
188
|
+
row.created_at = I18n.l(user.created_at, :format => :default)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# locales/en-US.yml
|
194
|
+
activerecord:
|
195
|
+
attributes:
|
196
|
+
user:
|
197
|
+
id: Code
|
198
|
+
|
199
|
+
|
200
|
+
==== Full Customization
|
201
|
+
|
202
|
+
You can always customize the output if you wish by building arrays of hashes,
|
203
|
+
arrays of arrays of bidimensional arrays etc :). Or you can obviously mix
|
204
|
+
anything you want and even use FasterCSV directly.
|
205
|
+
|
206
|
+
@user.to_csv { :only => [:name, :email] }, :col_sep => ','
|
207
|
+
|
208
|
+
There are other options for you to customize the output. Take a look at the
|
209
|
+
<tt>to_csv</tt> method documentation.
|
210
|
+
|
211
|
+
Copyright (c) 2009 Ícaro Leopoldino da Motta, released under the MIT license.
|
212
|
+
|