Class: MagicReport::Report::Csv
- Inherits:
-
Object
- Object
- MagicReport::Report::Csv
- Defined in:
- lib/magic_report/report/csv.rb
Instance Attribute Summary collapse
-
#csv ⇒ Object
readonly
Returns the value of attribute csv.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Instance Method Summary collapse
- #generate ⇒ Object
-
#initialize(report) ⇒ Csv
constructor
A new instance of Csv.
- #io ⇒ Object
-
#unlink ⇒ Object
Don’t forget to unlink in production code.
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
#csv ⇒ Object (readonly)
Returns the value of attribute csv.
8 9 10 |
# File 'lib/magic_report/report/csv.rb', line 8 def csv @csv end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
8 9 10 |
# File 'lib/magic_report/report/csv.rb', line 8 def file @file end |
#report ⇒ Object (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
#generate ⇒ Object
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 |
#io ⇒ Object
30 31 32 33 34 |
# File 'lib/magic_report/report/csv.rb', line 30 def io io = csv.to_io io.rewind io end |
#unlink ⇒ Object
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 |