Class: Resque::Reports::CsvReport
- Inherits:
-
BaseReport
- Object
- BaseReport
- Resque::Reports::CsvReport
- Extended by:
- Forwardable
- Defined in:
- lib/resque/reports/csv_report.rb
Overview
Class to inherit from for custom CSV reports To make your custom report you must define at least:
1. directory, is where to write reports to
2. source, is symbol of method that retrieves report data
3. table, report table configuration using DSL
Constant Summary collapse
- DEFAULT_CSV_OPTIONS =
{col_sep: ';', row_sep: "\r\n"}
Constants inherited from BaseReport
Class Attribute Summary collapse
-
.options ⇒ Object
Returns the value of attribute options.
Attributes inherited from BaseReport
Instance Method Summary collapse
-
#error_message(error) ⇒ Object
– Event handling # ++.
-
#initialize(*args) ⇒ CsvReport
constructor
A new instance of CsvReport.
- #write(io, force = false) ⇒ Object
- #write_line(csv, row_cells) ⇒ Object
Methods inherited from BaseReport
Methods included from Extensions
Constructor Details
#initialize(*args) ⇒ CsvReport
Returns a new instance of CsvReport.
26 27 28 29 30 |
# File 'lib/resque/reports/csv_report.rb', line 26 def initialize(*args) DEFAULT_CSV_OPTIONS.merge( || Hash.new) super(*args) end |
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
15 16 17 |
# File 'lib/resque/reports/csv_report.rb', line 15 def @options end |
Instance Method Details
#error_message(error) ⇒ Object
– Event handling # ++
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/resque/reports/csv_report.rb', line 66 def (error) = [] << 'Выгрузка отчета невозможна. ' << case error when Encoding::UndefinedConversionError <<-ERR_MSG.gsub(/^ {29}/, '') Символ #{error.error_char} не поддерживается заданной кодировкой ERR_MSG when EncodingError 'Ошибка преобразования в заданную кодировку' else fail error end * ' ' end |
#write(io, force = false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/resque/reports/csv_report.rb', line 32 def write(io, force = false) # You must use ancestor methods to work with report data: # 1) data_size => returns source data size # 2) data_each => yields given block for each source data element # 3) build_table_header => returns Array of report column names # 4) build_table_row(object) => returns Array of report cell values # (same order as header) progress = 0 CSV(io, ) do |csv| write_line csv, build_table_header data_each(force) do |data_element| begin write_line csv, build_table_row(data_element) rescue handle_error end handle_progress(progress += 1, data_size) end handle_progress(progress, data_size, true) end end |
#write_line(csv, row_cells) ⇒ Object
58 59 60 |
# File 'lib/resque/reports/csv_report.rb', line 58 def write_line(csv, row_cells) csv << row_cells end |