crafterm-comma 0.1.8 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +4 -0
- data/lib/comma/extractors.rb +2 -2
- data/lib/comma.rb +10 -3
- metadata +1 -1
data/README.markdown
CHANGED
@@ -109,6 +109,10 @@ You can specify which output format you would like to use via an optional parame
|
|
109
109
|
|
110
110
|
Specifying no description name to to_comma is equivalent to specifying :default as the description name.
|
111
111
|
|
112
|
+
You can pass options for FasterCVS, e.g.
|
113
|
+
|
114
|
+
Book.limited(10).to_comma(:style => :brief, :col_sep => ';', :force_quotes => true)
|
115
|
+
|
112
116
|
When used with Rails (ie. add 'comma' as a gem dependency), Comma automatically adds support for rendering CSV output in your controllers:
|
113
117
|
|
114
118
|
class BooksController < ApplicationController
|
data/lib/comma/extractors.rb
CHANGED
data/lib/comma.rb
CHANGED
@@ -4,9 +4,16 @@ require 'comma/extractors'
|
|
4
4
|
|
5
5
|
class Array
|
6
6
|
def to_comma(style = :default)
|
7
|
-
|
7
|
+
options = {}
|
8
|
+
|
9
|
+
if style.is_a? Hash
|
10
|
+
options = style
|
11
|
+
style = options.delete(:style)||:default
|
12
|
+
end
|
13
|
+
|
14
|
+
FasterCSV.generate(options) do |csv|
|
8
15
|
return "" if empty?
|
9
|
-
csv << first.to_comma_headers(style)
|
16
|
+
csv << first.to_comma_headers(style) # REVISIT: request to optionally include headers
|
10
17
|
each do |object|
|
11
18
|
csv << object.to_comma(style)
|
12
19
|
end
|
@@ -42,7 +49,7 @@ if defined?(ActionController)
|
|
42
49
|
|
43
50
|
module InstanceMethods
|
44
51
|
def render_with_csv(options = nil, extra_options = {}, &block)
|
45
|
-
return render_without_csv(options, extra_options, &block) unless
|
52
|
+
return render_without_csv(options, extra_options, &block) unless options.is_a?(Hash) and options[:csv]
|
46
53
|
data = options.delete(:csv)
|
47
54
|
style = options.delete(:style) || :default
|
48
55
|
send_data Array(data).to_comma(style), options.merge(:type => :csv)
|