Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2 from troy/fastercsv-options

fastercsv options;
run tests with rake;
  • Loading branch information...
commit 07c2238d4e7c08c41f5e7a606430664ce6f03e58 2 parents 5edab5a + 79d1bb3
@arydjmal authored
View
14 Rakefile
@@ -0,0 +1,14 @@
+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
4 lib/to_csv.rb
@@ -1,5 +1,5 @@
class Array
- def to_csv(options = {})
+ def to_csv(options = {}, fastercsv_options = {})
return '' if self.empty?
klass = self.first.class
@@ -15,7 +15,7 @@ def to_csv(options = {})
return '' if columns.empty?
- output = FasterCSV.generate do |csv|
+ output = FasterCSV.generate(fastercsv_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
1  test/test_helper.rb
@@ -3,6 +3,7 @@
require 'test/unit'
require 'fastercsv'
require 'active_support'
+ require 'active_support/inflector'
require File.dirname(__FILE__) + '/../lib/to_csv'
rescue LoadError
puts 'to_csv tests rely on fastercsv, and active_support'
View
4 test/to_csv_test.rb
@@ -12,6 +12,10 @@ def test_with_empty_array
assert_equal( "", [].to_csv )
end
+ def test_with_fastercsv_option
+ assert_equal( "Age\tId\tName\n25\t1\tAry\n22\t2\tNati\n", @users.to_csv({}, :col_sep => "\t"))
+ end
+
def test_with_no_options
assert_equal( "Age,Id,Name\n25,1,Ary\n22,2,Nati\n", @users.to_csv )
end
Please sign in to comment.
Something went wrong with that request. Please try again.