Class: Kdeploy::Template
- Inherits:
-
Object
- Object
- Kdeploy::Template
- Defined in:
- lib/kdeploy/template.rb
Overview
ERB template management class
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#variables ⇒ Object
readonly
Returns the value of attribute variables.
Instance Method Summary collapse
-
#exist? ⇒ Boolean
Check if template file exists.
-
#initialize(template_path, variables = {}) ⇒ Template
constructor
A new instance of Template.
-
#mtime ⇒ Time
Get template modification time.
-
#render(additional_vars = {}) ⇒ String
Render template with variables.
-
#render_to_file(output_path, additional_vars = {}) ⇒ Object
Render and save to file.
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
#content ⇒ Object (readonly)
Returns the value of attribute content.
6 7 8 |
# File 'lib/kdeploy/template.rb', line 6 def content @content end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
6 7 8 |
# File 'lib/kdeploy/template.rb', line 6 def path @path end |
#variables ⇒ Object (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
48 49 50 |
# File 'lib/kdeploy/template.rb', line 48 def exist? File.exist?(@path) end |
#mtime ⇒ Time
Get template modification time
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
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.}" end |
#render_to_file(output_path, additional_vars = {}) ⇒ Object
Render and save to file
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 |