Class: Puppet::Parser::TemplateWrapper
- Includes:
- Util
- Defined in:
- lib/vendor/puppet/parser/templatewrapper.rb
Constant Summary
Constants included from Util
Util::AbsolutePathPosix, Util::AbsolutePathWindows
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
- #scope ⇒ Object
-
#string ⇒ Object
Returns the value of attribute string.
Instance Method Summary collapse
-
#all_tags ⇒ Object
Allow templates to access the all the defined tags.
-
#classes ⇒ Object
Allow templates to access the defined classes.
-
#has_variable?(name) ⇒ Boolean
Should return true if a variable is defined, false if it is not.
-
#initialize(scope) ⇒ TemplateWrapper
constructor
A new instance of TemplateWrapper.
-
#method_missing(name, *args) ⇒ Object
Ruby treats variables like methods, so we used to expose variables within scope to the ERB code via method_missing.
- #result(string = nil) ⇒ Object
- #script_line ⇒ Object
-
#tags ⇒ Object
Allow templates to access the tags defined in the current scope.
- #to_s ⇒ Object
Methods included from Util
absolute_path?, activerecord_version, benchmark, binread, chuser, classproxy, #execfail, #execpipe, execute, execute_posix, execute_windows, logmethods, memory, path_to_uri, proxy, replace_file, safe_posix_fork, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, uri_to_path, wait_for_output, which, withumask
Methods included from Util::POSIX
#get_posix_field, #gid, #idfield, #methodbyid, #methodbyname, #search_posix_field, #uid
Constructor Details
#initialize(scope) ⇒ TemplateWrapper
Returns a new instance of TemplateWrapper.
13 14 15 |
# File 'lib/vendor/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
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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 58 def method_missing(name, *args) value = scope.lookupvar(name.to_s,:file => file,:line => script_line) if value != :undefined return value 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,script_line) end end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 8 def file @file end |
#scope ⇒ Object
17 18 19 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 17 def scope @__scope__ end |
#string ⇒ Object
Returns the value of attribute string.
9 10 11 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 9 def string @string end |
Instance Method Details
#all_tags ⇒ Object
Allow templates to access the all the defined tags
42 43 44 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 42 def scope.catalog. end |
#classes ⇒ Object
Allow templates to access the defined classes
32 33 34 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 32 def classes scope.catalog.classes end |
#has_variable?(name) ⇒ Boolean
Should return true if a variable is defined, false if it is not
27 28 29 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 27 def has_variable?(name) scope.lookupvar(name.to_s, :file => file, :line => script_line) != :undefined end |
#result(string = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 80 def result(string = nil) if string self.string = string template_source = "inline template" else 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 { |name, value| if name.kind_of?(String) realname = name.gsub(/[^\w]/, "_") else realname = name end instance_variable_set("@#{realname}", value) } end result = nil benchmark(:debug, "Interpolated template #{template_source}") do template = ERB.new(self.string, 0, "-") template.filename = file result = template.result(binding) end result end |
#script_line ⇒ Object
21 22 23 24 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 21 def script_line # find which line in the template (if any) we were called from (caller.find { |l| l =~ /#{file}:/ }||"")[/:(\d+):/,1] end |
#tags ⇒ Object
Allow templates to access the tags defined in the current scope
37 38 39 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 37 def scope. end |
#to_s ⇒ Object
112 113 114 |
# File 'lib/vendor/puppet/parser/templatewrapper.rb', line 112 def to_s "template[#{(file ? file : "inline")}]" end |