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



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
                     "                       \u0421\u0438\u043C\u0432\u043E\u043B \#{error.error_char} \u043D\u0435 \u043F\u043E\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044F\n                       \u0437\u0430\u0434\u0430\u043D\u043D\u043E\u0439 \u043A\u043E\u0434\u0438\u0440\u043E\u0432\u043A\u043E\u0439\n                     ERR_MSG\n                   when EncodingError\n                     '\u041E\u0448\u0438\u0431\u043A\u0430 \u043F\u0440\u0435\u043E\u0431\u0440\u0430\u0437\u043E\u0432\u0430\u043D\u0438\u044F \u0432 \u0437\u0430\u0434\u0430\u043D\u043D\u0443\u044E \u043A\u043E\u0434\u0438\u0440\u043E\u0432\u043A\u0443'\n                   else\n                     fail error\n                   end\n  error_message * ' '\nend\n".gsub(/^ {29}/, '')

#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