From e80207bede37e498c32bea6d56c46f1dd709721e Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- lib/puppet/parser/functions/flatten.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 781da78..a1ed183 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -16,7 +16,7 @@ Would return: ['a','b','c'] ) do |arguments| raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] -- cgit v1.2.3 From 5a11279cc54e7f9f5d168c75728f990d0bd0b694 Mon Sep 17 00:00:00 2001 From: Uwe Stuehler Date: Tue, 23 Oct 2012 16:43:03 +0200 Subject: Fix number of arguments check in flatten() The function only uses the first argument, so raise an error with too few arguments *and* with too many arguments. --- lib/puppet/parser/functions/flatten.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/flatten.rb b/lib/puppet/parser/functions/flatten.rb index 781da78..a1ed183 100644 --- a/lib/puppet/parser/functions/flatten.rb +++ b/lib/puppet/parser/functions/flatten.rb @@ -16,7 +16,7 @@ Would return: ['a','b','c'] ) do |arguments| raise(Puppet::ParseError, "flatten(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 + "given (#{arguments.size} for 1)") if arguments.size != 1 array = arguments[0] -- cgit v1.2.3 From 5d5a4d466ed6530fa8b68162e96bdaaaf35066e1 Mon Sep 17 00:00:00 2001 From: Justin Lambert Date: Mon, 17 Dec 2012 06:22:36 -0700 Subject: str2bool should return a boolean if called with a boolean --- lib/puppet/parser/functions/str2bool.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/str2bool.rb b/lib/puppet/parser/functions/str2bool.rb index c320da6..9ea6dd5 100644 --- a/lib/puppet/parser/functions/str2bool.rb +++ b/lib/puppet/parser/functions/str2bool.rb @@ -14,6 +14,11 @@ like: 0, f, n, false, no to 'false'. "given (#{arguments.size} for 1)") if arguments.size < 1 string = arguments[0] + + # If string is already Boolean, return it + if !!string == string + return string + end unless string.is_a?(String) raise(Puppet::ParseError, 'str2bool(): Requires either ' + -- cgit v1.2.3 From 961dcab15d3cf00eabeb5a496efc9da051169241 Mon Sep 17 00:00:00 2001 From: Eric Shamow Date: Tue, 3 Apr 2012 22:30:46 -0400 Subject: (#13610) Add is_function_available to stdlib This function provides a simple wrapper around Puppet::Parser::Functions.function for access within Puppet manifests. This will allow users to check whether or not a plugin or functionality such as hiera is installed on the server. --- .../parser/functions/is_function_available.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 lib/puppet/parser/functions/is_function_available.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb new file mode 100644 index 0000000..6cbd35c --- /dev/null +++ b/lib/puppet/parser/functions/is_function_available.rb @@ -0,0 +1,23 @@ +# +# is_function_available.rb +# + +module Puppet::Parser::Functions + newfunction(:is_function_available, :type => :rvalue, :doc => <<-EOS +This function accepts a string as an argument, determines whether the +Puppet runtime has access to a function by that name. It returns a +true if the function exists, false if not. + EOS + ) do |arguments| + + if (arguments.size != 1) then + raise(Puppet::ParseError, "is_function_available?(): Wrong number of arguments "+ + "given #{arguments.size} for 1") + end + + function = Puppet::Parser::Functions.function(arguments[0].to_sym) + function.is_a?(String) and not function.empty? + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 05273419e1c8b34115ede15b1d8a8739f6a0db00 Mon Sep 17 00:00:00 2001 From: Kristof Willaert Date: Tue, 19 Mar 2013 10:00:57 +0100 Subject: Add floor function implementation and unit tests --- lib/puppet/parser/functions/floor.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/puppet/parser/functions/floor.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb new file mode 100644 index 0000000..a401923 --- /dev/null +++ b/lib/puppet/parser/functions/floor.rb @@ -0,0 +1,20 @@ +module Puppet::Parser::Functions + newfunction(:floor, :type => :rvalue, :doc => <<-EOS + Returns the largest integer less or equal to the argument. + Takes a single numeric value as an argument. + EOS + ) do |arguments| + + raise(Puppet::ParseError, "floor(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size != 1 + + arg = arguments[0] + + raise(Puppet::ParseError, "floor(): Wrong argument type " + + "given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false + + arg.floor + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 88a93ac6cdf38045e1cf29325a70e5e4143016b3 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:45:40 -0700 Subject: add suffix function to accompany the prefix function --- lib/puppet/parser/functions/suffix.rb | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/puppet/parser/functions/suffix.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb new file mode 100644 index 0000000..5018280 --- /dev/null +++ b/lib/puppet/parser/functions/suffix.rb @@ -0,0 +1,45 @@ +# +# suffix.rb +# + +module Puppet::Parser::Functions + newfunction(:suffix, :type => :rvalue, :doc => <<-EOS +This function applies a suffix to all elements in an array. + +*Examples:* + + suffix(['a','b','c'], 'p') + +Will return: ['ap','bp','cp'] + EOS + ) do |arguments| + + # Technically we support two arguments but only first is mandatory ... + raise(Puppet::ParseError, "suffix(): Wrong number of arguments " + + "given (#{arguments.size} for 1)") if arguments.size < 1 + + array = arguments[0] + + unless array.is_a?(Array) + raise(Puppet::ParseError, 'suffix(): Requires array to work with') + end + + suffix = arguments[1] if arguments[1] + + if suffix + unless suffix.is_a?(String) + raise(Puppet::ParseError, 'suffix(): Requires string to work with') + end + end + + # Turn everything into string same as join would do ... + result = array.collect do |i| + i = i.to_s + suffix ? i + suffix : i + end + + return result + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From a83318d3ee41683118a55a2b15769c97efbcf6d9 Mon Sep 17 00:00:00 2001 From: Richard Soderberg Date: Tue, 26 Mar 2013 15:49:09 -0700 Subject: prefix: fix doc typo Examles -> Examples --- lib/puppet/parser/functions/prefix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 4593976..243cf18 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -6,7 +6,7 @@ module Puppet::Parser::Functions newfunction(:prefix, :type => :rvalue, :doc => <<-EOS This function applies a prefix to all elements in an array. -*Examles:* +*Examples:* prefix(['a','b','c'], 'p') -- cgit v1.2.3 From 29402f31e73a426dcc449216010834884d0251ec Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Wed, 27 Mar 2013 13:37:25 -0700 Subject: (maint) better error reporting for prefix and suffix When prefix and suffix did error checking with positional arguments, they would not report the position of the argument that failed to validate. This commit changes the messages to indicate which argument failed. --- lib/puppet/parser/functions/prefix.rb | 4 ++-- lib/puppet/parser/functions/suffix.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/prefix.rb b/lib/puppet/parser/functions/prefix.rb index 243cf18..62211ae 100644 --- a/lib/puppet/parser/functions/prefix.rb +++ b/lib/puppet/parser/functions/prefix.rb @@ -21,14 +21,14 @@ Will return: ['pa','pb','pc'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'prefix(): Requires array to work with') + raise Puppet::ParseError, "prefix(): expected first argument to be an Array, got #{array.inspect}" end prefix = arguments[1] if arguments[1] if prefix unless prefix.is_a?(String) - raise(Puppet::ParseError, 'prefix(): Requires string to work with') + raise Puppet::ParseError, "prefix(): expected second argument to be a String, got #{suffix.inspect}" end end diff --git a/lib/puppet/parser/functions/suffix.rb b/lib/puppet/parser/functions/suffix.rb index 5018280..f7792d6 100644 --- a/lib/puppet/parser/functions/suffix.rb +++ b/lib/puppet/parser/functions/suffix.rb @@ -21,14 +21,14 @@ Will return: ['ap','bp','cp'] array = arguments[0] unless array.is_a?(Array) - raise(Puppet::ParseError, 'suffix(): Requires array to work with') + raise Puppet::ParseError, "suffix(): expected first argument to be an Array, got #{array.inspect}" end suffix = arguments[1] if arguments[1] if suffix - unless suffix.is_a?(String) - raise(Puppet::ParseError, 'suffix(): Requires string to work with') + unless suffix.is_a? String + raise Puppet::ParseError, "suffix(): expected second argument to be a String, got #{suffix.inspect}" end end -- cgit v1.2.3 From ff5dd5d75adb3723e106ca20bac4e68466395a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 20 Mar 2013 16:36:20 +0100 Subject: Allow comparisons of Numeric and number as String Puppet passes numbers as String to functions, but it makes more sense to compare them as Numeric. But sometimes Puppet passes them as the wrong type, see: https://projects.puppetlabs.com/issues/19812 --- lib/puppet/parser/functions/max.rb | 10 +++++++++- lib/puppet/parser/functions/min.rb | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/max.rb b/lib/puppet/parser/functions/max.rb index 10b6f74..60fb94a 100644 --- a/lib/puppet/parser/functions/max.rb +++ b/lib/puppet/parser/functions/max.rb @@ -8,6 +8,14 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "max(): Wrong number of arguments " + "need at least one") if args.size == 0 - return args.max + # Sometimes we get numbers as numerics and sometimes as strings. + # We try to compare them as numbers when possible + return args.max do |a,b| + if a.to_s =~ /\A-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then + a.to_f <=> b.to_f + else + a.to_s <=> b.to_s + end + end end end diff --git a/lib/puppet/parser/functions/min.rb b/lib/puppet/parser/functions/min.rb index abf1b62..6bd6ebf 100644 --- a/lib/puppet/parser/functions/min.rb +++ b/lib/puppet/parser/functions/min.rb @@ -8,6 +8,14 @@ module Puppet::Parser::Functions raise(Puppet::ParseError, "min(): Wrong number of arguments " + "need at least one") if args.size == 0 - return args.min + # Sometimes we get numbers as numerics and sometimes as strings. + # We try to compare them as numbers when possible + return args.min do |a,b| + if a.to_s =~ /\A^-?\d+(.\d+)?\z/ and b.to_s =~ /\A-?\d+(.\d+)?\z/ then + a.to_f <=> b.to_f + else + a.to_s <=> b.to_s + end + end end end -- cgit v1.2.3 From e6916f83fd35989db4b86dfb10716c9198994389 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 10:04:05 -0400 Subject: Enable num2bool to accept numeric input Also ignore rspec fixtures directory --- lib/puppet/parser/functions/num2bool.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 874db22..638f693 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -14,7 +14,8 @@ higher then 0 become true. raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size < 1 - number = arguments[0] + # Since we're matching against a regex, coerce to String + number = arguments[0].to_s # Only numbers allowed ... unless number.match(/^\-?\d+$/) -- cgit v1.2.3 From 4a5218a8af8c3ffaf9ea2348a3900b19d6a95416 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 12:03:33 -0400 Subject: Reworked number-handling logic No more coercing to String and regex matching. Instead, we now coerce to Integer at the beginning or raise an error if we cannot coerce to Integer. A consequence of this change is that the function will now accept blatantly non-numeric strings as input, and return false. This seems a bit goofy to me, but it's how String#to_i works. If we really don't like this, then I'm open to suggestions. --- lib/puppet/parser/functions/num2bool.rb | 35 +++++++++++---------------------- 1 file changed, 11 insertions(+), 24 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 638f693..15dd560 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -2,39 +2,26 @@ # num2bool.rb # -# TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ... - module Puppet::Parser::Functions newfunction(:num2bool, :type => :rvalue, :doc => <<-EOS -This function converts a number into a true boolean. Zero becomes false. Numbers -higher then 0 become true. +This function converts a number or a string representation of a number into a +true boolean. Zero or anything non-numeric becomes false. Numbers higher then 0 +become true. EOS ) do |arguments| raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + - "given (#{arguments.size} for 1)") if arguments.size < 1 - - # Since we're matching against a regex, coerce to String - number = arguments[0].to_s - - # Only numbers allowed ... - unless number.match(/^\-?\d+$/) - raise(Puppet::ParseError, 'num2bool(): Requires integer to work with') - end + "given (#{arguments.size} for 1)") if arguments.size != 1 - result = case number - when /^0$/ - false - when /^\-?\d+$/ - # Numbers in Puppet are often string-encoded which is troublesome ... - number = number.to_i - # We yield true for any positive number and false otherwise ... - number > 0 ? true : false - else - raise(Puppet::ParseError, 'num2bool(): Unknown numeric format given') + # we need to get an Integer out of this + begin + number = arguments[0].to_i + rescue NoMethodError + raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!) end - return result + # Return true for any positive number and false otherwise + return number > 0 ? true : false end end -- cgit v1.2.3 From 8d217f0012fef332642faf485ad187773a95bcc1 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Fri, 29 Mar 2013 15:06:36 -0400 Subject: (19864) num2bool match fix This is a bit more heavy-handed than I might like, but it does appear to do the right things: * accepts numeric input appropriately, truncating floats * matches string input against a regex, then coerces number-looking strings to int * makes a best effort to coerce anything else to a string, then subjects it to the same treatment * raises an error in the event of incorrect number of arguments or non-number-looking strings I've also included some additional unit tests. --- lib/puppet/parser/functions/num2bool.rb | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index 15dd560..cf98f80 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -13,13 +13,32 @@ become true. raise(Puppet::ParseError, "num2bool(): Wrong number of arguments " + "given (#{arguments.size} for 1)") if arguments.size != 1 - # we need to get an Integer out of this - begin - number = arguments[0].to_i - rescue NoMethodError - raise(Puppet::ParseError, 'num2bool(): Unable to parse number: ' + $!) + number = arguments[0] + + case number + when Numeric + # Yay, it's a number + when String + # Deal with strings later + else + begin + number = number.to_s + rescue NoMethodError + raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!) + end + end + + case number + when String + # Only accept strings that look somewhat like numbers + unless number =~ /^-?\d+/ + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number") + end end + # Truncate floats + number = number.to_i + # Return true for any positive number and false otherwise return number > 0 ? true : false end -- cgit v1.2.3 From c372f177708df4c844337e9901646b7b84b86cd8 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Mon, 1 Apr 2013 11:44:09 -0400 Subject: Cleanup per adrianthebo suggestions * use Float() to process string arguments * get rid of doubly nested arrays * removing needless ternary operator * improving error message handling --- lib/puppet/parser/functions/num2bool.rb | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/num2bool.rb b/lib/puppet/parser/functions/num2bool.rb index cf98f80..af0e6ed 100644 --- a/lib/puppet/parser/functions/num2bool.rb +++ b/lib/puppet/parser/functions/num2bool.rb @@ -19,28 +19,24 @@ become true. when Numeric # Yay, it's a number when String - # Deal with strings later + begin + number = Float(number) + rescue ArgumentError => ex + raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number: #{ex.message}") + end else begin number = number.to_s - rescue NoMethodError - raise(Puppet::ParseError, 'num2bool(): Unable to parse argument: ' + $!) - end - end - - case number - when String - # Only accept strings that look somewhat like numbers - unless number =~ /^-?\d+/ - raise(Puppet::ParseError, "num2bool(): '#{number}' does not look like a number") + rescue NoMethodError => ex + raise(Puppet::ParseError, "num2bool(): Unable to parse argument: #{ex.message}") end end - # Truncate floats + # Truncate Floats number = number.to_i # Return true for any positive number and false otherwise - return number > 0 ? true : false + return number > 0 end end -- cgit v1.2.3 From 7a2fb80834d3ec4d21efe58e5e8af188e5bdd07c Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Sun, 31 Mar 2013 23:37:30 +1100 Subject: (#19998) Implement any2array This change is to implement a new function "any2array", which will take any argument or arguments and create an array which contains it. If the argument is a single array then it will be returned as-is. If the argument is a single hash then it will be converted into an array. Otherwise (if there are more than one argument, or the only argument is not an array or a hash) the function will return an array containing all the arguments. --- lib/puppet/parser/functions/any2array.rb | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/puppet/parser/functions/any2array.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb new file mode 100644 index 0000000..d150743 --- /dev/null +++ b/lib/puppet/parser/functions/any2array.rb @@ -0,0 +1,35 @@ +# +# str2bool.rb +# + +module Puppet::Parser::Functions + newfunction(:any2array, :type => :rvalue, :doc => <<-EOS +This converts any object to an array containing that object. Empty argument +lists are converted to an empty array. Arrays are left untouched. Hashes are +converted to arrays of alternating keys and values. + EOS + ) do |arguments| + + if arguments.empty? + return [] + end + + if arguments.length == 1 + if arguments[0].kind_of?(Array) + return arguments[0] + elsif arguments[0].kind_of?(Hash) + result = [] + arguments[0].each do |key, value| + result << key << value + end + return result + else + return [arguments[0]] + end + end + + return arguments + end +end + +# vim: set ts=2 sw=2 et : -- cgit v1.2.3 From 003cde074c86f034d2730eeb40d676d4e35c6022 Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Sun, 31 Mar 2013 23:47:11 +1100 Subject: (#19998) fix name of function in a comment --- lib/puppet/parser/functions/any2array.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index d150743..95944e1 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -1,5 +1,5 @@ # -# str2bool.rb +# any2array.rb # module Puppet::Parser::Functions -- cgit v1.2.3 From e7d394b6e9fc3ca07803d18fcf2f5d19b91e92e0 Mon Sep 17 00:00:00 2001 From: Amos Shapira Date: Mon, 1 Apr 2013 14:52:07 +1100 Subject: Remove resundant code --- lib/puppet/parser/functions/any2array.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/any2array.rb b/lib/puppet/parser/functions/any2array.rb index 95944e1..e71407e 100644 --- a/lib/puppet/parser/functions/any2array.rb +++ b/lib/puppet/parser/functions/any2array.rb @@ -23,8 +23,6 @@ converted to arrays of alternating keys and values. result << key << value end return result - else - return [arguments[0]] end end -- cgit v1.2.3 From f28550e78996f908b076cdc9aebcbe584c777cd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Dal=C3=A9n?= Date: Wed, 10 Apr 2013 14:38:14 +0200 Subject: Add a count function Similar to the ruby count method on arrays. --- lib/puppet/parser/functions/count.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 lib/puppet/parser/functions/count.rb (limited to 'lib/puppet/parser') diff --git a/lib/puppet/parser/functions/count.rb b/lib/puppet/parser/functions/count.rb new file mode 100644 index 0000000..52de1b8 --- /dev/null +++ b/lib/puppet/parser/functions/count.rb @@ -0,0 +1,22 @@ +module Puppet::Parser::Functions + newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-EOS +Takes an array as first argument and an optional second argument. +Count the number of elements in array that matches second argument. +If called with only an array it counts the number of elements that are not nil/undef. + EOS + ) do |args| + + if (args.size > 2) then + raise(ArgumentError, "count(): Wrong number of arguments "+ + "given #{args.size} for 1 or 2.") + end + + collection, item = args + + if item then + collection.count item + else + collection.count { |obj| obj != nil && obj != :undef && obj != '' } + end + end +end -- cgit v1.2.3