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 13 def initialize(scope) 14 @__scope__ = scope 15 end
Public Instance Methods
classes()
click to toggle source
@return [Array<String>] The list of defined classes @api public
# File lib/puppet/parser/templatewrapper.rb 46 def classes 47 scope.catalog.classes 48 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 19 def file 20 @__file__ 21 end
file=(filename)
click to toggle source
@api private
# File lib/puppet/parser/templatewrapper.rb 63 def file=(filename) 64 @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment) 65 unless @__file__ 66 raise Puppet::ParseError, _("Could not find template '%{filename}'") % { filename: filename } 67 end 68 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 40 def has_variable?(name) 41 scope.include?(name.to_s) 42 end
result(string = nil)
click to toggle source
@api private
# File lib/puppet/parser/templatewrapper.rb 71 def result(string = nil) 72 if string 73 template_source = "inline template" 74 else 75 string = Puppet::FileSystem.read_preserve_line_endings(@__file__) 76 template_source = @__file__ 77 end 78 79 # Expose all the variables in our scope as instance variables of the 80 # current object, making it possible to access them without conflict 81 # to the regular methods. 82 escaped_template_source = template_source.gsub(/%/, '%%') 83 benchmark(:debug, _("Bound template variables for %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do 84 scope.to_hash.each do |name, value| 85 realname = name.gsub(/[^\w]/, "_") 86 instance_variable_set("@#{realname}", value) 87 end 88 end 89 90 result = nil 91 benchmark(:debug, _("Interpolated template %{template_source} in %%{seconds} seconds") % { template_source: escaped_template_source }) do 92 template = ERB.new(string, 0, "-") 93 template.filename = @__file__ 94 result = template.result(binding) 95 end 96 97 result 98 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 25 def scope 26 @__scope__ 27 end
to_s()
click to toggle source
# File lib/puppet/parser/templatewrapper.rb 100 def to_s 101 "template[#{(@__file__ ? @__file__ : "inline")}]" 102 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 32 def script_line 33 identifier = Regexp.escape(@__file__ || "(erb)") 34 (caller.find { |l| l =~ /#{identifier}:/ }||"")[/:(\d+):/,1] 35 end