lib/comma.rb in crafterm-comma-0.1.8 vs lib/comma.rb in crafterm-comma-0.2.0
- old
+ new
@@ -2,13 +2,20 @@
require 'fastercsv'
require 'comma/extractors'
class Array
def to_comma(style = :default)
- FasterCSV.generate do |csv|
+ options = {}
+
+ if style.is_a? Hash
+ options = style
+ style = options.delete(:style)||:default
+ end
+
+ FasterCSV.generate(options) do |csv|
return "" if empty?
- csv << first.to_comma_headers(style)
+ csv << first.to_comma_headers(style) # REVISIT: request to optionally include headers
each do |object|
csv << object.to_comma(style)
end
end
end
@@ -40,10 +47,10 @@
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.respond_to? '[]') and options[:csv]
+ return render_without_csv(options, extra_options, &block) unless options.is_a?(Hash) and options[:csv]
data = options.delete(:csv)
style = options.delete(:style) || :default
send_data Array(data).to_comma(style), options.merge(:type => :csv)
end
end