Class: Resque::Reports::CsvReport

Inherits:
BaseReport show all
Extended by:
Forwardable
Includes:
Callbacks
Defined in:
lib/resque/reports/csv_report.rb

Constant Summary collapse

DEFAULT_CSV_OPTIONS =
{ col_sep: ';', row_sep: "\r\n" }

Constants inherited from BaseReport

BaseReport::DEFAULT_EXTENSION

Class Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseReport

#bg_build, #build

Methods included from Encodings

included

Constructor Details

#initialize(*args) ⇒ CsvReport

Returns a new instance of CsvReport.



18
19
20
21
# File 'lib/resque/reports/csv_report.rb', line 18

def initialize(*args)
  csv_options = DEFAULT_CSV_OPTIONS.merge(csv_options)
  super(*args)
end

Class Attribute Details

.csv_optionsObject

Returns the value of attribute csv_options.



9
10
11
# File 'lib/resque/reports/csv_report.rb', line 9

def csv_options
  @csv_options
end

Instance Method Details

#write(io) ⇒ Object

You must use ancestor methods to work with data:

1) get_data => returns Enumerable of source objects
2) build_table_header => returns Array of column names
3) build_table_row(object) => returns Array of cell values (same order as header)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/resque/reports/csv_report.rb', line 31

def write(io)        
  progress = 0

  CSV(io, csv_options) do |csv|
    data_collection = get_data
    
    if data_collection.size > 0
      write_line csv, build_table_header

      data_collection.each do |data_element|
        begin
          write_line csv, build_table_row(data_element)
        rescue
          handle_error
        end

        handle_progress(progress += 1)
      end

      handle_progress(progress, true)
    end
  end       
end

#write_line(csv, row_cells) ⇒ Object



55
56
57
# File 'lib/resque/reports/csv_report.rb', line 55

def write_line(csv, row_cells)
  csv << row_cells
end