class Puppet::Parser::TemplateWrapper

A simple wrapper for templates, so they don't have full access to the scope objects.

@api private

Public Class Methods

new(scope) click to toggle source
   # File lib/puppet/parser/templatewrapper.rb
14 def initialize(scope)
15   @__scope__ = scope
16 end

Public Instance Methods

all_tags() click to toggle source

@return [Array<String>] All the defined tags @api public

   # File lib/puppet/parser/templatewrapper.rb
59 def all_tags
60   scope.catalog.tags
61 end
classes() click to toggle source

@return [Array<String>] The list of defined classes @api public

   # File lib/puppet/parser/templatewrapper.rb
47 def classes
48   scope.catalog.classes
49 end
file() click to toggle source

@return [String] The full path name of the template that is being executed @api public

   # File lib/puppet/parser/templatewrapper.rb
20 def file
21   @__file__
22 end
file=(filename) click to toggle source

@api private

   # File lib/puppet/parser/templatewrapper.rb
64 def file=(filename)
65   @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment)
66   unless @__file__
67     raise Puppet::ParseError, _("Could not find template '%{filename}'") % { filename: filename }
68   end
69 end
has_variable?(name) click to toggle source

Should return true if a variable is defined, false if it is not @api public

   # File lib/puppet/parser/templatewrapper.rb
41 def has_variable?(name)
42   scope.include?(name.to_s)
43 end
result(string = nil) click to toggle source

@api private

   # File lib/puppet/parser/templatewrapper.rb
72 def result(string = nil)
73   if string
74     template_source = "inline template"
75   else
76     string = Puppet::FileSystem.read_preserve_line_endings(@__file__)
77     template_source = @__file__
78   end
79 
80   # Expose all the variables in our scope as instance variables of the
81   # current object, making it possible to access them without conflict
82   # to the regular methods.
83   escaped_template_source = template_source.gsub(/%/, '%%')
84   benchmark(:debug, _("Bound template variables for %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do
85     scope.to_hash.each do |name, value|
86       realname = name.gsub(/[^\w]/, "_")
87       instance_variable_set("@#{realname}", value)
88     end
89   end
90 
91   result = nil
92   benchmark(:debug, _("Interpolated template %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do
93     template = Puppet::Util.create_erb(string)
94     template.filename = @__file__
95     result = template.result(binding)
96   end
97 
98   result
99 end
scope() click to toggle source

@return [Puppet::Parser::Scope] The scope in which the template is evaluated @api public

   # File lib/puppet/parser/templatewrapper.rb
26 def scope
27   @__scope__
28 end
tags() click to toggle source

@return [Array<String>] The tags defined in the current scope @api public

   # File lib/puppet/parser/templatewrapper.rb
53 def tags
54   raise NotImplementedError, "Call 'all_tags' instead."
55 end
to_s() click to toggle source
    # File lib/puppet/parser/templatewrapper.rb
101 def to_s
102   "template[#{(@__file__ ? @__file__ : "inline")}]"
103 end

Private Instance Methods

script_line() click to toggle source

Find which line in the template (if any) we were called from. @return [String] the line number @api private

   # File lib/puppet/parser/templatewrapper.rb
33 def script_line
34   identifier = Regexp.escape(@__file__ || "(erb)")
35   (caller.find { |l| l =~ /#{identifier}:/ }||"")[/:(\d+):/,1]
36 end