comparison modules/stdlib/lib/puppet/parser/functions/member.rb @ 272:c42fb28cff86

Update to a newer Python module This also pulls in an EPEL module (which we don't use) and a newer stdlib version.
author IBBoard <dev@ibboard.co.uk>
date Fri, 03 Jan 2020 19:56:04 +0000
parents addb0ea390a1
children d9352a684e62
comparison
equal deleted inserted replaced
271:c62728474654 272:c42fb28cff86
1 # TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
2 # TODO(Krzysztof Wilczynski): Support for strings and hashes too ...
1 # 3 #
2 # member.rb 4 # member.rb
3 # 5 #
6 module Puppet::Parser::Functions
7 newfunction(:member, :type => :rvalue, :doc => <<-DOC
8 This function determines if a variable is a member of an array.
9 The variable can be a string, fixnum, or array.
4 10
5 # TODO(Krzysztof Wilczynski): We need to add support for regular expression ... 11 *Examples:*
6 # TODO(Krzysztof Wilczynski): Support for strings and hashes too ...
7 12
8 module Puppet::Parser::Functions 13 member(['a','b'], 'b')
9 newfunction(:member, :type => :rvalue, :doc => <<-EOS
10 This function determines if a variable is a member of an array.
11 The variable can be a string, fixnum, or array.
12 14
13 *Examples:* 15 Would return: true
14 16
15 member(['a','b'], 'b') 17 member(['a', 'b', 'c'], ['a', 'b'])
16 18
17 Would return: true 19 would return: true
18 20
19 member(['a', 'b', 'c'], ['a', 'b']) 21 member(['a','b'], 'c')
20 22
21 would return: true 23 Would return: false
22 24
23 member(['a','b'], 'c') 25 member(['a', 'b', 'c'], ['d', 'b'])
24 26
25 Would return: false 27 would return: false
28 DOC
29 ) do |arguments|
26 30
27 member(['a', 'b', 'c'], ['d', 'b']) 31 raise(Puppet::ParseError, "member(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2
28
29 would return: false
30 EOS
31 ) do |arguments|
32
33 raise(Puppet::ParseError, "member(): Wrong number of arguments " +
34 "given (#{arguments.size} for 2)") if arguments.size < 2
35 32
36 array = arguments[0] 33 array = arguments[0]
37 34
38 unless array.is_a?(Array) 35 unless array.is_a?(Array)
39 raise(Puppet::ParseError, 'member(): Requires array to work with') 36 raise(Puppet::ParseError, 'member(): Requires array to work with')
40 end 37 end
41 38
42 unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or arguments[1].is_a? Array 39 unless arguments[1].is_a?(String) || arguments[1].is_a?(Integer) || arguments[1].is_a?(Array)
43 raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array') 40 raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array')
44 end 41 end
45 42
46 if arguments[1].is_a? String or arguments[1].is_a? Fixnum 43 item = if arguments[1].is_a?(String) || arguments[1].is_a?(Integer)
47 item = Array(arguments[1]) 44 [arguments[1]]
48 else 45 else
49 item = arguments[1] 46 arguments[1]
50 end 47 end
51 48
52 49 raise(Puppet::ParseError, 'member(): You must provide item to search for within array given') if item.respond_to?('empty?') && item.empty?
53 raise(Puppet::ParseError, 'member(): You must provide item ' +
54 'to search for within array given') if item.respond_to?('empty?') && item.empty?
55 50
56 result = (item - array).empty? 51 result = (item - array).empty?
57 52
58 return result 53 return result
59 end 54 end