Class: GemHadar::TemplateCompiler
- Inherits:
-
Object
- Object
- GemHadar::TemplateCompiler
- Includes:
- Tins::BlockSelf, Tins::MethodMissingDelegator::DelegatorModule
- Defined in:
- lib/gem_hadar/template_compiler.rb
Overview
A class for compiling ERB template files into their final output representations.
The TemplateCompiler class provides functionality to process ERB templates, substituting placeholders with actual values from a configuration block. It handles the reading of template files, rendering them with the provided context, and writing the resulting content to specified destination files.
Instance Method Summary collapse
-
#compile(src, dst) ⇒ Object
The compile method processes an ERB template file and writes the rendered output to a destination file.
-
#initialize(&block) ⇒ TemplateCompiler
constructor
The initialize method sets up the template compiler instance by evaluating the provided block in the context of the object.
-
#method_missing(id, *a, &b) ⇒ Object
The method_missing method handles dynamic attribute access and assignment.
Constructor Details
#initialize(&block) ⇒ TemplateCompiler
The initialize method sets up the template compiler instance by evaluating the provided block in the context of the object.
25 26 27 28 29 |
# File 'lib/gem_hadar/template_compiler.rb', line 25 def initialize(&block) super block_self(&block) @values = {} instance_eval(&block) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *a, &b) ⇒ Object
The method_missing method handles dynamic attribute access and assignment.
This method intercepts calls to undefined methods on the object, allowing for dynamic retrieval and setting of values through method calls. If a method name corresponds to a key in @values and no arguments are provided, it returns the stored value. If a single argument is provided, it stores the argument under the method name as a key in @values. For all other cases, it delegates the call to the superclass implementation.
65 66 67 68 69 70 71 72 73 |
# File 'lib/gem_hadar/template_compiler.rb', line 65 def method_missing(id, *a, &b) if a.empty? && id && @values.key?(id) @values[id] elsif a.size == 1 @values[id] = a.first else super end end |
Instance Method Details
#compile(src, dst) ⇒ Object
The compile method processes an ERB template file and writes the rendered output to a destination file.
This method reads the content of a source file, treats it as an ERB template, and renders it using the provided binding. The result is then written to a specified destination file, effectively generating a new file based on the template.
41 42 43 44 45 46 47 48 |
# File 'lib/gem_hadar/template_compiler.rb', line 41 def compile(src, dst) template = File.read(src) File.open(dst, 'w') do |output| erb = ERB.new(template, nil, '-') erb.filename = src.to_s output.write erb.result binding end end |