2011-04-29 09:27:10 +02:00
|
|
|
#
|
2011-04-30 16:00:49 +02:00
|
|
|
# member.rb
|
2011-04-29 09:27:10 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
# TODO(Krzysztof Wilczynski): We need to add support for regular expression ...
|
|
|
|
# TODO(Krzysztof Wilczynski): Support for strings and hashes too ...
|
|
|
|
|
|
|
|
module Puppet::Parser::Functions
|
2011-04-30 16:00:49 +02:00
|
|
|
newfunction(:member, :type => :rvalue, :doc => <<-EOS
|
2011-07-30 00:09:30 +02:00
|
|
|
This function determines if a variable is a member of an array.
|
2014-11-14 23:33:59 +01:00
|
|
|
The variable can be a string, fixnum, or array.
|
2011-07-30 00:09:30 +02:00
|
|
|
|
|
|
|
*Examples:*
|
|
|
|
|
|
|
|
member(['a','b'], 'b')
|
|
|
|
|
|
|
|
Would return: true
|
|
|
|
|
2014-09-15 20:16:52 +02:00
|
|
|
member(['a', 'b', 'c'], ['a', 'b'])
|
|
|
|
|
|
|
|
would return: true
|
|
|
|
|
2011-07-30 00:09:30 +02:00
|
|
|
member(['a','b'], 'c')
|
|
|
|
|
|
|
|
Would return: false
|
2014-09-15 20:16:52 +02:00
|
|
|
|
|
|
|
member(['a', 'b', 'c'], ['d', 'b'])
|
|
|
|
|
|
|
|
would return: false
|
2011-04-29 09:27:10 +02:00
|
|
|
EOS
|
|
|
|
) do |arguments|
|
|
|
|
|
2011-04-30 16:00:49 +02:00
|
|
|
raise(Puppet::ParseError, "member(): Wrong number of arguments " +
|
2011-04-29 09:27:10 +02:00
|
|
|
"given (#{arguments.size} for 2)") if arguments.size < 2
|
|
|
|
|
|
|
|
array = arguments[0]
|
|
|
|
|
2011-04-30 16:00:49 +02:00
|
|
|
unless array.is_a?(Array)
|
|
|
|
raise(Puppet::ParseError, 'member(): Requires array to work with')
|
2011-04-29 09:27:10 +02:00
|
|
|
end
|
|
|
|
|
2014-11-14 23:33:59 +01:00
|
|
|
unless arguments[1].is_a? String or arguments[1].is_a? Fixnum or 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
|
2015-06-01 13:21:59 +02:00
|
|
|
item = [arguments[1]]
|
2014-09-15 20:16:52 +02:00
|
|
|
else
|
|
|
|
item = arguments[1]
|
|
|
|
end
|
2011-04-29 09:27:10 +02:00
|
|
|
|
2014-10-30 06:02:13 +01:00
|
|
|
|
2011-04-30 16:00:49 +02:00
|
|
|
raise(Puppet::ParseError, 'member(): You must provide item ' +
|
2014-10-30 06:02:13 +01:00
|
|
|
'to search for within array given') if item.respond_to?('empty?') && item.empty?
|
2011-04-29 09:27:10 +02:00
|
|
|
|
2014-09-15 20:16:52 +02:00
|
|
|
result = (item - array).empty?
|
2011-04-29 09:27:10 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# vim: set ts=2 sw=2 et :
|