Class: Beardley::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/beardley/report.rb

Overview

This class permit to produce reports in PDF, ODT and DOCX using Jasper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report, options = {}) ⇒ Report

Constructor for a report generator

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/beardley/report.rb', line 31

def initialize(report, options = {})
  @parameters = options.delete(:report) || {}
  @source_file, @object_file = nil, nil
  if report.is_a?(Pathname)
    @source_file = report if report.extname.downcase == ".jrxml" or report.extname.downcase == ".xml"
    @object_file = report if report.extname.downcase == ".jasper"
  elsif report.is_a?(String)
    hash = Digest::SHA256.hexdigest(report)
    @source_file = Pathname.new(options[:tmp_dir] || "/tmp").join("report-" + hash + '.jrxml')
    File.open(@source_file, 'wb') do |f|
      f.write(report)
    end
  end
  @object_file ||= @source_file.dirname.join(@source_file.basename.to_s + ".jasper")
  raise ArgumentError.new("An object must be given at least") unless @object_file.is_a?(Pathname)
end

Instance Attribute Details

#object_fileObject (readonly)

Returns the value of attribute object_file.



28
29
30
# File 'lib/beardley/report.rb', line 28

def object_file
  @object_file
end

#source_fileObject (readonly)

Returns the value of attribute source_file.



28
29
30
# File 'lib/beardley/report.rb', line 28

def source_file
  @source_file
end

Instance Method Details

#to_csv(*args) ⇒ Object

Export report to CSV with given datasource



66
67
68
# File 'lib/beardley/report.rb', line 66

def to_csv(*args)
  return to(:csv, *args)
end

#to_docx(*args) ⇒ Object

Export report to DOCX with given datasource



71
72
73
# File 'lib/beardley/report.rb', line 71

def to_docx(*args)
  return to(:docx, *args)
end

#to_ods(*args) ⇒ Object

Export report to ODS with given datasource



61
62
63
# File 'lib/beardley/report.rb', line 61

def to_ods(*args)
  return to(:ods, *args)
end

#to_odt(*args) ⇒ Object

Export report to ODT with given datasource



56
57
58
# File 'lib/beardley/report.rb', line 56

def to_odt(*args)
  return to(:odt, *args)
end

#to_pdf(*args) ⇒ Object

Export report to PDF with given datasource



49
50
51
52
53
# File 'lib/beardley/report.rb', line 49

def to_pdf(*args)
  options = extract_options!(args)
  datasource = args[0]
  return JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', prepare(datasource))
end

#to_xlsx(*args) ⇒ Object

Export report to XLSX with given datasource



76
77
78
# File 'lib/beardley/report.rb', line 76

def to_xlsx(*args)
  return to(:xlsx, *args)
end