Class: Beardley::Report
- Inherits:
-
Object
- Object
- Beardley::Report
- Defined in:
- lib/beardley/report.rb
Overview
This class permit to produce reports in PDF, ODT and DOCX using Jasper
Instance Attribute Summary collapse
-
#object_file ⇒ Object
readonly
Returns the value of attribute object_file.
-
#source_file ⇒ Object
readonly
Returns the value of attribute source_file.
Instance Method Summary collapse
- #compile(xml_path, compiled_path) ⇒ Object
-
#initialize(report, options = {}) ⇒ Report
constructor
Constructor for a report generator.
-
#to_csv(*args) ⇒ Object
Export report to CSV with given datasource.
-
#to_docx(*args) ⇒ Object
Export report to DOCX with given datasource.
-
#to_file(format, *args) ⇒ Object
Generic method to export to some format like ODT and DOCX as file in the given place.
-
#to_ods(*args) ⇒ Object
Export report to ODS with given datasource.
-
#to_odt(*args) ⇒ Object
Export report to ODT with given datasource.
-
#to_pdf(*args) ⇒ Object
Export report to PDF with given datasource.
-
#to_xlsx(*args) ⇒ Object
Export report to XLSX with given datasource.
Constructor Details
#initialize(report, options = {}) ⇒ Report
Constructor for a report generator
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/beardley/report.rb', line 30 def initialize(report, = {}) @parameters = .delete(:report) || {} @source_file = nil @object_file = nil if report.is_a?(Pathname) @source_file = report if report.extname.casecmp('.jrxml').zero? || report.extname.casecmp('.xml').zero? @object_file = report if report.extname.casecmp('.jasper').zero? elsif report.is_a?(String) hash = Digest::SHA256.hexdigest(report) @source_file = Pathname.new([:tmp_dir] || '/tmp').join('report-' + hash + '.jrxml') File.open(@source_file, 'wb') do |f| f.write(report) end end if locale = .delete(:locale) locale_parts = locale.split('-')[0..2] _Locale = Rjb.import('java.util.Locale') @parameters['REPORT_LOCALE'] = _Locale.new(*locale_parts) end @object_file ||= @source_file.dirname.join(@source_file.basename.to_s + '.jasper') unless @object_file.is_a?(Pathname) raise ArgumentError, 'An object must be given at least' end end |
Instance Attribute Details
#object_file ⇒ Object (readonly)
Returns the value of attribute object_file.
27 28 29 |
# File 'lib/beardley/report.rb', line 27 def object_file @object_file end |
#source_file ⇒ Object (readonly)
Returns the value of attribute source_file.
27 28 29 |
# File 'lib/beardley/report.rb', line 27 def source_file @source_file end |
Instance Method Details
#compile(xml_path, compiled_path) ⇒ Object
110 111 112 113 114 115 |
# File 'lib/beardley/report.rb', line 110 def compile(xml_path, compiled_path) if File.exist?(xml_path) _JasperCompileManager = Rjb.import('net.sf.jasperreports.engine.JasperCompileManager') _JasperCompileManager.compileReportToFile(xml_path, compiled_path) end end |
#to_csv(*args) ⇒ Object
Export report to CSV with given datasource
75 76 77 |
# File 'lib/beardley/report.rb', line 75 def to_csv(*args) to(:csv, *args) end |
#to_docx(*args) ⇒ Object
Export report to DOCX with given datasource
80 81 82 |
# File 'lib/beardley/report.rb', line 80 def to_docx(*args) to(:docx, *args) end |
#to_file(format, *args) ⇒ Object
Generic method to export to some format like ODT and DOCX as file in the given place
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/beardley/report.rb', line 90 def to_file(format, *args) = (args) datasource = args[0] path = [:path] || File.join(Dir.tmpdir, "beardley_#{Time.now.to_i.to_s(36)}_#{rand(100_000_000_000).to_s(36)}.#{format}") if format == :pdf _JasperPrint = Rjb.import('net.sf.jasperreports.engine.JasperPrint') _JasperExportManager = Rjb.import('net.sf.jasperreports.engine.JasperExportManager') _JasperExportManager._invoke('exportReportToPdfFile', 'Lnet.sf.jasperreports.engine.JasperPrint;Ljava.lang.String;', prepare(datasource), Rjb.import('java.lang.String').new(path.to_s)) elsif Beardley.exporters[format] exporter = Beardley.with_warnings { Rjb.import(Beardley.exporters[format]) }.new _JRExporterParameter = Rjb.import('net.sf.jasperreports.engine.JRExporterParameter') exporter.setParameter(_JRExporterParameter.JASPER_PRINT, prepare(datasource)) exporter.setParameter(_JRExporterParameter.OUTPUT_FILE_NAME, path.to_s) exporter.exportReport else raise "Invalid export format: #{format.inspect}" end path end |
#to_ods(*args) ⇒ Object
Export report to ODS with given datasource
70 71 72 |
# File 'lib/beardley/report.rb', line 70 def to_ods(*args) to(:ods, *args) end |
#to_odt(*args) ⇒ Object
Export report to ODT with given datasource
65 66 67 |
# File 'lib/beardley/report.rb', line 65 def to_odt(*args) to(:odt, *args) end |
#to_pdf(*args) ⇒ Object
Export report to PDF with given datasource
56 57 58 59 60 61 62 |
# File 'lib/beardley/report.rb', line 56 def to_pdf(*args) = (args) datasource = args[0] _JasperPrint = Rjb.import('net.sf.jasperreports.engine.JasperPrint') _JasperExportManager = Rjb.import('net.sf.jasperreports.engine.JasperExportManager') _JasperExportManager._invoke('exportReportToPdf', 'Lnet.sf.jasperreports.engine.JasperPrint;', prepare(datasource)) end |
#to_xlsx(*args) ⇒ Object
Export report to XLSX with given datasource
85 86 87 |
# File 'lib/beardley/report.rb', line 85 def to_xlsx(*args) to(:xlsx, *args) end |