Class: Puppet::Parser::TemplateWrapper
- Includes:
- Util
- Defined in:
- lib/puppet/parser/templatewrapper.rb
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
-
#tags ⇒ Object
Allow templates to access the tags defined in the current scope.
- #to_s ⇒ Object
Methods included from Util
activerecord_version, benchmark, chuser, classproxy, #execfail, #execpipe, execute, logmethods, memory, proxy, recmkdir, secure_open, symbolize, symbolizehash, symbolizehash!, synchronize_on, thinmark, #threadlock, 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/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.
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/puppet/parser/templatewrapper.rb', line 57 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 '#{name}'" end end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
8 9 10 |
# File 'lib/puppet/parser/templatewrapper.rb', line 8 def file @file end |
#scope ⇒ Object
17 18 19 |
# File 'lib/puppet/parser/templatewrapper.rb', line 17 def scope @__scope__ end |
#string ⇒ Object
Returns the value of attribute string.
9 10 11 |
# File 'lib/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
41 42 43 |
# File 'lib/puppet/parser/templatewrapper.rb', line 41 def scope.catalog. end |
#classes ⇒ Object
Allow templates to access the defined classes
31 32 33 |
# File 'lib/puppet/parser/templatewrapper.rb', line 31 def classes scope.catalog.classes end |
#has_variable?(name) ⇒ Boolean
Should return true if a variable is defined, false if it is not
22 23 24 25 26 27 28 |
# File 'lib/puppet/parser/templatewrapper.rb', line 22 def has_variable?(name) if scope.lookupvar(name.to_s, false) != :undefined true else false end end |
#result(string = nil) ⇒ Object
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/puppet/parser/templatewrapper.rb', line 81 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, "-") result = template.result(binding) end result end |
#tags ⇒ Object
Allow templates to access the tags defined in the current scope
36 37 38 |
# File 'lib/puppet/parser/templatewrapper.rb', line 36 def scope. end |
#to_s ⇒ Object
112 113 114 |
# File 'lib/puppet/parser/templatewrapper.rb', line 112 def to_s "template[#{(file ? file : "inline")}]" end |