ActiveRecord Dumper
Formats ActiveRecord data in chunks and dumps it to a file, temporary file, or string. Specify the page_size used to paginate records and flush files.
Specify Output
-
:filenamethe name of the file to create. By default will create a file based on the timestamp. -
:file_extensionappends file extension unless one exists in file name -
:onlya list of the attributes to be included. By default, all column_names are used. -
:excepta list of the attributes to be excluded. By default, all column_names are used. This option is not available if:onlyis used -
:methodsa list of the methods to be called on the object -
:procshash of header name to Proc object
Attributes (Only and Exclude)
Specify which attributes to include and exclude
Book.dumper :yml, :only => [:author_name, :title]
Book.dump :csv, :except => [:topic_id]
Methods
Use :methods to include methods on the record that are not column attributes
BigOle.dumper :csv, :methods => [:age, :favorite_food]
Output..
..other attributes.., 25, doughnuts
Proc
To call procs on the object(s) use :procs with a hash of name to value The dumper options hash are provided to the proc, and contains the current record options[:record] is provided
Proc Options Hash
-
:record- the active record -
:result_set- the current result set -
:counter- the number of the record -
:page_num- the page number -
:target- the file/string targettopic_content_proc = Proc.new{|options| options.topic ? options.topic.content : ‘NO CONTENT’ } Book.dumper :procs => => topic_content_proc})
<book> # ... other attributes and methods ... <topic-content>NO CONTENT</my_rating> </book>
Finder Methods
-
:finda map of the finder options passed to find. For example,{:conditions => ['hairy = ?', 'of course'], :include => :rodents} -
:records- the records to be dumped instead of using a find
Format Header
-
:headerwhen a hash is specified, maps the field name to the header name. For example{:a => 'COL A', :b => 'COL B'}would print ‘COL A’, ‘COL B’
when an array is specified uses this instead of the fields when true or by default prints the fields when false does not include a header
-
:text_formata string method such as:titleize,:dasherize,:underscoreto format the on all the headers. If an attribute is:email_addressand:titleizeis chosen, then the Header value is “Email Address” -
:rootIn xml, this is the name of the highest level list object. The plural of the class name is the default. For yml, this is the base name of thethe objects. Each record will be root_id. For example, contact_2348
Filename and Target
:target_type The target_type for the data. Defaults to :file.
-
:stringprints to string. Do not use with large data sets -
:tmp_file. Use a temporary file that is destroyed when the process exists -
:file. Use a standard file
:filename basename of the file. Defaults to random time based string for non-temporary files :file_extension Extension (suffix) like .csv, .xml. Added only if the basename has no suffix. :file_extension is only available when :target_type_type => :file :file_path path or directory of the file. Defaults to dumper_file_path or temporary directories
Format specific options
-
:csv- any options to pass to csv parser. Example:csv => { :col_sep => "\t" } -
:xml- any options to pass to xml parser. Example:xml => { :indent => 4 }
Installation
script/plugin install git://github.com/blythedunham/ar_dumper.git
Developers
Blythe Dunham snowgiraffe.com
Homepage
-
Project Site: github.com/blythedunham/ar_dumper/tree/master