File: each.rb

package info (click to toggle)
puppet-agent 8.10.0-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 27,404 kB
  • sloc: ruby: 286,820; sh: 492; xml: 116; makefile: 88; cs: 68
file content (106 lines) | stat: -rw-r--r-- 3,783 bytes parent folder | download | duplicates (2)
1
2
3
4
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true

Puppet::Parser::Functions.newfunction(
  :each,
  :type => :rvalue,
  :arity => -3,
  :doc => <<~DOC
    Runs a [lambda](https://puppet.com/docs/puppet/latest/lang_lambdas.html)
    repeatedly using each value in a data structure, then returns the values unchanged.

    This function takes two mandatory arguments, in this order:

    1. An array or hash the function will iterate over.
    2. A lambda, which the function calls for each element in the first argument. It can
    request one or two parameters.

    **Example**: Using the `each` function

    `$data.each |$parameter| { <PUPPET CODE BLOCK> }`

    or

    `each($data) |$parameter| { <PUPPET CODE BLOCK> }`

    When the first argument (`$data` in the above example) is an array, Puppet passes each
    value in turn to the lambda, then returns the original values.

    **Example**: Using the `each` function with an array and a one-parameter lambda

    ~~~ puppet
    # For the array $data, run a lambda that creates a resource for each item.
    $data = ["routers", "servers", "workstations"]
    $data.each |$item| {
     notify { $item:
       message => $item
     }
    }
    # Puppet creates one resource for each of the three items in $data. Each resource is
    # named after the item's value and uses the item's value in a parameter.
    ~~~

    When the first argument is a hash, Puppet passes each key and value pair to the lambda
    as an array in the form `[key, value]` and returns the original hash.

    **Example**: Using the `each` function with a hash and a one-parameter lambda

    ~~~ puppet
    # For the hash $data, run a lambda using each item as a key-value array that creates a
    # resource for each item.
    $data = {"rtr" => "Router", "svr" => "Server", "wks" => "Workstation"}
    $data.each |$items| {
     notify { $items[0]:
       message => $items[1]
     }
    }
    # Puppet creates one resource for each of the three items in $data, each named after the
    # item's key and containing a parameter using the item's value.
    ~~~

    When the first argument is an array and the lambda has two parameters, Puppet passes the
    array's indexes (enumerated from 0) in the first parameter and its values in the second
    parameter.

    **Example**: Using the `each` function with an array and a two-parameter lambda

    ~~~ puppet
    # For the array $data, run a lambda using each item's index and value that creates a
    # resource for each item.
    $data = ["routers", "servers", "workstations"]
    $data.each |$index, $value| {
     notify { $value:
       message => $index
     }
    }
    # Puppet creates one resource for each of the three items in $data, each named after the
    # item's value and containing a parameter using the item's index.
    ~~~

    When the first argument is a hash, Puppet passes its keys to the first parameter and its
    values to the second parameter.

    **Example**: Using the `each` function with a hash and a two-parameter lambda

    ~~~ puppet
    # For the hash $data, run a lambda using each item's key and value to create a resource
    # for each item.
    $data = {"rtr" => "Router", "svr" => "Server", "wks" => "Workstation"}
    $data.each |$key, $value| {
     notify { $key:
       message => $value
     }
    }
    # Puppet creates one resource for each of the three items in $data, each named after the
    # item's key and containing a parameter using the item's value.
    ~~~

    For an example that demonstrates how to create multiple `file` resources using `each`,
    see the Puppet
    [iteration](https://puppet.com/docs/puppet/latest/lang_iteration.html)
    documentation.

    - Since 4.0.0
  DOC
) do |_args|
  Puppet::Parser::Functions::Error.is4x('each')
end