Skip to content

Commit

Permalink
Merge pull request #319 from Spredzy/member_array_in_array
Browse files Browse the repository at this point in the history
(MODULES-1329) Allow member to look for array
  • Loading branch information
hunner committed Nov 13, 2014
2 parents b6830f1 + c9f906f commit fb42396
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/puppet/parser/functions/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,25 @@
module Puppet::Parser::Functions
newfunction(:member, :type => :rvalue, :doc => <<-EOS
This function determines if a variable is a member of an array.
The variable can either be a string or an array.
*Examples:*
member(['a','b'], 'b')
Would return: true
member(['a', 'b', 'c'], ['a', 'b'])
would return: true
member(['a','b'], 'c')
Would return: false
member(['a', 'b', 'c'], ['d', 'b'])
would return: false
EOS
) do |arguments|

Expand All @@ -30,13 +39,17 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, 'member(): Requires array to work with')
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 ' +
'to search for within array given') if item.respond_to?('empty?') && item.empty?

result = array.include?(item)
result = (item - array).empty?

return result
end
Expand Down
10 changes: 10 additions & 0 deletions spec/functions/member_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@
result = scope.function_member([["a","b","c"], "d"])
expect(result).to(eq(false))
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

0 comments on commit fb42396

Please sign in to comment.