Class: MagicReport::Report::Csv

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_report/report/csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report) ⇒ Csv

Returns a new instance of Csv.



10
11
12
13
14
# File 'lib/magic_report/report/csv.rb', line 10

def initialize(report)
  @report = report
  @file = Tempfile.new
  @csv = ::CSV.new(@file, write_headers: true)
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



8
9
10
# File 'lib/magic_report/report/csv.rb', line 8

def csv
  @csv
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/magic_report/report/csv.rb', line 8

def file
  @file
end

#reportObject (readonly)

Returns the value of attribute report.



8
9
10
# File 'lib/magic_report/report/csv.rb', line 8

def report
  @report
end

Instance Method Details

#generateObject



16
17
18
19
20
21
22
# File 'lib/magic_report/report/csv.rb', line 16

def generate
  write_headers

  report.result.each do |row|
    row.to_h.each { |nested_row| csv << nested_row.values }
  end
end

#ioObject



30
31
32
33
34
# File 'lib/magic_report/report/csv.rb', line 30

def io
  io = csv.to_io
  io.rewind
  io
end

Don’t forget to unlink in production code



25
26
27
28
# File 'lib/magic_report/report/csv.rb', line 25

def unlink
  file.close
  file.unlink
end