Puppet Function: array_do

Defined in:
lib/puppet/parser/functions/array_do.rb
Function type:
Ruby 3.x API

Overview

array_do()Any

When passed an array, a function name and parameters, executes the specified function on each element of the array.

If you wish to capture output, you must use array_do_r().

This function is especially useful when combined with validate_* – you can now apply the validation to within your arrays.

Example:

$usernames = [ ‘tom’, ‘jerry’, ‘bruno’, ‘pluto’ ] $regex = [ ‘^(tom|jerry|bruno)$’ array_do( $usernames, ‘validate_re’, $regex )

Returns:

  • (Any)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/parser/functions/array_do.rb', line 5

newfunction(:array_do, :doc => <<-EOS
When passed an array, a function name and parameters, executes the specified
function on each element of the array.

If you wish to capture output, you must use array_do_r().

This function is especially useful when combined with validate_* -- you can now
apply the validation to within your arrays.

Example:

$usernames = [ 'tom', 'jerry', 'bruno', 'pluto' ]
$regex = [ '^(tom|jerry|bruno)$'
array_do( $usernames, 'validate_re', $regex )
  EOS
) do |arguments|

  if arguments.size < 1 or arguments.size > 3
    raise( Puppet::ParseError,
          "array_do(): invalid arg count. must be 2 or 3." )
  end

  data = arguments[0]
  func = arguments[1]
  params = arguments[2] if arguments.size == 3

  # functions are required to pass anonymous arrays for parameters
  # so if we weren't given an array to prepend with our element
  # we'll convert it here
  if ! params.is_a?(Array)
    params = Array.new.push(params)
  end

  # Let's load our puppet function
  # TODO: validate its a good function
  Puppet::Parser::Functions.function(func)

  # iterate through each of our array elements, building our parameter
  # array and then executing our function.
  data.each do |element|
    args = params.dup.insert(0,element)
    if args.size > 2
      raise(Puppet::ParseError, args )
    end
    args.compact!
    send("function_#{func}", args)
  end
end