Sha256: 358409454f5f6b6fc3bc363a62fab7e308ec5441d766e4466e7d3b642cd3629a

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

# 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)

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

        # We'll only ever not have an interpreter in testing, but, eh.
        if @scope.interp
            @scope.interp.newfile(@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

# $Id: templatewrapper.rb 2277 2007-03-09 00:48:28Z lutter $

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
puppet-0.23.0 lib/puppet/parser/templatewrapper.rb
puppet-0.22.4 lib/puppet/parser/templatewrapper.rb
puppet-0.23.2 lib/puppet/parser/templatewrapper.rb
puppet-0.23.1 lib/puppet/parser/templatewrapper.rb