From 0136cdb35999a5ce8c3885bdcb5ad308114efec9 Mon Sep 17 00:00:00 2001 From: mh Date: Wed, 20 Mar 2013 21:37:51 +0100 Subject: init of templatewlv --- lib/puppet/parser/templatewrapperwlv.rb | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 lib/puppet/parser/templatewrapperwlv.rb (limited to 'lib/puppet/parser/templatewrapperwlv.rb') diff --git a/lib/puppet/parser/templatewrapperwlv.rb b/lib/puppet/parser/templatewrapperwlv.rb new file mode 100644 index 0000000..f1753e1 --- /dev/null +++ b/lib/puppet/parser/templatewrapperwlv.rb @@ -0,0 +1,39 @@ +# A wrapper for templates, that allows you to additionally define +# local variables +class Puppet::Parser::TemplateWrapperWlv < Puppet::Parser::TemplateWrapper + attr_reader :local_vars + def initialize(scope, local_vars) + super(scope) + @local_vars = local_vars + end + + # Should return true if a variable is defined, false if it is not + def has_variable?(name) + super(name) || local_vars.keys.include?(name.to_s) + end + + def method_missing(name, *args) + if local_vars.keys.include?(n=name.to_s) + local_vars[n] + else + super(name, *args) + end + end + + def result(string = nil) + # 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 local template variables for #{@__file__}") do + local_vars.each do |name, value| + if name.kind_of?(String) + realname = name.gsub(/[^\w]/, "_") + else + realname = name + end + instance_variable_set("@#{realname}", value) + end + end + super(string) + end +end -- cgit v1.2.3