Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / count.rb
1 #
2 # count.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:count, :type => :rvalue, :arity => -2, :doc => <<-DOC
6     Takes an array as first argument and an optional second argument.
7     Count the number of elements in array that is equal to the second argument.
8     If called with only an array, it counts the number of elements that are not nil/undef/empty-string.
9
10     Note: equality is tested with a Ruby method and it is therefore subject to what Ruby considers
11     to be equal. For strings this means that equality is case sensitive.
12
13     In Puppet core, counting can be done in general by using a combination of the core functions
14     filter() (since Puppet 4.0.0) and length() (since Puppet 5.5.0, before that in stdlib).
15     Example below shows counting values that are not undef.
16
17       notice([42, "hello", undef].filter |$x| { $x =~ NotUndef }.length)
18
19     Would notice the value 2.
20
21   DOC
22              ) do |args|
23
24     if args.size > 2
25       raise(ArgumentError, "count(): Wrong number of arguments given #{args.size} for 1 or 2.")
26     end
27
28     collection, item = args
29
30     if item
31       collection.count item
32     else
33       collection.count { |obj| !obj.nil? && obj != :undef && obj != '' }
34     end
35   end
36 end