README.rdoc in csv_pirate-3.4.4 vs README.rdoc in csv_pirate-4.0.1
- old
+ new
@@ -1,12 +1,14 @@
== CsvPirate
Easily create CSVs of any data that can be derived from your models.
-CsvPirate is the easy way to create a CSV of essentially anything in Rails, in full pirate regalia.
+CsvPirate is the easy way to create a CSV of essentially anything in Ruby, in full pirate regalia.
It works better if you are wearing a tricorne!
+Everything in the source that depended on Rails extensions of Ruby classes has been refactored to work in pure Ruby!
+
My goal is to have it do EVERYTHING it possibly can for me, since almost every project I do needs CSV exports.
CsvPirate only works for commissions of swag OR grub!
Initialize method (a.k.a new()) takes a hash of parameters:
@@ -31,13 +33,13 @@
# :clean - do not use :swab above (:counter or :timestamp), and instead overwrite the file
# :dirty - do not use :swab above (:counter, or :timestamp), and do not overwrite. Just keep adding on.
# :bury_treasure should we store the csv data as it is collected in an array in Ruby form for later use (true), or just write the CSV (false)?
# :blackjack Specify how you want your CSV header
# {:join => '-'} joins the method names called to get hte data for that column with '_' underscores.
- # {:humanize =>'-'} first joins as above, then humanizes the string
+ # {:humanize =>'-'} first joins as above, then humanizes the string (this is the default)
# {:array => ['col1',col2','col3'] Uses the column names provided. If the array's length is less than the booty array's length it reverts to :humanize =>'_'
- # check the source to see if there anything else hiding in there!
+ # check the source to see if there anything else hiding in there! There are a few new options in the 4.0.0 release, which are as yet undocumented.
The create method has the same parameters, and actually creates the CSV.
Avast! Here be pirates! To brush up on pirate coding naming conventions:
@@ -108,10 +110,12 @@
MyClass.walk_the_plank # creates the csv, and returns contents of the exported data (that was written into the csv) (as a string)
MyClass.land_ho # Does Not create the csv, sets up the CsvPirate instance. You can manipulate it and then call .hoist_mainstay on it to create the csv
== Usage without ActiveRecord
+[ See Spec Tests for more Examples! ]
+
Since the defaults assume an active record class you need to override some of them:
class Star
extend NinthBit::PirateShip::ActMethods
has_csv_pirate_ship :booty => [:name, :distance, :spectral_type, {:name => :hash}, {:name => :next}, {:name => :upcase}, :star_vowels],
@@ -197,11 +201,11 @@
:mop => :clean # Symbol: If we DO end up writing to a preexisting file (by design or accident) should we overwrite (:clean) or append (:dirty)?
:shrouds => ',' # String: Delimiter for CSV. '\t' will create a tab delimited file (tsv), '|' will create a pipe delimited file.
:bury_treasure => true # Boolean: Should the array of objects in :swag be stored in the CsvPirate object for later inspection?
:blackjack => {:humanize => '-'} # Hash: Specify how you want your CSV header
# {:join => '-'} joins the method names called to get hte data for that column with '_' underscores.
- # {:humanize => '-'} first joins as above, then humanizes the string
+ # {:humanize => '-'} first joins as above, then humanizes the string (this is the default)
# {:array => ['col1',col2','col3'] Uses the column names provided in the array. If the array provided is too short defaults to :humanize =>'_'
# A customized version to create a tab delimited file for this class might look like this:
# has_csv_pirate_ship :spyglasses => [:factory_in_germany],
# :booty => [:id, :name],
@@ -322,17 +326,24 @@
You have the same Make class as above, and you have a MakeController:
class MakeController < ApplicationController
def download_csv
csv_pirate = Make.blindfold
- send_data csv_pirate.maroon,
- :type => 'text/csv; charset=iso-8859-1; header=present',
- :disposition => "attachment; filename=#{csv_pirate.nocturnal}"
+
+ # maroon saves the read to the file system, by using the text of the csv stored in the CsvPirate object.
+ send_data csv_pirate.maroon, ...
+ :type => 'text/csv; charset=iso-8859-1; header=present',
+ :disposition => "attachment; filename=#{csv_pirate.nocturnal}"
+
+ # However if CSVs are created using multiple CsvPirate objects that all append to a single file,
+ # we need to read the final product from the fs.
+ #send_file csv_pirate.brigantine,
+ # :type => 'text/csv; charset=utf-8; header=present',
+ # :disposition => "attachment; filename=#{csv_pirate.nocturnal}"
end
end
-
== Advanced Example with Nested Methods
You have a VehicleModel class and the same Make class as up above:
# == Schema Information
@@ -396,9 +407,17 @@
This allows you to modify the csv_pirate object before creating the csv like this:
csv_pirate.booty -= [:id, :name]
csv_pirate.hoist_mainstay()
+
+== Tests
+
+The tests are run with rspec. The test suite is expanding. Currently there is ample coverage of basic functionality.
+
+To run tests cd to where ever you have csv_pirate installed, and do:
+
+ rake spec
== BUGS (Not mine, Micrsoft's)
Microsoft Office (Excel) "SYLK Invalid Format" Error will occur if the string "ID" (without quotes)
is at the beginning of the CSV file. This is strangely inconvenient for rails CSVs since every table