Class: GemHadar::TemplateCompiler

Inherits:
Object
  • Object
show all
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.

Examples:

Compiling a template file

# Set up template compiler
compiler = GemHadar::TemplateCompiler.new do
  name    'my_template'
  version '1.0.0'
end
# Template (template.erb): Contains ERB placeholders for name and version.
#   Name: <%= name %>
#   Version: <%= version %>
#   Description: This is a template for <%= name %> v<%= version %>

# Compile the template.
compiler.compile('template.erb', 'output.txt')

# Result (output.txt): The rendered output with interpolated values.
#   Name: my_template
#   Version: 1.0.0
#   Description: This is a template for my_template v1.0.0

Instance Method Summary collapse

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.

Parameters:

  • block (Proc)

    the block to be evaluated for configuring the template compiler



37
38
39
40
41
# File 'lib/gem_hadar/template_compiler.rb', line 37

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.

Parameters:

  • id (Symbol)

    the name of the method being called

  • a (Array)

    the arguments passed to the method

  • b (Proc)

    the block passed to the method

Returns:

  • (Object)

    the value associated with the method name if retrieving, otherwise delegates to super



77
78
79
80
81
82
83
84
85
# File 'lib/gem_hadar/template_compiler.rb', line 77

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.

Parameters:

  • src (String)

    the path to the source ERB template file

  • dst (String)

    the path to the destination file where the rendered content will be written



53
54
55
56
57
58
59
60
# File 'lib/gem_hadar/template_compiler.rb', line 53

def compile(src, dst)
  template = File.read(src)
  File.open(dst, 'w') do |output|
    erb = ERB.new(template, trim_mode: ?-)
    erb.filename = src.to_s
    output.write erb.result binding
  end
end