summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/templatewrapper.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-04 18:24:24 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-10-04 18:24:24 +0000
commit28cee40689440388994a4768bd301ae32c8ecc05 (patch)
treec865ab44f4c9247052cf83de16ffc8ebe8b15e54 /lib/puppet/parser/templatewrapper.rb
parente0e291332bd4676962a28c7b220ae5c5e9651c0a (diff)
downloadpuppet-28cee40689440388994a4768bd301ae32c8ecc05.tar.gz
puppet-28cee40689440388994a4768bd301ae32c8ecc05.tar.xz
puppet-28cee40689440388994a4768bd301ae32c8ecc05.zip
Merging the changes from the override-refactor branch. This is a significant rewrite of the parser, but it has little affect on the rest of the code tree.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1726 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/parser/templatewrapper.rb')
-rw-r--r--lib/puppet/parser/templatewrapper.rb58
1 files changed, 58 insertions, 0 deletions
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
new file mode 100644
index 000000000..62b45852b
--- /dev/null
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -0,0 +1,58 @@
+# A simple wrapper for templates, so they don't have full access to
+# the scope objects.
+class Puppet::Parser::TemplateWrapper
+ attr_accessor :scope, :file
+ include Puppet::Util
+ Puppet::Util.logmethods(self)
+
+ def initialize(scope, file)
+ @scope = scope
+ if file =~ /^#{File::SEPARATOR}/
+ @file = file
+ else
+ @file = File.join(Puppet[:templatedir], file)
+ end
+
+ unless FileTest.exists?(@file)
+ raise Puppet::ParseError,
+ "Could not find template %s" % file
+ end
+
+ # We'll only ever not have an interpreter in testing, but, eh.
+ if @scope.interp
+ @scope.interp.newfile(@file)
+ end
+ end
+
+ # Ruby treats variables like methods, so we can cheat here and
+ # trap missing vars like they were missing methods.
+ 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 '%s'" % name
+ end
+ end
+
+ def result
+ result = nil
+ benchmark(:debug, "Interpolated template #{@file}") do
+ template = ERB.new(File.read(@file), 0, "-")
+ result = template.result(binding)
+ end
+
+ result
+ end
+
+ def to_s
+ "template[%s]" % @file
+ end
+end
+
+# $Id$