Class: Autoproj::Jenkins::TemplateRenderingContext Private

Inherits:
BasicObject
Defined in:
lib/autoproj/jenkins/render_template.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

A rendering context for ERB templates

It ensures that the templates are restricted to use the parameters that are provided to them

Instance Method Summary collapse

Constructor Details

#initialize(template_path, allow_unused: false, indent: 0, **parameters) ⇒ TemplateRenderingContext

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of TemplateRenderingContext.



12
13
14
15
16
17
18
# File 'lib/autoproj/jenkins/render_template.rb', line 12

def initialize(template_path, allow_unused: false, indent: 0, **parameters)
    @allow_unused = allow_unused
    @indent = indent
    @template_path = template_path
    @parameters = parameters
    @used_parameters = ::Set.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
57
58
59
60
61
62
# File 'lib/autoproj/jenkins/render_template.rb', line 54

def method_missing(m)
    if @parameters.has_key?(m)
        result = @parameters.fetch(m)
        @used_parameters << m
        result
    else
        ::Kernel.raise ::Autoproj::Jenkins::UnknownTemplateParameter, "#{m} is not a known template parameter"
    end
end

Instance Method Details

#__verifyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



20
21
22
23
24
25
26
27
28
# File 'lib/autoproj/jenkins/render_template.rb', line 20

def __verify
    return if @allow_unused

    unused_parameters = @parameters.keys.to_set -
        @used_parameters
    if !unused_parameters.empty?
        ::Kernel.raise ::Autoproj::Jenkins::UnusedTemplateParameters, "#{unused_parameters.size} unused parameters: #{unused_parameters.map(&:to_s).sort.join(", ")}"
    end
end

#escape_to_groovy(content) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
52
# File 'lib/autoproj/jenkins/render_template.rb', line 48

def escape_to_groovy(content)
    content.
        gsub("\n", "\\n").
        gsub("\"", "\\\"")
end

#read_and_escape_file(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
46
# File 'lib/autoproj/jenkins/render_template.rb', line 43

def read_and_escape_file(path)
    file_data = (::Autoproj::Jenkins.template_path + path).read
    escape_to_groovy(file_data)
end

#render_template(path, escape: false, indent: 0, **parameters) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/autoproj/jenkins/render_template.rb', line 30

def render_template(path, escape: false, indent: 0, **parameters)
    result = ::Autoproj::Jenkins.render_template(path, template_path: @template_path, indent: indent, **parameters)
    @used_parameters.merge(parameters.keys)
    if escape
        result = escape_to_groovy(result)
    end

    indent_string = " " * indent
    # We assume that the ERB tag for render_template is indented
    # properly
    result.split("\n").join("\n#{indent_string}")
end