Class: Puppet::Parser::TemplateWrapper Private
- Includes:
- Util
- Defined in:
- lib/puppet/parser/templatewrapper.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 simple wrapper for templates, so they don’t have full access to the scope objects.
Constant Summary
Constants included from Util
Util::AbsolutePathPosix, Util::AbsolutePathWindows, Util::DEFAULT_POSIX_MODE, Util::DEFAULT_WINDOWS_MODE
Constants included from Util::POSIX
Util::POSIX::LOCALE_ENV_VARS, Util::POSIX::USER_ENV_VARS
Constants included from Util::SymbolicFileMode
Util::SymbolicFileMode::SetGIDBit, Util::SymbolicFileMode::SetUIDBit, Util::SymbolicFileMode::StickyBit, Util::SymbolicFileMode::SymbolicMode, Util::SymbolicFileMode::SymbolicSpecialToBit
Instance Method Summary collapse
-
#all_tags ⇒ Array<String>
All the defined tags.
-
#classes ⇒ Array<String>
The list of defined classes.
-
#file ⇒ String
The full path name of the template that is being executed.
- #file=(filename) ⇒ Object private
-
#has_variable?(name) ⇒ Boolean
Should return true if a variable is defined, false if it is not.
-
#initialize(scope) ⇒ TemplateWrapper
constructor
private
A new instance of TemplateWrapper.
-
#method_missing(name, *args) ⇒ Object
private
Ruby treats variables like methods, so we used to expose variables within scope to the ERB code via method_missing.
- #result(string = nil) ⇒ Object private
-
#scope ⇒ Puppet::Parser::Scope
The scope in which the template is evaluated.
-
#tags ⇒ Array<String>
The tags defined in the current scope.
- #to_s ⇒ Object private
Methods included from Util
absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, deterministic_rand, execfail, execpipe, execute, exit_on_fail, logmethods, memory, path_to_uri, pretty_backtrace, proxy, replace_file, safe_posix_fork, symbolizehash, thinmark, uri_to_path, which, withenv, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Methods included from Util::SymbolicFileMode
#normalize_symbolic_mode, #symbolic_mode_to_int, #valid_symbolic_mode?
Constructor Details
#initialize(scope) ⇒ TemplateWrapper
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 TemplateWrapper.
13 14 15 |
# File 'lib/puppet/parser/templatewrapper.rb', line 13 def initialize(scope) @__scope__ = scope end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ 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.
Ruby treats variables like methods, so we used to expose variables within scope to the ERB code via method_missing. As per RedMine #1427, though, this means that conflicts between methods in our inheritance tree (Kernel#fork) and variable names (fork => “yes/no”) could arise.
Worse, /new/ conflicts could pop up when a new kernel or object method was added to Ruby, causing templates to suddenly fail mysteriously when Ruby was upgraded.
To ensure that legacy templates using unqualified names work we retain the missing_method definition here until we declare the syntax finally dead.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/puppet/parser/templatewrapper.rb', line 74 def method_missing(name, *args) line_number = script_line if scope.include?(name.to_s) Puppet.deprecation_warning("Variable access via '#{name}' is deprecated. Use '@#{name}' instead. #{to_s}:#{line_number}") return scope[name.to_s, { :file => @__file__, :line => line_number }] else # Just throw an error immediately, instead of searching for # other missingmethod things or whatever. raise Puppet::ParseError.new("Could not find value for '#{name}'", @__file__, line_number) end end |
Instance Method Details
#all_tags ⇒ Array<String>
Returns All the defined tags.
58 59 60 |
# File 'lib/puppet/parser/templatewrapper.rb', line 58 def scope.catalog. end |
#classes ⇒ Array<String>
Returns The list of defined classes.
46 47 48 |
# File 'lib/puppet/parser/templatewrapper.rb', line 46 def classes scope.catalog.classes end |
#file ⇒ String
Returns The full path name of the template that is being executed.
19 20 21 |
# File 'lib/puppet/parser/templatewrapper.rb', line 19 def file @__file__ end |
#file=(filename) ⇒ 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.
87 88 89 90 91 92 93 94 |
# File 'lib/puppet/parser/templatewrapper.rb', line 87 def file=(filename) unless @__file__ = Puppet::Parser::Files.find_template(filename, scope.compiler.environment) raise Puppet::ParseError, "Could not find template '#{filename}'" end # We'll only ever not have a parser in testing, but, eh. scope.known_resource_types.watch_file(@__file__) end |
#has_variable?(name) ⇒ Boolean
Should return true if a variable is defined, false if it is not
40 41 42 |
# File 'lib/puppet/parser/templatewrapper.rb', line 40 def has_variable?(name) scope.include?(name.to_s) end |
#result(string = nil) ⇒ 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.
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/puppet/parser/templatewrapper.rb', line 97 def result(string = nil) if string template_source = "inline template" else string = Puppet::FileSystem.read_preserve_line_endings(@__file__) template_source = @__file__ end # Expose all the variables in our scope as instance variables of the # current object, making it possible to access them without conflict # to the regular methods. benchmark(:debug, "Bound template variables for #{template_source}") do scope.to_hash.each do |name, value| realname = name.gsub(/[^\w]/, "_") instance_variable_set("@#{realname}", value) end end result = nil benchmark(:debug, "Interpolated template #{template_source}") do template = ERB.new(string, 0, "-") template.filename = @__file__ result = template.result(binding) end result end |
#scope ⇒ Puppet::Parser::Scope
Returns The scope in which the template is evaluated.
25 26 27 |
# File 'lib/puppet/parser/templatewrapper.rb', line 25 def scope @__scope__ end |
#tags ⇒ Array<String>
Returns The tags defined in the current scope.
52 53 54 |
# File 'lib/puppet/parser/templatewrapper.rb', line 52 def scope. end |
#to_s ⇒ 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.
125 126 127 |
# File 'lib/puppet/parser/templatewrapper.rb', line 125 def to_s "template[#{(@__file__ ? @__file__ : "inline")}]" end |