]> git.donarmstrong.com Git - dsa-puppet.git/blob - modules/stdlib/lib/puppet/parser/functions/values_at.rb
upgrade to concat 2.0.0
[dsa-puppet.git] / modules / stdlib / lib / puppet / parser / functions / values_at.rb
1 #
2 # values_at.rb
3 #
4
5 module Puppet::Parser::Functions
6   newfunction(:values_at, :type => :rvalue, :doc => <<-EOS
7 Finds value inside an array based on location.
8
9 The first argument is the array you want to analyze, and the second element can
10 be a combination of:
11
12 * A single numeric index
13 * A range in the form of 'start-stop' (eg. 4-9)
14 * An array combining the above
15
16 *Examples*:
17
18     values_at(['a','b','c'], 2)
19
20 Would return ['c'].
21
22     values_at(['a','b','c'], ["0-1"])
23
24 Would return ['a','b'].
25
26     values_at(['a','b','c','d','e'], [0, "2-3"])
27
28 Would return ['a','c','d'].
29     EOS
30   ) do |arguments|
31
32     raise(Puppet::ParseError, "values_at(): Wrong number of " +
33       "arguments given (#{arguments.size} for 2)") if arguments.size < 2
34
35     array = arguments.shift
36
37     unless array.is_a?(Array)
38       raise(Puppet::ParseError, 'values_at(): Requires array to work with')
39     end
40
41     indices = [arguments.shift].flatten() # Get them all ... Pokemon ...
42
43     if not indices or indices.empty?
44       raise(Puppet::ParseError, 'values_at(): You must provide ' +
45         'at least one positive index to collect')
46     end
47
48     result       = []
49     indices_list = []
50
51     indices.each do |i|
52       if m = i.match(/^(\d+)(\.\.\.?|\-)(\d+)$/)
53         start = m[1].to_i
54         stop  = m[3].to_i
55
56         type = m[2]
57
58         if start > stop
59           raise(Puppet::ParseError, 'values_at(): Stop index in ' +
60             'given indices range is smaller than the start index')
61         elsif stop > array.size - 1 # First element is at index 0 is it not?
62           raise(Puppet::ParseError, 'values_at(): Stop index in ' +
63             'given indices range exceeds array size')
64         end
65
66         range = case type
67           when /^(\.\.|\-)$/ then (start .. stop)
68           when /^(\.\.\.)$/  then (start ... stop) # Exclusive of last element ...
69         end
70
71         range.each { |i| indices_list << i.to_i }
72       else
73         # Only positive numbers allowed in this case ...
74         if not i.match(/^\d+$/)
75           raise(Puppet::ParseError, 'values_at(): Unknown format ' +
76             'of given index')
77         end
78
79         # In Puppet numbers are often string-encoded ...
80         i = i.to_i
81
82         if i > array.size - 1 # Same story.  First element is at index 0 ...
83           raise(Puppet::ParseError, 'values_at(): Given index ' +
84             'exceeds array size')
85         end
86
87         indices_list << i
88       end
89     end
90
91     # We remove nil values as they make no sense in Puppet DSL ...
92     result = indices_list.collect { |i| array[i] }.compact
93
94     return result
95   end
96 end
97
98 # vim: set ts=2 sw=2 et :