Taking Any Returned Record Set and Writing It In CVS Format

I would like to be able to do the following in my controller:

@job = Job.find(:all)
write_returned_recordset_to_file(@job, “filename.txt”)

And have the write_returned_recordset_to_file write the returned data
with column headers to a file. I have some idea but they are not
class independent. Any sexy suggestions?


John K.
[email protected]

http://www.kopanas.com

http://www.soen.info

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John K. wrote:

I would like to be able to do the following in my controller:

@job = Job.find(:all)
write_returned_recordset_to_file(@job, “filename.txt”)

And have the write_returned_recordset_to_file write the returned data
with column headers to a file. I have some idea but they are not
class independent. Any sexy suggestions?

How about this?

require ‘faster_csv’

module FindToCSV
def self.included( cl )
cl.instance_eval "
alias :_original_find :find
def find( *args )
results = _original_find( *args )
results.extend( ArrayExtension ) if results.is_a?( Array )
results
end
"
end

module ArrayExtension
def to_csv( filename )
csv = FasterCSV.generate do |csv|
csv << self.first.attributes.sort.map{ |arr| arr.first }
self.each do |row|
csv << self.first.attributes.sort.map{ |arr| arr.last }
end
end
File.open( filename, “w” ){ |io| io.puts csv }
end
end
end

class Job < ActiveRecord::Base
include FindToCSV
end

@jobs = Job.find( :all )
@jobs.to_csv( ‘myfile.csv’ )

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFgFR8Myx0fW1d8G0RAufzAJ4s5MIenAtu/IvKKttyExfaDtSL4QCfexu3
ayht6w29hQaoy5anazy/+z0=
=+ECB
-----END PGP SIGNATURE-----

definitely a faq item - CSV (common separated values export)

referring to this proposal:

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/
99ea153cc378f7f1/ba010c2c547d7637?lnk=gst&q=%5BFAQ%
5D&rnum=2#ba010c2c547d7637

I really like this, but this would be nice too

headers = [ “id” , “name” , …]
exclude = [ “phone_number” , “user_id” ]
@jobs.to_csv( ‘myfile.csv’ , :header=> headers , :exclude_cols=>
exclue )

so I can add headers to the csv file, and also exclude certain columns
easily

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jodi S. wrote:

definitely a faq item - CSV (common separated values export)

I am going to add this to the ActiveRecord::Extensions plugin… where
is this FAQ page that you speak?

http://www.continuousthinking.com/are

Zach

with column headers to a file. I have some idea but they are not
results = _original_find( *args )
self.each do |row|
include FindToCSV
end

@jobs = Job.find( :all )
@jobs.to_csv( ‘myfile.csv’ )

Zach

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFgFqjMyx0fW1d8G0RAulQAJ0WbwEETGthP9OvAps7Llo9CxyWGACfUYV8
OkRYyGRGTJrG+8ytdylOIWI=
=V6X6
-----END PGP SIGNATURE-----

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[email protected] wrote:

I really like this, but this would be nice too

headers = [ “id” , “name” , …]
exclude = [ “phone_number” , “user_id” ]
@jobs.to_csv( ‘myfile.csv’ , :header=> headers , :exclude_cols=>
exclue )

so I can add headers to the csv file, and also exclude certain columns
easily

Ok, i just committed the first rendition to my svn repos for
ActiveRecored::Extensions with an initial test. I’ll add some test
cases for this later today and get the functionality committed.

This weekend ActiveRecord::Extensions will launch a new release so if
you can hold out until then you’ll be able to install it as
a plugin or as a gem. If not feel free to pull down from trunk,
http://rubyforge.org/projects/arext/

Zach
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFgHR4Myx0fW1d8G0RAhxFAJsECzyvsbhhwBaTNyn3P82ko2jd2ACfVtSc
XmNcOf8kvL3atwmpEwel3ek=
=+jtQ
-----END PGP SIGNATURE-----