source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/member.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 1.5 KB
Line 
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
8module Puppet::Parser::Functions
9 newfunction(:member, :type => :rvalue, :doc => <<-EOS
10This function determines if a variable is a member of an array.
11The variable can be a string, fixnum, or array.
12
13*Examples:*
14
15 member(['a','b'], 'b')
16
17Would return: true
18
19 member(['a', 'b', 'c'], ['a', 'b'])
20
21would return: true
22
23 member(['a','b'], 'c')
24
25Would return: false
26
27 member(['a', 'b', 'c'], ['d', 'b'])
28
29would 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
60end
61
62# vim: set ts=2 sw=2 et :
Note: See TracBrowser for help on using the repository browser.