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
# File 'lib/magic_report/report/csv.rb', line 10

def initialize(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

#add_headings(report) ⇒ Object



23
24
25
# File 'lib/magic_report/report/csv.rb', line 23

def add_headings(report)
  csv << report.headings
end

#add_row(row) ⇒ Object



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

def add_row(row)
  csv << row.to_a
end

#generateObject



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

def generate
  write_headers

  report.rows.each do |row|
    csv << row.to_a
  end
end

#ioObject



37
38
39
40
41
# File 'lib/magic_report/report/csv.rb', line 37

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

Don’t forget to unlink in production code



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

def unlink
  file.close
  file.unlink
end