diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-07-28 19:56:34 +0200 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2009-08-01 11:15:29 +1000 |
commit | 3ebf148bf3d82d25e690aec6ec49975e0837e604 (patch) | |
tree | b94504992e575d8b4181440b90bdf510cf67ef8c /lib/puppet/parser/ast/selector.rb | |
parent | ef68967f2b72e609a9d69e53771a61fd9f522149 (diff) | |
download | puppet-3ebf148bf3d82d25e690aec6ec49975e0837e604.tar.gz puppet-3ebf148bf3d82d25e690aec6ec49975e0837e604.tar.xz puppet-3ebf148bf3d82d25e690aec6ec49975e0837e604.zip |
Enhance selector and case statements to match with regexp
The case and selector statements define ephemeral vars, like 'if'.
Usage:
case statement:
$var = "foobar"
case $var {
"foo": {
notify { "got a foo": }
}
/(.*)bar$/: {
notify{ "hey we got a $1": }
}
}
and for selector:
$val = $test ? {
/^match.*$/ => "matched",
default => "default"
}
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet/parser/ast/selector.rb')
-rw-r--r-- | lib/puppet/parser/ast/selector.rb | 42 |
1 files changed, 12 insertions, 30 deletions
diff --git a/lib/puppet/parser/ast/selector.rb b/lib/puppet/parser/ast/selector.rb index ecad163d4..1b05f57f0 100644 --- a/lib/puppet/parser/ast/selector.rb +++ b/lib/puppet/parser/ast/selector.rb @@ -12,17 +12,12 @@ class Puppet::Parser::AST # Find the value that corresponds with the test. def evaluate(scope) - retvalue = nil - found = nil - # Get our parameter. paramvalue = @param.safeevaluate(scope) sensitive = Puppet[:casesensitive] - if ! sensitive and paramvalue.respond_to?(:downcase) - paramvalue = paramvalue.downcase - end + paramvalue = paramvalue.downcase if not sensitive and paramvalue.respond_to?(:downcase) default = nil @@ -31,33 +26,20 @@ class Puppet::Parser::AST end # Then look for a match in the options. - @values.each { |obj| - param = obj.param.safeevaluate(scope) - if ! sensitive && param.respond_to?(:downcase) - param = param.downcase - end - if param == paramvalue - # we found a matching option - retvalue = obj.value.safeevaluate(scope) - found = true - break - elsif obj.param.is_a?(Default) - # Store the default, in case it's necessary. - default = obj - end - } + @values.each do |obj| + # short circuit asap if we have a match + return obj.value.safeevaluate(scope) if obj.param.evaluate_match(paramvalue, scope, :file => file, :line => line, :sensitive => sensitive) - # Unless we found something, look for the default. - unless found - if default - retvalue = default.value.safeevaluate(scope) - else - self.fail Puppet::ParseError, - "No matching value for selector param '%s'" % paramvalue - end + # Store the default, in case it's necessary. + default = obj if obj.param.is_a?(Default) end - return retvalue + # Unless we found something, look for the default. + return default.value.safeevaluate(scope) if default + + self.fail Puppet::ParseError, "No matching value for selector param '%s'" % paramvalue + ensure + scope.unset_ephemeral_var end end end |