(MODULES-1329) Allow member function to look for array
Currently, the member function allows one to only find if a variable is part of an array. Sometimes it is useful to find if an array is part of a bigger array for validation purpose.
This commit is contained in:
parent
85d7eddc41
commit
c9f906f803
2 changed files with 25 additions and 2 deletions
|
@ -8,6 +8,7 @@
|
||||||
module Puppet::Parser::Functions
|
module Puppet::Parser::Functions
|
||||||
newfunction(:member, :type => :rvalue, :doc => <<-EOS
|
newfunction(:member, :type => :rvalue, :doc => <<-EOS
|
||||||
This function determines if a variable is a member of an array.
|
This function determines if a variable is a member of an array.
|
||||||
|
The variable can either be a string or an array.
|
||||||
|
|
||||||
*Examples:*
|
*Examples:*
|
||||||
|
|
||||||
|
@ -15,9 +16,17 @@ This function determines if a variable is a member of an array.
|
||||||
|
|
||||||
Would return: true
|
Would return: true
|
||||||
|
|
||||||
|
member(['a', 'b', 'c'], ['a', 'b'])
|
||||||
|
|
||||||
|
would return: true
|
||||||
|
|
||||||
member(['a','b'], 'c')
|
member(['a','b'], 'c')
|
||||||
|
|
||||||
Would return: false
|
Would return: false
|
||||||
|
|
||||||
|
member(['a', 'b', 'c'], ['d', 'b'])
|
||||||
|
|
||||||
|
would return: false
|
||||||
EOS
|
EOS
|
||||||
) do |arguments|
|
) do |arguments|
|
||||||
|
|
||||||
|
@ -30,13 +39,17 @@ Would return: false
|
||||||
raise(Puppet::ParseError, 'member(): Requires array to work with')
|
raise(Puppet::ParseError, 'member(): Requires array to work with')
|
||||||
end
|
end
|
||||||
|
|
||||||
item = arguments[1]
|
if arguments[1].is_a? String
|
||||||
|
item = Array(arguments[1])
|
||||||
|
else
|
||||||
|
item = arguments[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
raise(Puppet::ParseError, 'member(): You must provide item ' +
|
raise(Puppet::ParseError, 'member(): You must provide item ' +
|
||||||
'to search for within array given') if item.respond_to?('empty?') && item.empty?
|
'to search for within array given') if item.respond_to?('empty?') && item.empty?
|
||||||
|
|
||||||
result = array.include?(item)
|
result = (item - array).empty?
|
||||||
|
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
|
@ -21,4 +21,14 @@ describe "the member function" do
|
||||||
result = scope.function_member([["a","b","c"], "d"])
|
result = scope.function_member([["a","b","c"], "d"])
|
||||||
expect(result).to(eq(false))
|
expect(result).to(eq(false))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should return true if a member array is in an array" do
|
||||||
|
result = scope.function_member([["a","b","c"], ["a", "b"]])
|
||||||
|
expect(result).to(eq(true))
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should return false if a member array is not in an array" do
|
||||||
|
result = scope.function_member([["a","b","c"], ["d", "e"]])
|
||||||
|
expect(result).to(eq(false))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue