lib/tablesmith/table.rb in tablesmith-0.2.2 vs lib/tablesmith/table.rb in tablesmith-0.3.0
- old
+ new
@@ -1,6 +1,7 @@
require 'text-table'
+require 'csv'
module Tablesmith
class Table < Array
def method_missing(meth_id, *args)
count = 1
@@ -35,9 +36,25 @@
sort_columns(rows)
rows = create_headers(rows) + (rows.map { |row| row_values(row) })
rows.to_text_table
+ end
+
+ def to_csv
+ CSV.generate do |csv|
+ text_table.rows.each do |row|
+ next if row == :separator
+ csv << row.map do |cell|
+ case cell
+ when Hash
+ cell[:value]
+ else
+ cell
+ end
+ end
+ end
+ end
end
# override in subclass or mixin
def row_values(row)
row
\ No newline at end of file