Class: Resque::Reports::CsvReport

Inherits:
BaseReport show all
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

BaseReport::DEFAULT_QUEUE

Class Attribute Summary collapse

Attributes inherited from BaseReport

#job_id

Instance Method Summary collapse

Methods inherited from BaseReport

#bg_build, #build, build

Methods included from Extensions

included

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)
  csv_options DEFAULT_CSV_OPTIONS.merge(options || Hash.new)

  super(*args)
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



15
16
17
# File 'lib/resque/reports/csv_report.rb', line 15

def options
  @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_message(error)
  error_message = []
  error_message << 'Выгрузка отчета невозможна. '
  error_message << case error
                   when Encoding::UndefinedConversionError
                     <<-ERR_MSG.gsub(/^ {29}/, '')
                       Символ #{error.error_char} не поддерживается
                       заданной кодировкой
                     ERR_MSG
                   when EncodingError
                     'Ошибка преобразования в заданную кодировку'
                   else
                     fail error
                   end
  error_message * ' '
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, options) 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