summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/templatewrapper.rb
blob: 7a8f741562a0773257bab81b3b8c13f78deb887e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# A simple wrapper for templates, so they don't have full access to
# the scope objects.
class Puppet::Parser::TemplateWrapper
    attr_accessor :scope, :file
    include Puppet::Util
    Puppet::Util.logmethods(self)

    def initialize(scope, file)
        @scope = scope
        @file = Puppet::Module::find_template(file, @scope.compiler.environment)

        unless FileTest.exists?(@file)
            raise Puppet::ParseError,
                "Could not find template %s" % file
        end

        # We'll only ever not have a parser in testing, but, eh.
        if @scope.parser
            @scope.parser.watch_file(@file)
        end
    end

    # Ruby treats variables like methods, so we can cheat here and
    # trap missing vars like they were missing methods.
    def method_missing(name, *args)
        # We have to tell lookupvar to return :undefined to us when
        # appropriate; otherwise it converts to "".
        value = @scope.lookupvar(name.to_s, false)
        if value != :undefined
            return value
        else
            # Just throw an error immediately, instead of searching for
            # other missingmethod things or whatever.
            raise Puppet::ParseError,
                "Could not find value for '%s'" % name
        end
    end

    def result
        result = nil
        benchmark(:debug, "Interpolated template #{@file}") do
            template = ERB.new(File.read(@file), 0, "-")
            result = template.result(binding)
        end

        result
    end

    def to_s
        "template[%s]" % @file
    end
end