Class: Invoicing::CombinedPdfGenerator

Inherits:
BaseService show all
Defined in:
app/services/invoicing/combined_pdf_generator.rb

Overview

Service object: combined pdf generator.

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

#initialize(options = {}) ⇒ CombinedPdfGenerator

Returns a new instance of CombinedPdfGenerator.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :invoice_generator (Invoicing::PdfGenerator)

    generator used for each invoice PDF

  • :output_to_file (Boolean)

    write the combined PDF to a file

  • :output_to_file_path (String)

    path to write the combined PDF to

  • :logger (Logger)

    custom logger (defaults to Rails.logger)



23
24
25
26
# File 'app/services/invoicing/combined_pdf_generator.rb', line 23

def initialize(options = {})
  @invoice_generator = options[:invoice_generator] || Invoicing::PdfGenerator.new(options)
  super
end

Instance Attribute Details

#invoice_generatorObject (readonly)

Returns the value of attribute invoice_generator.



16
17
18
# File 'app/services/invoicing/combined_pdf_generator.rb', line 16

def invoice_generator
  @invoice_generator
end

Instance Method Details

#process(invoice, invoice_options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/services/invoicing/combined_pdf_generator.rb', line 28

def process(invoice, invoice_options = {})
  invoice_options = invoice_options.reverse_merge({ output_to_file: true })
  combined_pdf = invoice_generator.process(invoice, {})

  result_hsh = { options: invoice_options, file_name: invoice.file_name }
  if invoice_options[:output_to_file]
    result_hsh[:pdf_file_path] = invoice_options[:output_to_file_path] || File.join(Rails.application.config.x.temp_storage_path.to_s, result_hsh[:file_name])
    combined_pdf.save result_hsh[:pdf_file_path]
  else # Stream
    result_hsh[:pdf_data] = combined_pdf.to_pdf
  end
  Result.new(**result_hsh)
end