summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHunter Haugen <hunter@puppetlabs.com>2014-11-13 12:27:14 -0800
committerHunter Haugen <hunter@puppetlabs.com>2014-11-13 12:27:14 -0800
commitfb42396c75d90ce3a9473e2a7ed22682266ea03f (patch)
tree7534301fed07ea095c576083b4e2266e01dd743d
parentb6830f1ade27bf1dad9c75d45ee21cebcddf11f6 (diff)
parentc9f906f80325a12a6509633d87472bd3cbaf0364 (diff)
downloadpuppet-stdlib-fb42396c75d90ce3a9473e2a7ed22682266ea03f.tar.gz
puppet-stdlib-fb42396c75d90ce3a9473e2a7ed22682266ea03f.tar.bz2
Merge pull request #319 from Spredzy/member_array_in_array
(MODULES-1329) Allow member to look for array
-rw-r--r--lib/puppet/parser/functions/member.rb17
-rwxr-xr-xspec/functions/member_spec.rb10
2 files changed, 25 insertions, 2 deletions
diff --git a/lib/puppet/parser/functions/member.rb b/lib/puppet/parser/functions/member.rb
index 11a1d24..bb19a86 100644
--- a/lib/puppet/parser/functions/member.rb
+++ b/lib/puppet/parser/functions/member.rb
@@ -8,6 +8,7 @@
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:*
@@ -15,9 +16,17 @@ This function determines if a variable is a member of an array.
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|
@@ -30,13 +39,17 @@ Would return: false
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
diff --git a/spec/functions/member_spec.rb b/spec/functions/member_spec.rb
index cee6110..1a1d7c6 100755
--- a/spec/functions/member_spec.rb
+++ b/spec/functions/member_spec.rb
@@ -21,4 +21,14 @@ describe "the member function" do
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