]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/count.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / count.rb
1 module Puppet::Parser::Functions
2   newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-EOS
3 Takes an array as first argument and an optional second argument.
4 Count the number of elements in array that matches second argument.
5 If called with only an array it counts the number of elements that are not nil/undef.
6     EOS
7   ) do |args|
8
9     if (args.size > 2) then
10       raise(ArgumentError, "count(): Wrong number of arguments "+
11         "given #{args.size} for 1 or 2.")
12     end
13
14     collection, item = args
15
16     if item then
17       collection.count item
18     else
19       collection.count { |obj| obj != nil && obj != :undef && obj != '' }
20     end
21   end
22 end