Class: ReportNative

Inherits:
Object
  • Object
show all
Defined in:
lib/majorsilence_reporting/report_native.rb

Constant Summary collapse

VALID_TYPES =
%w[pdf csv xlsx xlsx_table xml rtf tif tifb html mht].freeze

Instance Method Summary collapse

Constructor Details

#initialize(fns, report_path) ⇒ ReportNative

Returns a new instance of ReportNative.



116
117
118
119
120
121
122
# File 'lib/majorsilence_reporting/report_native.rb', line 116

def initialize(fns, report_path)
  @fns               = fns
  @report_path       = report_path
  @connection_string = nil
  @parameters        = {}
  @data_sets         = {}
end

Instance Method Details

#add_data(dataset_name, rows) ⇒ Object

Supply in-memory data for a named dataset, bypassing any database query. dataset_name - name of the DataSet element in the RDL (e.g. "Data") rows - Array of Hashes mapping field name => value (all values as strings). Field names must match the values in the RDL.

SkipDatabaseSchemaValidation is set automatically when dataset rows are present, so no DB connection is needed at parse or render time.

Example:

rpt.add_data('Data', [
{ 'Product' => 'Chai',  'Amount' => '1250.00' },
{ 'Product' => 'Chang', 'Amount' =>  '980.50' },
])


149
150
151
# File 'lib/majorsilence_reporting/report_native.rb', line 149

def add_data(dataset_name, rows)
  @data_sets[dataset_name] = rows
end

#export(type, export_path) ⇒ Object

Render the report and save it to export_path. type - output format (defaults to "pdf") export_path - destination file path



156
157
158
159
160
161
162
# File 'lib/majorsilence_reporting/report_native.rb', line 156

def export(type, export_path)
  fmt = VALID_TYPES.include?(type) ? type : 'pdf'
  with_handle do |h|
    ret = @fns[:rdl_report_render_file].call(h, c_str(export_path), c_str(fmt))
    raise_last_error('rdl_report_render_file') unless ret.zero?
  end
end

#export_to_memory(type) ⇒ Object

Render the report and return the output. Returns a binary String for all formats. type - output format (defaults to "pdf")



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/majorsilence_reporting/report_native.rb', line 167

def export_to_memory(type)
  fmt = VALID_TYPES.include?(type) ? type : 'pdf'
  tmp = Tempfile.new(['rdlnative', ".#{fmt}"], binmode: true)
  tmp_path = tmp.path
  tmp.close
  begin
    export(fmt, tmp_path)
    File.binread(tmp_path)
  ensure
    File.delete(tmp_path) if File.exist?(tmp_path)
  end
end

#set_connection_string(connection_string) ⇒ Object

Override the connection string defined in the RDL.



132
133
134
# File 'lib/majorsilence_reporting/report_native.rb', line 132

def set_connection_string(connection_string)
  @connection_string = connection_string
end

#set_parameter(name, value) ⇒ Object

Set a report parameter value. name - parameter name as declared in the RDL value - parameter value (string)



127
128
129
# File 'lib/majorsilence_reporting/report_native.rb', line 127

def set_parameter(name, value)
  @parameters[name] = value
end