X-Git-Url: https://git.adam-barratt.org.uk/?a=blobdiff_plain;f=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fmember.rb;fp=3rdparty%2Fmodules%2Fstdlib%2Flib%2Fpuppet%2Fparser%2Ffunctions%2Fmember.rb;h=8154f3b03df6dc4f5d0ea83a05ab700d417e4906;hb=131e09855e065be940e104d9ab0f18940cc76257;hp=fb93452181ebea0e60c2dd3cc669170a05860e70;hpb=407d322498f4fde815abf381007fbecfe5c10b2b;p=mirror%2Fdsa-puppet.git diff --git a/3rdparty/modules/stdlib/lib/puppet/parser/functions/member.rb b/3rdparty/modules/stdlib/lib/puppet/parser/functions/member.rb index fb9345218..8154f3b03 100644 --- a/3rdparty/modules/stdlib/lib/puppet/parser/functions/member.rb +++ b/3rdparty/modules/stdlib/lib/puppet/parser/functions/member.rb @@ -1,34 +1,45 @@ +# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... +# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... # # member.rb # +module Puppet::Parser::Functions + newfunction(:member, :type => :rvalue, :doc => <<-DOC + This function determines if a variable is a member of an array. + The variable can be a string, fixnum, or array. -# TODO(Krzysztof Wilczynski): We need to add support for regular expression ... -# TODO(Krzysztof Wilczynski): Support for strings and hashes too ... + *Examples:* -module Puppet::Parser::Functions - newfunction(:member, :type => :rvalue, :doc => <<-EOS -This function determines if a variable is a member of an array. -The variable can be a string, fixnum, or array. + member(['a','b'], 'b') -*Examples:* + Would return: true - member(['a','b'], 'b') + member(['a', 'b', 'c'], ['a', 'b']) -Would return: true + would return: true - member(['a', 'b', 'c'], ['a', 'b']) + member(['a','b'], 'c') -would return: true + Would return: false - member(['a','b'], 'c') + member(['a', 'b', 'c'], ['d', 'b']) -Would return: false + would return: false - member(['a', 'b', 'c'], ['d', 'b']) + Note: Since Puppet 4.0.0 the same can be performed in the Puppet language. For single values + the operator `in` can be used: -would return: false - EOS - ) do |arguments| + 'a' in ['a', 'b'] # true + + And for arrays by using operator `-` to compute a diff: + + ['d', 'b'] - ['a', 'b', 'c'] == [] # false because 'd' is not subtracted + ['a', 'b'] - ['a', 'b', 'c'] == [] # true because both 'a' and 'b' are subtracted + + Also note that since Puppet 5.2.0 the general form of testing content of an array or hash is to use the built-in + `any` and `all` functions. + DOC + ) do |arguments| raise(Puppet::ParseError, "member(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2 @@ -38,16 +49,15 @@ would return: false raise(Puppet::ParseError, 'member(): Requires array to work with') end - unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or arguments[1].is_a? Array + unless arguments[1].is_a?(String) || arguments[1].is_a?(Integer) || arguments[1].is_a?(Array) raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array') end - if arguments[1].is_a? String or arguments[1].is_a? Fixnum - item = [arguments[1]] - else - item = arguments[1] - end - + item = if arguments[1].is_a?(String) || arguments[1].is_a?(Integer) + [arguments[1]] + else + arguments[1] + end raise(Puppet::ParseError, 'member(): You must provide item to search for within array given') if item.respond_to?('empty?') && item.empty?