Skip to content

(MODULES-1329) Allow member to look for array #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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