Class: HybiscusPdfReport::ReportBuilder
- Inherits:
-
Object
- Object
- HybiscusPdfReport::ReportBuilder
- Defined in:
- lib/hybiscus_pdf_report/report_builder.rb
Overview
Base class for building PDF reports with JSON templates
This class allows users to create custom report builders by inheriting from it. It provides a simple way to generate JSON structures for the Hybiscus API using ERB templates.
Usage:
class InvoiceReport < HybiscusPdfReport::ReportBuilder
def initialize(invoice:, **)
@invoice = invoice
super(report_name: "Invoice Report", **)
end
end
report = InvoiceReport.new(invoice: my_invoice)
json_data = report.generate
Constant Summary collapse
- DEFAULT_TEMPLATE_DIR =
File.dirname(__FILE__)
Instance Attribute Summary collapse
-
#report_name ⇒ Object
readonly
Returns the value of attribute report_name.
-
#template_dir ⇒ Object
readonly
Returns the value of attribute template_dir.
Instance Method Summary collapse
-
#generate ⇒ Object
Main method to generate the report JSON Returns a JSON string that can be sent to the Hybiscus API.
-
#initialize(report_name: nil, template_dir: nil, **template_params) ⇒ ReportBuilder
constructor
A new instance of ReportBuilder.
- #load_configuration_first? ⇒ Boolean
-
#template_name ⇒ Object
Returns the template filename (can be overridden in subclasses).
-
#template_path ⇒ Object
Returns the full path to the template file.
Constructor Details
#initialize(report_name: nil, template_dir: nil, **template_params) ⇒ ReportBuilder
Returns a new instance of ReportBuilder.
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 27 def initialize(report_name: nil, template_dir: nil, **template_params) # Set the report name - use provided name or derive from class name @report_name = report_name || derive_report_name @template_dir = template_dir || DEFAULT_TEMPLATE_DIR # Dynamically set all parameters as instance variables template_params.each do |key, value| instance_variable_set("@#{key}", value) end end |
Instance Attribute Details
#report_name ⇒ Object (readonly)
Returns the value of attribute report_name.
25 26 27 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 25 def report_name @report_name end |
#template_dir ⇒ Object (readonly)
Returns the value of attribute template_dir.
25 26 27 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 25 def template_dir @template_dir end |
Instance Method Details
#generate ⇒ Object
Main method to generate the report JSON Returns a JSON string that can be sent to the Hybiscus API
40 41 42 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 40 def generate render_json end |
#load_configuration_first? ⇒ Boolean
54 55 56 57 58 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 54 def load_configuration_first? # By default, we assume the report doesn't require pre-loading configuration. # This can be overridden in subclasses if configuration loading is needed. false end |
#template_name ⇒ Object
Returns the template filename (can be overridden in subclasses)
50 51 52 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 50 def template_name "#{underscore(class_name)}.json.erb" end |
#template_path ⇒ Object
Returns the full path to the template file
45 46 47 |
# File 'lib/hybiscus_pdf_report/report_builder.rb', line 45 def template_path File.join(template_dir, template_name) end |