Class: Ruby::Reports::CsvReport
- Inherits:
-
BaseReport
- Object
- BaseReport
- Ruby::Reports::CsvReport
- Defined in:
- lib/ruby/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
Instance Attribute Summary collapse
-
#csv_options ⇒ Object
readonly
Returns the value of attribute csv_options.
Attributes inherited from BaseReport
#args, #events_handler, #job_id
Instance Method Summary collapse
- #error_message(error) ⇒ Object
-
#initialize(*args) ⇒ CsvReport
constructor
A new instance of CsvReport.
-
#progress_message(*args) ⇒ Object
– Event handling # ++.
- #write(io, force = false) ⇒ Object
- #write_line(csv, row_cells) ⇒ Object
Methods inherited from BaseReport
#build, build, config, #error_handler, #progress_handler, table
Constructor Details
#initialize(*args) ⇒ CsvReport
Returns a new instance of CsvReport.
14 15 16 17 18 |
# File 'lib/ruby/reports/csv_report.rb', line 14 def initialize(*args) config.extension = :csv super @csv_options = config. end |
Instance Attribute Details
#csv_options ⇒ Object (readonly)
Returns the value of attribute csv_options.
12 13 14 |
# File 'lib/ruby/reports/csv_report.rb', line 12 def @csv_options end |
Instance Method Details
#error_message(error) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ruby/reports/csv_report.rb', line 59 def (error) = [] << 'Выгрузка отчета невозможна. ' << case error when Encoding::UndefinedConversionError <<-ERR_MSG.gsub(/^ {29}/, '') Символ #{error.error_char} не поддерживается заданной кодировкой ERR_MSG when EncodingError 'Ошибка преобразования в заданную кодировку' else fail error end * ' ' end |
#progress_message(*args) ⇒ Object
– Event handling # ++
55 56 57 |
# File 'lib/ruby/reports/csv_report.rb', line 55 def (*args) 'Выгрузка отчета в CSV' end |
#write(io, force = false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ruby/reports/csv_report.rb', line 20 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, table.build_header iterator.data_each(force) do |data_element| begin write_line csv, table.build_row(data_element) rescue events_handler.error end events_handler.progress(progress += 1, iterator.data_size) end events_handler.progress(progress, iterator.data_size, true) end end |
#write_line(csv, row_cells) ⇒ Object
46 47 48 |
# File 'lib/ruby/reports/csv_report.rb', line 46 def write_line(csv, row_cells) csv << row_cells end |