Class: Kdeploy::Template

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

Overview

ERB template management class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Template.



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

def initialize(template_path, variables = {})
  @path = template_path
  @variables = variables
  @content = load_template
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/kdeploy/template.rb', line 6

def content
  @content
end

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/kdeploy/template.rb', line 6

def path
  @path
end

#variablesObject (readonly)

Returns the value of attribute variables.



6
7
8
# File 'lib/kdeploy/template.rb', line 6

def variables
  @variables
end

Instance Method Details

#exist?Boolean

Check if template file exists

Returns:

  • (Boolean)

    True if template exists



48
49
50
# File 'lib/kdeploy/template.rb', line 48

def exist?
  File.exist?(@path)
end

#mtimeTime

Get template modification time

Returns:

  • (Time)

    Template file mtime



54
55
56
# File 'lib/kdeploy/template.rb', line 54

def mtime
  File.mtime(@path) if exist?
end

#render(additional_vars = {}) ⇒ String

Render template with variables

Parameters:

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

    Additional variables to merge

Returns:

  • (String)

    Rendered content



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/kdeploy/template.rb', line 17

def render(additional_vars = {})
  all_vars = @variables.merge(additional_vars)

  # Create binding with variables
  template_binding = create_binding(all_vars)

  # Render ERB template
  erb = ERB.new(@content, trim_mode: '-')
  erb.result(template_binding)
rescue StandardError => e
  raise TemplateError, "Failed to render template #{@path}: #{e.message}"
end

#render_to_file(output_path, additional_vars = {}) ⇒ Object

Render and save to file

Parameters:

  • output_path (String)

    Output file path

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

    Additional variables



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kdeploy/template.rb', line 33

def render_to_file(output_path, additional_vars = {})
  rendered_content = render(additional_vars)

  # Ensure output directory exists
  FileUtils.mkdir_p(File.dirname(output_path))

  # Write rendered content
  File.write(output_path, rendered_content)

  KdeployLogger.info("Template rendered to: #{output_path}")
  output_path
end