summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/templatewrapper.rb
diff options
context:
space:
mode:
authorLuke Kanies <luke@madstop.com>2008-12-02 16:26:54 -0600
committerLuke Kanies <luke@madstop.com>2008-12-02 16:26:54 -0600
commit99a9b5a045af6f1c68619792a45603cbe450652d (patch)
tree579963d464c0529d4e9250f937d495e415f1e867 /lib/puppet/parser/templatewrapper.rb
parentf73e13e7464edab73443857d628602b89361c220 (diff)
parent278bfe83015312292360f727d6532a143610db0d (diff)
downloadpuppet-99a9b5a045af6f1c68619792a45603cbe450652d.tar.gz
puppet-99a9b5a045af6f1c68619792a45603cbe450652d.tar.xz
puppet-99a9b5a045af6f1c68619792a45603cbe450652d.zip
Merge branch '0.24.x'
Conflicts: bin/puppetca lib/puppet/type/group.rb lib/puppet/type/tidy.rb lib/puppet/util/settings.rb Also edited the following files so tests will pass: lib/puppet/type/component.rb spec/unit/ssl/certificate_request.rb spec/unit/type/computer.rb spec/unit/type/mcx.rb spec/unit/type/resources.rb spec/unit/util/settings.rb spec/unit/util/storage.rb test/ral/type/zone.rb
Diffstat (limited to 'lib/puppet/parser/templatewrapper.rb')
-rw-r--r--lib/puppet/parser/templatewrapper.rb52
1 files changed, 30 insertions, 22 deletions
diff --git a/lib/puppet/parser/templatewrapper.rb b/lib/puppet/parser/templatewrapper.rb
index 036f6604e..55c7745ba 100644
--- a/lib/puppet/parser/templatewrapper.rb
+++ b/lib/puppet/parser/templatewrapper.rb
@@ -1,33 +1,18 @@
# A simple wrapper for templates, so they don't have full access to
# the scope objects.
class Puppet::Parser::TemplateWrapper
- attr_accessor :scope, :file
+ attr_accessor :scope, :file, :string
include Puppet::Util
Puppet::Util.logmethods(self)
- def initialize(scope, filename)
+ def initialize(scope)
@__scope__ = scope
- @__file__ = Puppet::Module::find_template(filename, scope.compiler.environment)
-
- unless FileTest.exists?(file)
- raise Puppet::ParseError,
- "Could not find template %s" % file
- end
-
- # We'll only ever not have a parser in testing, but, eh.
- if scope.parser
- scope.parser.watch_file(file)
- end
end
def scope
@__scope__
end
- def file
- @__file__
- end
-
# Should return true if a variable is defined, false if it is not
def has_variable?(name)
if scope.lookupvar(name.to_s, false) != :undefined
@@ -77,11 +62,34 @@ class Puppet::Parser::TemplateWrapper
end
end
- def result
+ def file=(filename)
+ @file = Puppet::Module::find_template(filename, scope.compiler.environment)
+
+ unless FileTest.exists?(file)
+ raise Puppet::ParseError,
+ "Could not find template %s" % file
+ end
+
+ # We'll only ever not have a parser in testing, but, eh.
+ if scope.parser
+ scope.parser.watch_file(file)
+ end
+
+ @string = File.read(file)
+ end
+
+ 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 #{file}") do
+ benchmark(:debug, "Bound template variables for #{template_source}") do
scope.to_hash.each { |name, value|
if name.kind_of?(String)
realname = name.gsub(/[^\w]/, "_")
@@ -93,8 +101,8 @@ class Puppet::Parser::TemplateWrapper
end
result = nil
- benchmark(:debug, "Interpolated template #{file}") do
- template = ERB.new(File.read(file), 0, "-")
+ benchmark(:debug, "Interpolated template #{template_source}") do
+ template = ERB.new(self.string, 0, "-")
result = template.result(binding)
end
@@ -102,7 +110,7 @@ class Puppet::Parser::TemplateWrapper
end
def to_s
- "template[%s]" % file
+ "template[%s]" % (file ? file : "inline")
end
end