diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2010-06-06 17:22:03 +0200 |
|---|---|---|
| committer | test branch <puppet-dev@googlegroups.com> | 2010-02-17 06:50:53 -0800 |
| commit | 9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d (patch) | |
| tree | b2823dc0ed736e3c4cb34f4a0dc5756c7a4061cd /lib/puppet/parser/ast | |
| parent | 3696d951f70f5b94b49619dfbc57138d5241bbb8 (diff) | |
| download | puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.tar.gz puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.tar.xz puppet-9592dd896c6aab9a3e9b0181c943e7c9ba3d2d6d.zip | |
Fix #3871 - Add the 'in' operator
This operator allows to find if the left operand is in the right one.
The left operand must be resort to a string, but the right operand can be:
* a string
* an array
* a hash (the search is done on the keys)
This syntax can be used in any place where an expression is supported.
Syntax:
$eatme = 'eat'
if $eatme in ['ate', 'eat'] {
...
}
$value = 'beat generation'
if 'eat' in $value {
notice("on the road")
}
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'lib/puppet/parser/ast')
| -rw-r--r-- | lib/puppet/parser/ast/in_operator.rb | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast/in_operator.rb b/lib/puppet/parser/ast/in_operator.rb new file mode 100644 index 000000000..0f543af9b --- /dev/null +++ b/lib/puppet/parser/ast/in_operator.rb @@ -0,0 +1,26 @@ +require 'puppet' +require 'puppet/parser/ast/branch' + +class Puppet::Parser::AST + class InOperator < AST::Branch + + attr_accessor :lval, :rval + + # Returns a boolean which is the result of the 'in' operation + # of lval and rval operands + def evaluate(scope) + + # evaluate the operands, should return a boolean value + lval = @lval.safeevaluate(scope) + unless lval.is_a?(::String) + raise ArgumentError, "'#{lval}' from left operand of 'in' expression is not a string" + end + + rval = @rval.safeevaluate(scope) + unless rval.respond_to?(:include?) + raise ArgumentError, "'#{rval}' from right operand of 'in' expression is not of a supported type (string, array or hash)" + end + return rval.include?(lval) + end + end +end |
