Class: Tabularasa

Inherits:
Object
  • Object
show all
Defined in:
lib/tabularasa.rb

Instance Method Summary collapse

Constructor Details

#initialize(rows, options = {}, &block) ⇒ Tabularasa

Returns a new instance of Tabularasa.



5
6
7
8
9
10
11
# File 'lib/tabularasa.rb', line 5

def initialize(rows, options = {}, &block)
  @generator = RUBY_VERSION >= '1.9' ? CSV : FasterCSV
  @delimiter = options[:delimiter] || ','
  @rows = rows
  @keys = (options[:only] || get_keys) - (options[:exclude] || [])
  @headers = options[:headers] || options[:only] || get_headers
end

Instance Method Details

#to_csvObject



13
14
15
16
17
18
19
20
21
# File 'lib/tabularasa.rb', line 13

def to_csv
  csv_data = @generator.generate(:col_sep => @delimiter) do |csv|
    csv << @headers
    @rows.each do |row|
      csv << collect(row)
    end
  end
  csv_data
end