John Kopanas
2006-Dec-13 18:52 UTC
Taking Any Returned Record Set and Writing It In CVS Format to A File
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 Kopanas john-Iau1QiYlxLpBDgjK7y7TUQ@public.gmane.org http://www.kopanas.com http://www.cusec.net http://www.soen.info --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
zdennis
2006-Dec-13 19:29 UTC
Re: Taking Any Returned Record Set and Writing It In CVS Format to A File
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 John Kopanas 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----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jodi Showers
2006-Dec-13 19:35 UTC
Re: [FAQ] Taking Any Returned Record Set and Writing It In CSV Format to A File
definitely a faq item - CSV (common separated values export) On 13-Dec-06, at 2:29 PM, zdennis wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > John Kopanas 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----- > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
zdennis
2006-Dec-13 19:55 UTC
Re: [FAQ] Taking Any Returned Record Set and Writing It In CSV Format to A File
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Jodi Showers 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> > On 13-Dec-06, at 2:29 PM, zdennis wrote: > > John Kopanas 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 iD8DBQFFgFqjMyx0fW1d8G0RAulQAJ0WbwEETGthP9OvAps7Llo9CxyWGACfUYV8 OkRYyGRGTJrG+8ytdylOIWI=V6X6 -----END PGP SIGNATURE----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jodi Showers
2006-Dec-13 20:03 UTC
Re: Taking Any Returned Record Set and Writing It In CSV Format to A File
referring to this proposal: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/ 99ea153cc378f7f1/ba010c2c547d7637?lnk=gst&q=%5BFAQ% 5D&rnum=2#ba010c2c547d7637 On 13-Dec-06, at 2:55 PM, zdennis wrote:> > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > Jodi Showers 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 > >> >> On 13-Dec-06, at 2:29 PM, zdennis wrote: >> >> John Kopanas 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 > > iD8DBQFFgFqjMyx0fW1d8G0RAulQAJ0WbwEETGthP9OvAps7Llo9CxyWGACfUYV8 > OkRYyGRGTJrG+8ytdylOIWI> =V6X6 > -----END PGP SIGNATURE----- > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
pmr16366-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2006-Dec-13 20:46 UTC
Re: Taking Any Returned Record Set and Writing It In CVS Format to A File
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 zdennis wrote:> -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > John Kopanas 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-------~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
zdennis
2006-Dec-13 21:45 UTC
Re: Taking Any Returned Record Set and Writing It In CVS Format to A File
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 pmr16366-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org 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 > easilyOk, 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----- --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---