tabularasa 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Manifest +8 -0
- data/README.rdoc +62 -0
- data/Rakefile +24 -0
- data/init.rb +7 -0
- data/lib/to_csv.rb +29 -0
- data/tabularasa.gemspec +30 -0
- data/test/test_helper.rb +37 -0
- data/test/to_csv_test.rb +50 -0
- metadata +64 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008-2011 Ary Djmal
|
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/Manifest
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= to_csv plugin
|
2
|
+
|
3
|
+
This simple plugin gives you the ability to call to_csv to a collection of activerecords. The builder options are the same as to_json / to_xml, except for the :include.
|
4
|
+
|
5
|
+
|
6
|
+
== Usage
|
7
|
+
|
8
|
+
@users = User.all
|
9
|
+
|
10
|
+
#
|
11
|
+
# defaults are export headers and all fields
|
12
|
+
#
|
13
|
+
|
14
|
+
@users.to_csv
|
15
|
+
@users.to_csv(:only => [:last_name, :role]) # This will also set the order
|
16
|
+
@users.to_csv(:headers => false)
|
17
|
+
@users.to_csv(:except => [:last_name, :role])
|
18
|
+
@users.to_csv(:methods => :admin?)
|
19
|
+
@users.to_csv({:methods => :admin?}, {:col_sep => '\t'}) # Send csv generation options
|
20
|
+
|
21
|
+
|
22
|
+
== Real life example
|
23
|
+
|
24
|
+
In the controller where you want to export to csv, add the format.csv line (as of rails 2.1 it is not necessary to register the csv myme_type)
|
25
|
+
|
26
|
+
class UserController < ApplicationController
|
27
|
+
def index
|
28
|
+
@users = User.all
|
29
|
+
|
30
|
+
respond_to do |format|
|
31
|
+
format.html
|
32
|
+
format.xml { render :xml => @users }
|
33
|
+
format.csv { send_data @users.to_csv }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
== Dependencies
|
40
|
+
|
41
|
+
None if using ruby >= 1.9
|
42
|
+
|
43
|
+
Else, install fastercsv:
|
44
|
+
sudo gem install fastercsv
|
45
|
+
|
46
|
+
|
47
|
+
== Install
|
48
|
+
|
49
|
+
./script/plugin install git://github.com/arydjmal/to_csv.git
|
50
|
+
|
51
|
+
|
52
|
+
== Ideas
|
53
|
+
|
54
|
+
I got ideas and influence from Mike Clarks recipe #35 in Rails Recipes book, some anonymous pastie, and whoever wrote to_xml/to_json builders.
|
55
|
+
|
56
|
+
|
57
|
+
== Note
|
58
|
+
|
59
|
+
Does not work on a single activerecord, ie, User.first.to_csv.
|
60
|
+
|
61
|
+
|
62
|
+
Copyright (c) 2008-2011 Ary Djmal, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'echoe'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
desc 'Test the to_csv plugin.'
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
Echoe.new('tabularasa', '0.1.1') do |p|
|
16
|
+
p.description = "This Rails gem gives you the ability to call to_csv to a collection of activerecords"
|
17
|
+
p.url = "https://github.com/arydjmal/to_csv"
|
18
|
+
p.author = "Ary Djmal"
|
19
|
+
p.email = "arydjmal@gmail.com"
|
20
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
21
|
+
p.development_dependencies = []
|
22
|
+
end
|
23
|
+
|
24
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
data/lib/to_csv.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
class Array
|
2
|
+
def to_csv(options = {}, csv_options = {})
|
3
|
+
return '' if self.empty?
|
4
|
+
|
5
|
+
klass = self.first.class
|
6
|
+
attributes = self.first.attributes.keys.sort.map(&:to_sym)
|
7
|
+
|
8
|
+
if options[:only]
|
9
|
+
columns = Array(options[:only]) & attributes
|
10
|
+
else
|
11
|
+
columns = attributes - Array(options[:except])
|
12
|
+
end
|
13
|
+
|
14
|
+
columns += Array(options[:methods])
|
15
|
+
|
16
|
+
return '' if columns.empty?
|
17
|
+
|
18
|
+
writer = RUBY_VERSION >= "1.9.0" ? CSV : FasterCSV
|
19
|
+
|
20
|
+
output = writer.generate(csv_options) do |csv|
|
21
|
+
csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
|
22
|
+
self.each do |item|
|
23
|
+
csv << columns.collect { |column| item.send(column) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
output
|
28
|
+
end
|
29
|
+
end
|
data/tabularasa.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "tabularasa"
|
5
|
+
s.version = "0.1.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Ary Djmal"]
|
9
|
+
s.date = "2011-11-23"
|
10
|
+
s.description = "This Rails gem gives you the ability to call to_csv to a collection of activerecords"
|
11
|
+
s.email = "arydjmal@gmail.com"
|
12
|
+
s.extra_rdoc_files = ["README.rdoc", "lib/to_csv.rb"]
|
13
|
+
s.files = ["MIT-LICENSE", "Manifest", "README.rdoc", "Rakefile", "init.rb", "lib/to_csv.rb", "test/test_helper.rb", "test/to_csv_test.rb", "tabularasa.gemspec"]
|
14
|
+
s.homepage = "https://github.com/arydjmal/to_csv"
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Tabularasa", "--main", "README.rdoc"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = "tabularasa"
|
18
|
+
s.rubygems_version = "1.8.11"
|
19
|
+
s.summary = "This Rails gem gives you the ability to call to_csv to a collection of activerecords"
|
20
|
+
s.test_files = ["test/test_helper.rb", "test/to_csv_test.rb"]
|
21
|
+
|
22
|
+
if s.respond_to? :specification_version then
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
else
|
27
|
+
end
|
28
|
+
else
|
29
|
+
end
|
30
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
begin
|
2
|
+
require 'rubygems'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/inflector'
|
6
|
+
if RUBY_VERSION >= "1.9.0"
|
7
|
+
require 'csv'
|
8
|
+
else
|
9
|
+
require 'fastercsv'
|
10
|
+
end
|
11
|
+
require File.dirname(__FILE__) + '/../lib/to_csv'
|
12
|
+
rescue LoadError
|
13
|
+
puts 'to_csv tests rely on active_support, and fastercsv if ruby version < 1.9'
|
14
|
+
end
|
15
|
+
|
16
|
+
class User
|
17
|
+
COLUMNS = %w(id name age)
|
18
|
+
|
19
|
+
attr_accessor *COLUMNS
|
20
|
+
|
21
|
+
def self.human_attribute_name(attribute)
|
22
|
+
attribute.to_s.humanize
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(params={})
|
26
|
+
params.each { |key, value| self.send("#{key}=", value); }
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def attributes
|
31
|
+
COLUMNS.inject({}) { |attributes, attribute| attributes.merge(attribute => send(attribute)) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def is_old?
|
35
|
+
age > 40
|
36
|
+
end
|
37
|
+
end
|
data/test/to_csv_test.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class ToCsvTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
@users = [
|
6
|
+
User.new(:id => 1, :name => 'Ary', :age => 25),
|
7
|
+
User.new(:id => 2, :name => 'Nati', :age => 22)
|
8
|
+
]
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_with_empty_array
|
12
|
+
assert_equal( "", [].to_csv )
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_with_csv_options
|
16
|
+
assert_equal( "Age\tId\tName\n25\t1\tAry\n22\t2\tNati\n", @users.to_csv({}, :col_sep => "\t"))
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_with_no_options
|
20
|
+
assert_equal( "Age,Id,Name\n25,1,Ary\n22,2,Nati\n", @users.to_csv )
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_with_no_headers
|
24
|
+
assert_equal( "25,1,Ary\n22,2,Nati\n", @users.to_csv(:headers => false) )
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_with_only
|
28
|
+
assert_equal( "Name\nAry\nNati\n", @users.to_csv(:only => :name) )
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_with_empty_only
|
32
|
+
assert_equal( "", @users.to_csv(:only => "") )
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_with_only_and_wrong_column_names
|
36
|
+
assert_equal( "Name\nAry\nNati\n", @users.to_csv(:only => [:name, :yoyo]) )
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_with_except
|
40
|
+
assert_equal( "Age\n25\n22\n", @users.to_csv(:except => [:id, :name]) )
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_with_except_and_only_should_listen_to_only
|
44
|
+
assert_equal( "Name\nAry\nNati\n", @users.to_csv(:except => [:id, :name], :only => :name) )
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_with_except
|
48
|
+
assert_equal( "Age,Id,Name,Is old?\n25,1,Ary,false\n22,2,Nati,false\n", @users.to_csv(:methods => [:is_old?]) )
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tabularasa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ary Djmal
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-11-23 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: This Rails gem gives you the ability to call to_csv to a collection of
|
15
|
+
activerecords
|
16
|
+
email: arydjmal@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files:
|
20
|
+
- README.rdoc
|
21
|
+
- lib/to_csv.rb
|
22
|
+
files:
|
23
|
+
- MIT-LICENSE
|
24
|
+
- Manifest
|
25
|
+
- README.rdoc
|
26
|
+
- Rakefile
|
27
|
+
- init.rb
|
28
|
+
- lib/to_csv.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
- test/to_csv_test.rb
|
31
|
+
- tabularasa.gemspec
|
32
|
+
homepage: https://github.com/arydjmal/to_csv
|
33
|
+
licenses: []
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --line-numbers
|
37
|
+
- --inline-source
|
38
|
+
- --title
|
39
|
+
- Tabularasa
|
40
|
+
- --main
|
41
|
+
- README.rdoc
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.2'
|
56
|
+
requirements: []
|
57
|
+
rubyforge_project: tabularasa
|
58
|
+
rubygems_version: 1.8.11
|
59
|
+
signing_key:
|
60
|
+
specification_version: 3
|
61
|
+
summary: This Rails gem gives you the ability to call to_csv to a collection of activerecords
|
62
|
+
test_files:
|
63
|
+
- test/test_helper.rb
|
64
|
+
- test/to_csv_test.rb
|