Skip to content

Commit f5bf8d6

Browse files
committed
(MODULES-1329) Allow member to look for array
Currently, member allows one to only find if a variable is member of an array. Sometimes it is usefull to find if an array is member of a bigger array for validation purpose.
1 parent 9e8127b commit f5bf8d6

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

lib/puppet/parser/functions/member.rb

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,25 @@
88
module Puppet::Parser::Functions
99
newfunction(:member, :type => :rvalue, :doc => <<-EOS
1010
This function determines if a variable is a member of an array.
11+
The variable can either be a string or an array.
1112
1213
*Examples:*
1314
1415
member(['a','b'], 'b')
1516
1617
Would return: true
1718
19+
member(['a', 'b', 'c'], ['a', 'b'])
20+
21+
would return: true
22+
1823
member(['a','b'], 'c')
1924
2025
Would return: false
26+
27+
member(['a', 'b', 'c'], ['d', 'b'])
28+
29+
would return: false
2130
EOS
2231
) do |arguments|
2332

@@ -30,12 +39,16 @@ module Puppet::Parser::Functions
3039
raise(Puppet::ParseError, 'member(): Requires array to work with')
3140
end
3241

33-
item = arguments[1]
42+
if arguments[1].is_a? String
43+
item_array = Array(arguments[1])
44+
else
45+
item_array = arguments[1]
46+
end
3447

3548
raise(Puppet::ParseError, 'member(): You must provide item ' +
36-
'to search for within array given') if item.empty?
49+
'to search for within array given') if item_array.empty?
3750

38-
result = array.include?(item)
51+
result = (item_array - array).empty?
3952

4053
return result
4154
end

spec/functions/member_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@
2121
result = scope.function_member([["a","b","c"], "d"])
2222
expect(result).to(eq(false))
2323
end
24+
25+
it "should return true if a member array is in an array" do
26+
result = scope.function_member([["a","b","c"], ["a", "b"]])
27+
expect(result).to(eq(true))
28+
end
29+
30+
it "should return false if a member array is not in an array" do
31+
result = scope.function_member([["a","b","c"], ["d", "e"]])
32+
expect(result).to(eq(false))
33+
end
2434
end

0 commit comments

Comments
 (0)