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

compiler = GemHadar::TemplateCompiler.new do |t|
  t.name = 'my_template'
  t.version = '1.0.0'
end
compiler.compile('template.erb', 'output.txt')

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



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.

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



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.

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



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