Sha256: bb7b8c79e8d91f80f30c0b533cc1d278fee8a2979629ccdcea5fa065414e8e45

Contents?: true

Size: 994 Bytes

Versions: 3

Compression:

Stored size: 994 Bytes

Contents

require 'activesupport'
require 'fastercsv'
require 'comma/extractors'

class Array
  def to_comma
    FasterCSV.generate do |csv|
      csv << first.to_comma_headers
      each do |object| 
        csv << object.to_comma
      end
    end
  end
end

class Object
  def self.comma(&block)
    define_method :to_comma do
      Comma::DataExtractor.new(self, &block).results
    end

    define_method :to_comma_headers do
      Comma::HeaderExtractor.new(self, &block).results
    end
  end
end

if defined?(ActionController)
  module RenderAsCSV

    def self.included(base)
      base.send :include, InstanceMethods
      base.alias_method_chain :render, :csv
    end

    module InstanceMethods
      def render_with_csv(options = nil, extra_options = {}, &block)
        return render_without_csv(options, extra_options, &block) unless options and options[:csv]
        send_data Array(options[:csv]).to_comma
      end
    end

  end

  ActionController::Base.send :include, RenderAsCSV
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
crafterm-comma-0.1.1 lib/comma.rb
crafterm-comma-0.1.2 lib/comma.rb
crafterm-comma-0.1.3 lib/comma.rb