Class: Resque::Reports::BaseReport
- Inherits:
-
Object
- Object
- Resque::Reports::BaseReport
- Extended by:
- Forwardable
- Includes:
- Extensions
- Defined in:
- lib/resque/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
# 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
Constant Summary collapse
- DEFAULT_EXTENSION =
– Constants # ++
'txt'- DEFAULT_QUEUE =
:base
Instance Method Summary collapse
-
#bg_build(force = false) ⇒ Object
Builds report in background, returns job_id, to watch progress.
-
#build(force = false) ⇒ Object
Builds report synchronously.
-
#initialize(*args) ⇒ BaseReport
constructor
– Public instance methods ++.
Methods included from Extensions
Constructor Details
#initialize(*args) ⇒ BaseReport
– Public instance methods ++
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/resque/reports/base_report.rb', line 150 def initialize(*args) # TODO: Check consistance, fail if user initialized wrong object set_instance(self) if create_block define_singleton_method(:create_dispatch, create_block) create_dispatch(*args) else if args && (attrs_hash = args.first) && attrs_hash.is_a?(Hash) attrs_hash.each do |name, value| send("#{name}=", value) end end end @args = args init_cache_file init_table end |
Instance Method Details
#bg_build(force = false) ⇒ Object
Builds report in background, returns job_id, to watch progress
179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/resque/reports/base_report.rb', line 179 def bg_build(force = false) report_class = self.class.to_s args_json = [*@args, force].to_json # Check report if it already in progress and tring return its job_id... job_id = ReportJob.enqueued?(report_class, args_json).try(:meta_id) # ...and start new job otherwise job_id || ReportJob.enqueue_to(job_queue || DEFAULT_QUEUE, report_class, args_json).try(:meta_id) end |
#build(force = false) ⇒ Object
Builds report synchronously
172 173 174 175 176 |
# File 'lib/resque/reports/base_report.rb', line 172 def build(force = false) init_table if force @cache_file.open(force) { |file| write(file, force) } end |