1 | #
|
---|
2 | # member.rb
|
---|
3 | #
|
---|
4 |
|
---|
5 | # TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
---|
6 | # TODO(Krzysztof Wilczynski): Support for strings and hashes too ...
|
---|
7 |
|
---|
8 | module Puppet::Parser::Functions
|
---|
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 |
|
---|
13 | *Examples:*
|
---|
14 |
|
---|
15 | member(['a','b'], 'b')
|
---|
16 |
|
---|
17 | Would return: true
|
---|
18 |
|
---|
19 | member(['a', 'b', 'c'], ['a', 'b'])
|
---|
20 |
|
---|
21 | would return: true
|
---|
22 |
|
---|
23 | member(['a','b'], 'c')
|
---|
24 |
|
---|
25 | Would return: false
|
---|
26 |
|
---|
27 | member(['a', 'b', 'c'], ['d', 'b'])
|
---|
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 |
|
---|
36 | array = arguments[0]
|
---|
37 |
|
---|
38 | unless array.is_a?(Array)
|
---|
39 | raise(Puppet::ParseError, 'member(): Requires array to work with')
|
---|
40 | end
|
---|
41 |
|
---|
42 | unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or arguments[1].is_a? Array
|
---|
43 | raise(Puppet::ParseError, 'member(): Item to search for must be a string, fixnum, or array')
|
---|
44 | end
|
---|
45 |
|
---|
46 | if arguments[1].is_a? String or arguments[1].is_a? Fixnum
|
---|
47 | item = [arguments[1]]
|
---|
48 | else
|
---|
49 | item = arguments[1]
|
---|
50 | end
|
---|
51 |
|
---|
52 |
|
---|
53 | raise(Puppet::ParseError, 'member(): You must provide item ' +
|
---|
54 | 'to search for within array given') if item.respond_to?('empty?') && item.empty?
|
---|
55 |
|
---|
56 | result = (item - array).empty?
|
---|
57 |
|
---|
58 | return result
|
---|
59 | end
|
---|
60 | end
|
---|
61 |
|
---|
62 | # vim: set ts=2 sw=2 et :
|
---|