Skip to content

blythedunham/ar_dumper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • :filename the name of the file to create. By default will create a file based on the timestamp.

  • :file_extension appends file extension unless one exists in file name

  • :only a list of the attributes to be included. By default, all column_names are used.

  • :except a list of the attributes to be excluded. By default, all column_names are used. This option is not available if :only is used

  • :methods a list of the methods to be called on the object

  • :procs hash 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 target

    topic_content_proc = Proc.new{|options| options.topic ? options.topic.content : ‘NO CONTENT’ } Book.dumper :procs => {:topic_content => topic_content_proc}})

    <book>
      # ... other attributes and methods ...
      <topic-content>NO CONTENT</my_rating>
    </book>

Finder Methods

  • :find a 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

  • :header when 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_format a string method such as :titleize, :dasherize, :underscore to format the on all the headers. If an attribute is :email_address and :titleize is chosen, then the Header value is “Email Address”

  • :root In 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 the

    the 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.

  • :string prints 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

Plugin

script/plugin install git://github.com/blythedunham/ar_dumper.git

Gem

gem sources -a http://gems.github.com
sudo gem install blythedunham-ar_dumper

Developers

Blythe Dunham snowgiraffe.com

Homepage

About

Extends ActiveRecord to export volume records to a file, string, or temp file in csv, yaml, or xml formats

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages