summaryrefslogtreecommitdiff
path: root/collect_indices.rb
blob: e96c3e9c8f96b16cdbd6a9ce424592e582658459 (plain)
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
#
# collect_indices.rb
#

module Puppet::Parser::Functions
  newfunction(:collect_indices, :type => :rvalue, :doc => <<-EOS
    EOS
  ) do |arguments|

    raise(Puppet::ParseError, "Wrong number of arguments " +
      "given (#{arguments.size} for 2)") if arguments.size < 2

    array = arguments.shift

    if not array.is_a?(Array)
      raise(Puppet::ParseError, 'Requires an array to work with')
    end

    indices = *arguments # Get them all ... Pokemon ...

    if not indices or indices.empty?
      raise(Puppet::ParseError, 'You must provide indices to collect')
    end

    # In Puppet numbers are often string-encoded ...
    array = indices.collect { |i| array[i.to_i] }.compact

    return array
  end
end

# vim: set ts=2 sw=2 et :