summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/scope.rb
diff options
context:
space:
mode:
authorMarkus Roberts <Markus@reality.com>2010-10-21 17:24:09 -0700
committerNick Lewis <nick@puppetlabs.com>2011-04-12 12:47:31 -0700
commit31f8e660c8f4c0ec01f140322cf7c585144a0888 (patch)
tree115f2b3d2b31c005b4b0de81b74d9195fa6ca495 /lib/puppet/parser/scope.rb
parentd5dc3036eda210f318160e2442f7b65e0995ee23 (diff)
downloadpuppet-31f8e660c8f4c0ec01f140322cf7c585144a0888.tar.gz
puppet-31f8e660c8f4c0ec01f140322cf7c585144a0888.tar.xz
puppet-31f8e660c8f4c0ec01f140322cf7c585144a0888.zip
Refactor en route to #5027 -- remove usestring parameter from lookupvar
The usestring parameter to lookupvar was objectionable for several reasons; first, it performed a function orthogonal to the main purpose of the method, second its default was the least common value, and third it was causing other code to work for reasons that were not obvious (extlookup). This refactor breaks the value-transforming function out into a seperate method which allows the user to specify the value to be used in lieu of :undef and removes the parameter. The function, Scope#undef_as(default,exp) is written so that it can be used in user code (templates, functions, etc.) if needed. This refactor will introduce a user-visible behaviour change in the case where users were counting on lookupvar to return "" for undefined variables. The best solution is to have them use undef_as, replacing: lookupvar('myvar') with undef_as('',lookupvar('myvar')) (with the option to specify another default value if desired). If this is too objectionable, we could rename the existing lookupvar as raw_lookupvar and define def lookupvar(v) undef_as('',raw_lookupvar(v)) end to restore the present behaviour.
Diffstat (limited to 'lib/puppet/parser/scope.rb')
-rw-r--r--lib/puppet/parser/scope.rb42
1 files changed, 19 insertions, 23 deletions
diff --git a/lib/puppet/parser/scope.rb b/lib/puppet/parser/scope.rb
index 07347552b..df307915e 100644
--- a/lib/puppet/parser/scope.rb
+++ b/lib/puppet/parser/scope.rb
@@ -210,45 +210,41 @@ class Puppet::Parser::Scope
find_definition(name) || find_hostclass(name)
end
- def lookup_qualified_var(name, usestring)
+ def undef_as(x,v)
+ (v == :undefined) ? x : (v == :undef) ? x : v
+ end
+
+ def lookup_qualified_var(name)
parts = name.split(/::/)
shortname = parts.pop
klassname = parts.join("::")
klass = find_hostclass(klassname)
unless klass
warning "Could not look up qualified variable '#{name}'; class #{klassname} could not be found"
- return usestring ? "" : :undefined
+ return :undefined
end
unless kscope = class_scope(klass)
warning "Could not look up qualified variable '#{name}'; class #{klassname} has not been evaluated"
- return usestring ? "" : :undefined
+ return :undefined
end
- kscope.lookupvar(shortname, usestring)
+ kscope.lookupvar(shortname)
end
private :lookup_qualified_var
- # Look up a variable. The simplest value search we do. Default to returning
- # an empty string for missing values, but support returning a constant.
- def lookupvar(name, usestring = true)
+ # Look up a variable. The simplest value search we do.
+ def lookupvar(name)
table = ephemeral?(name) ? @ephemeral.last : @symtable
# If the variable is qualified, then find the specified scope and look the variable up there instead.
if name =~ /::/
- return lookup_qualified_var(name, usestring)
- end
- # We can't use "if table[name]" here because the value might be false
- if ephemeral_include?(name) or table.include?(name)
- if usestring and table[name] == :undef
- return ""
- else
- return table[name]
- end
- elsif self.parent
- return parent.lookupvar(name, usestring)
- elsif usestring
- return ""
+ lookup_qualified_var(name)
+ elsif ephemeral_include?(name) or table.include?(name)
+ # We can't use "if table[name]" here because the value might be false
+ table[name]
+ elsif parent
+ parent.lookupvar(name)
else
- return :undefined
+ :undefined
end
end
@@ -333,7 +329,7 @@ class Puppet::Parser::Scope
table[name] = value
else # append case
# lookup the value in the scope if it exists and insert the var
- table[name] = lookupvar(name)
+ table[name] = undef_as('',lookupvar(name))
# concatenate if string, append if array, nothing for other types
case value
when Array
@@ -363,7 +359,7 @@ class Puppet::Parser::Scope
if var and var =~ /^[0-9]+$/ and not ephemeral_include?(var)
next
end
- out << lookupvar(var).to_s || ""
+ out << undef_as('',lookupvar(var)).to_s
end
elsif ss.scan(/^\\(.)/)
# Puppet.debug("Got escape: pos:%d; m:%s" % [ss.pos, ss.matched])