public
Description: Comma is a small CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL
Homepage: http://github.com/crafterm/comma
Clone URL: git://github.com/crafterm/comma.git
comma /
name age message
file .gitignore Sat Dec 12 19:29:00 -0800 2009 Updated to use Jeweler for gem package management [crafterm]
file MIT-LICENSE Sun Feb 22 01:52:18 -0800 2009 Initial commit [crafterm]
file README.rdoc Fri Dec 11 15:25:00 -0800 2009 typo [stefanpenner]
file Rakefile Sat Dec 12 19:29:00 -0800 2009 Updated to use Jeweler for gem package management [crafterm]
file VERSION Sat Dec 12 19:29:04 -0800 2009 Version bump to 0.3.0 [crafterm]
file init.rb Fri Dec 11 16:03:32 -0800 2009 merged in bquorning's branch, and applied the p... [stefanpenner]
directory lib/ Fri Dec 11 16:03:32 -0800 2009 merged in bquorning's branch, and applied the p... [stefanpenner]
directory spec/ Fri Dec 11 16:03:32 -0800 2009 merged in bquorning's branch, and applied the p... [stefanpenner]
README.rdoc

COMMA

github.com/crafterm/comma

DESCRIPTION:

Comma is a CSV (ie. comma separated values) generation extension for Ruby objects, that lets you seamlessly define a CSV output format via a small DSL. Comma works well on pure Ruby objects with attributes, as well as complex ones such as ActiveRecord objects with associations, extensions, etc. It doesn’t distinguish between attributes, methods, associations, extensions, etc. - they all are considered equal and invoked identically via the Comma DSL description. Multiple different CSV output descriptions can also be defined.

When multiple objects in an Array are converted to CSV, the output includes generation of a header row reflected from names of the properties requested, or specified via the DSL.

CSV can be a bit of a boring format - the motivation behind Comma was to have a CSV extension that was simple, flexible, and would treat attributes, methods, associations, etc., all the same without the need for any complex configuration, and also work on Ruby objects, not just ActiveRecord or other base class derivatives.

An example Comma CSV enabled ActiveRecord class:

    class Book < ActiveRecord::Base

      # ================
      # = Associations =
      # ================
      has_many   :pages
      has_one    :isbn
      belongs_to :publisher

      # ===============
      # = CSV support =
      # ===============
      comma do

        name
        description

        pages :size => 'Pages'
        publisher :name
        isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
        blurb 'Summary'

      end

    end

Annotated, the comma description is as follows:

    # starts a Comma description block, generating 2 methods #to_comma and #to_comma_headers for this class.
    comma do

      # name, description are attributes of Book with the header being reflected as 'Name', 'Description'
      name
      description

      # pages is an association returning an array, :size is called on the association results, with the header name specified as 'Pages'
      pages :size => 'Pages'

      # publisher is an association returning an object, :name is called on the associated object, with the reflected header 'Name'
      publisher :name

      # isbn is an association returning an object, :number_10 and :number_13 are called on the object with the specified headers 'ISBN-10' and 'ISBN-13'
      isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'

      # blurb is an attribute of Book, with the header being specified directly as 'Summary'
      blurb 'Summary'

    end

In the above example, any of the declarations (name, description, pages, publisher, isbn, blurb, etc), could be methods, attributes, associations, etc - no distinction during configuration is required, as everything is invoked via Ruby’s #send method.

You can get the CSV representation of any object by calling the to_comma method, optionally providing a CSV description name to use.

Object values are automatically converted to strings via to_s allowing you to reuse any existing to_s methods on your objects (instead of having to call particular properties or define CSV specific output methods). Header names are also automatically humanized when reflected (eg. Replacing _ characters with whitespace). The ‘isbn’ example above shows how multiple values can be added to the CSV output.

Multiple CSV descriptions can also be specified for the same class, eg:

    class Book < ActiveRecord::Base

      # ================
      # = Associations =
      # ================
      has_many   :pages
      has_one    :isbn
      belongs_to :publisher

      # ===============
      # = CSV support =
      # ===============
      comma do

        name
        description

        pages :size => 'Pages'
        publisher :name
        isbn :number_10 => 'ISBN-10', :number_13 => 'ISBN-13'
        blurb 'Summary'

      end

      comma :brief do

        name
        description
        blurb 'Summary'

      end

    end

You can specify which output format you would like to use via an optional parameter to to_comma:

    Book.limited(10).to_comma(:brief)

Specifying no description name to to_comma is equivalent to specifying :default as the description name.

You can pass options for FasterCVS, e.g.

    Book.limited(10).to_comma(:style => :brief, :col_sep => ';', :force_quotes => true)

When used with Rails (ie. add ‘comma’ as a gem dependency), Comma automatically adds support for rendering CSV output in your controllers:

    class BooksController < ApplicationController

      def index
        respond_to do |format|
          format.csv { render :csv => Book.limited(50) }
        end
      end

    end

If you have any questions or suggestions for Comma, please feel free to contact me at crafterm@redartisan.com, all feedback welcome!

DEPENDENCIES

If you’re on ruby 1.8.*, the fasterCSV gem is recommended for performance reasons.

    gem install fastercsv