Class: Dossier::StreamCSV

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, headers = nil) ⇒ StreamCSV

Returns a new instance of StreamCSV.



7
8
9
10
# File 'lib/dossier/stream_csv.rb', line 7

def initialize(collection, headers = nil)
  @headers    = headers || collection.shift unless false === headers
  @collection = collection
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



5
6
7
# File 'lib/dossier/stream_csv.rb', line 5

def collection
  @collection
end

#headersObject (readonly)

Returns the value of attribute headers.



5
6
7
# File 'lib/dossier/stream_csv.rb', line 5

def headers
  @headers
end

Instance Method Details

#eachObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dossier/stream_csv.rb', line 12

def each
  yield headers.to_csv if headers?
  collection.each do |record|
    yield record.to_csv
  end
rescue => e
  if Rails.application.config.consider_all_requests_local
    yield e.message
    e.backtrace.each do |line|
      yield "#{line}\n"
    end
  else
    yield "We're sorry, but something went wrong." 
  end
end