Skip to content
Browse files

don't rely on the fastercsv gem when ruby >= 1.9

  • Loading branch information...
1 parent 07c2238 commit 9dc83a3b53f13f6f88bb9b0d1c898104ba1090f6 @arydjmal committed Aug 19, 2011
Showing with 24 additions and 21 deletions.
  1. +1 −1 MIT-LICENSE
  2. +6 −11 README.rdoc
  3. +0 −3 Rakefile
  4. +6 −1 init.rb
  5. +4 −2 lib/to_csv.rb
  6. +6 −2 test/test_helper.rb
  7. +1 −1 test/to_csv_test.rb
View
2 MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2008-2010 Ary Djmal
+Copyright (c) 2008-2011 Ary Djmal
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
View
17 README.rdoc
@@ -12,18 +12,18 @@ This simple plugin gives you the ability to call to_csv to a collection of activ
#
@users.to_csv
- @users.to_csv(:only => [:last_name, :role])
+ @users.to_csv(:only => [:last_name, :role]) # This will also set the order
@users.to_csv(:headers => false)
@users.to_csv(:except => [:last_name, :role])
@users.to_csv(:methods => :admin?)
+ @users.to_csv({:methods => :admin?}, {:col_sep => '\t'}) # Send csv generation options
== Real life example
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)
class UserController < ApplicationController
-
def index
@users = User.all
@@ -33,19 +33,14 @@ In the controller where you want to export to csv, add the format.csv line (as o
format.csv { send_data @users.to_csv }
end
end
-
- def show...
- def new...
- def edit...
- def create...
- def update...
- def destroy...
-
end
== Dependencies
+ None if using ruby >= 1.9
+
+ Else, install fastercsv:
sudo gem install fastercsv
@@ -64,4 +59,4 @@ I got ideas and influence from Mike Clarks recipe #35 in Rails Recipes book, som
Does not work on a single activerecord, ie, User.first.to_csv.
-Copyright (c) 2008-2010 Ary Djmal, released under the MIT license
+Copyright (c) 2008-2011 Ary Djmal, released under the MIT license
View
3 Rakefile
@@ -1,14 +1,11 @@
require 'rake'
require 'rake/testtask'
-require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
desc 'Test the to_csv plugin.'
Rake::TestTask.new(:test) do |t|
- t.libs << 'lib'
- t.libs << 'test'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
View
7 init.rb
@@ -1,2 +1,7 @@
-require 'fastercsv'
+if RUBY_VERSION >= "1.9.0"
+ require 'csv'
+else
+ require 'fastercsv'
+end
+
require 'to_csv'
View
6 lib/to_csv.rb
@@ -1,5 +1,5 @@
class Array
- def to_csv(options = {}, fastercsv_options = {})
+ def to_csv(options = {}, csv_options = {})
return '' if self.empty?
klass = self.first.class
@@ -15,7 +15,9 @@ def to_csv(options = {}, fastercsv_options = {})
return '' if columns.empty?
- output = FasterCSV.generate(fastercsv_options) do |csv|
+ writer = RUBY_VERSION >= "1.9.0" ? CSV : FasterCSV
+
+ output = writer.generate(csv_options) do |csv|
csv << columns.map { |column| klass.human_attribute_name(column) } unless options[:headers] == false
self.each do |item|
csv << columns.collect { |column| item.send(column) }
View
8 test/test_helper.rb
@@ -1,12 +1,16 @@
begin
require 'rubygems'
require 'test/unit'
- require 'fastercsv'
require 'active_support'
require 'active_support/inflector'
+ if RUBY_VERSION >= "1.9.0"
+ require 'csv'
+ else
+ require 'fastercsv'
+ end
require File.dirname(__FILE__) + '/../lib/to_csv'
rescue LoadError
- puts 'to_csv tests rely on fastercsv, and active_support'
+ puts 'to_csv tests rely on active_support, and fastercsv if ruby version < 1.9'
end
class User
View
2 test/to_csv_test.rb
@@ -12,7 +12,7 @@ def test_with_empty_array
assert_equal( "", [].to_csv )
end
- def test_with_fastercsv_option
+ def test_with_csv_options
assert_equal( "Age\tId\tName\n25\t1\tAry\n22\t2\tNati\n", @users.to_csv({}, :col_sep => "\t"))
end

0 comments on commit 9dc83a3

Please sign in to comment.
Something went wrong with that request. Please try again.