Class: LiquidXlsx::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid_xlsx/template.rb

Overview

Main template class for rendering .xlsx files.

Instance Method Summary collapse

Constructor Details

#initialize(template_path, options = {}) ⇒ Template

Returns a new instance of Template.

Parameters:

  • template_path (String)

    path to the .xlsx template

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


8
9
10
11
# File 'lib/liquid_xlsx/template.rb', line 8

def initialize(template_path, options = {})
  @template_path = template_path
  @options = options
end

Instance Method Details

#render(data) ⇒ String

Render template and return binary content.

Parameters:

  • data (Hash, Liquid::Drop, #to_liquid)

    data to render (Hash/Drop, or any object whose #to_liquid returns one)

Returns:

  • (String)

    binary .xlsx content



31
32
33
34
35
36
37
38
39
# File 'lib/liquid_xlsx/template.rb', line 31

def render(data)
  package = render_package(data)
  package.to_binary
rescue LiquidXlsx::Error
  raise
rescue Zip::Error, Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::EISDIR => e
  raise OutputWriteError,
        "Failed to generate binary output: #{e.class}: #{e.message}"
end

#render_to_file(data, output_path) ⇒ Object

Render template and save to file.

Parameters:

  • data (Hash, Liquid::Drop, #to_liquid)

    data to render (Hash/Drop, or any object whose #to_liquid returns one)

  • output_path (String)


17
18
19
20
21
22
23
24
25
# File 'lib/liquid_xlsx/template.rb', line 17

def render_to_file(data, output_path)
  package = render_package(data)
  package.write(output_path)
rescue LiquidXlsx::Error
  raise
rescue Zip::Error, Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::EISDIR => e
  raise OutputWriteError,
        "Failed to write output '#{output_path}': #{e.class}: #{e.message}"
end