Class: Ruby::Reports::BaseReport

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ruby/reports/base_report.rb

Overview

Class describes base report class for inheritance. BaseReport successor must implement “write(io, force)” method and may specify file extension with “extension” method call example:

class CustomTypeReport < Resque::Reports::BaseReport
  extension :type # specify that report file must ends
                  # with '.type', e.g. 'abc.type'

  # Method specifies how to output report data
  def write(io, force)
    io << 'Hello World!'
  end
end

BaseReport provides following DSL, example:

class CustomReport < CustomTypeReport
  # include Resque::Reports::Common::BatchedReport
  #   overrides data retrieving to achieve batching
  #   if included 'source :select_data' becomes needless

  queue :custom_reports # Resque queue name
  source :select_data # method called to retrieve report data
  encoding UTF8 # file encoding
  expire_in 86_400 # cache time of the file, default: 86_400

  # Specify in which directory to keep this type files
  directory File.join(Dir.tmpdir, 'resque-reports')

  # Describe table using 'column' method
  table do |element|
    column 'Column 1 Header', :decorate_one
    column 'Column 2 Header', decorate_two(element[1])
    column 'Column 3 Header', 'Column 3 Cell'
    column 'Column 4 Header', :formatted_four, formatter: :just_cute
  end

  # Class initialize if needed
  # NOTE: must be used instead of define 'initialize' method
  # Default behaviour is to receive in *args Hash with report attributes
  # like: CustomReport.new(main_param: 'value') => calls send(:main_param=, 'value')
  create do |param|
    @main_param = param
  end

  def self.just_cute_formatter(column_value)
    "I'm so cute #{column_value}"
  end

  # decorate method, called by symbol-name
  def decorate_one(element)
    "decorate_one: #{element[0]}"
  end

  # decorate method, called directly when filling cell
  def decorate_two(text)
    "decorate_two: #{text}"
  end

  # method returns report data Enumerable
  def select_data
    [[0, 'text0'], [1, 'text1']]
  end
end

Direct Known Subclasses

CsvReport

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ BaseReport

– Public instance methods ++



102
103
104
105
# File 'lib/ruby/reports/base_report.rb', line 102

def initialize(*args)
  @args = args
  assign_attributes
end

Class Attribute Details

.config_hashObject (readonly)

Returns the value of attribute config_hash.



75
76
77
# File 'lib/ruby/reports/base_report.rb', line 75

def config_hash
  @config_hash
end

.error_handle_blockObject (readonly)

Returns the value of attribute error_handle_block.



75
76
77
# File 'lib/ruby/reports/base_report.rb', line 75

def error_handle_block
  @error_handle_block
end

.progress_handle_blockObject (readonly)

Returns the value of attribute progress_handle_block.



75
76
77
# File 'lib/ruby/reports/base_report.rb', line 75

def progress_handle_block
  @progress_handle_block
end

.table_blockObject (readonly)

Returns the value of attribute table_block.



75
76
77
# File 'lib/ruby/reports/base_report.rb', line 75

def table_block
  @table_block
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



95
96
97
# File 'lib/ruby/reports/base_report.rb', line 95

def args
  @args
end

#events_handlerObject (readonly)

Returns the value of attribute events_handler.



95
96
97
# File 'lib/ruby/reports/base_report.rb', line 95

def events_handler
  @events_handler
end

#job_idObject (readonly)

Returns the value of attribute job_id.



95
96
97
# File 'lib/ruby/reports/base_report.rb', line 95

def job_id
  @job_id
end

Class Method Details

.build(options = {}) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/ruby/reports/base_report.rb', line 85

def build(options = {})
  force = options.delete(:force)

  report = new(options)
  report.build(force)

  report
end

.config(hash) ⇒ Object



77
78
79
# File 'lib/ruby/reports/base_report.rb', line 77

def config(hash)
  @config_hash = hash
end

.table(&block) ⇒ Object



81
82
83
# File 'lib/ruby/reports/base_report.rb', line 81

def table(&block)
  @table_block = block
end

Instance Method Details

#build(force = false) ⇒ Object

Builds report synchronously



108
109
110
111
112
113
# File 'lib/ruby/reports/base_report.rb', line 108

def build(force = false)
  @table = nil if force
  @events_handler = Services::EventsHandler.new(@progress_handle_block, @error_handle_block)

  cache_file.open(force) { |file| write(file, force) }
end

#error_handler(&block) ⇒ Object



119
120
121
# File 'lib/ruby/reports/base_report.rb', line 119

def error_handler(&block)
  @error_handle_block = block
end

#progress_handler(&block) ⇒ Object



115
116
117
# File 'lib/ruby/reports/base_report.rb', line 115

def progress_handler(&block)
  @progress_handle_block = block
end