4 # TODO(Krzysztof Wilczynski): We probably need to approach numeric values differently ...
5 module Puppet::Parser::Functions
6 newfunction(:range, :type => :rvalue, :doc => <<-DOC
8 When given range in the form of (start, stop) it will extrapolate a range as
12 the range is extrapolated as an array
16 Will return: [0,1,2,3,4,5,6,7,8,9]
19 Will return: [0,1,2,3,4,5,6,7,8,9]
20 (Zero padded strings are converted to integers automatically)
23 Will return: ["a","b","c"]
25 range("host01", "host10")
26 Will return: ["host01", "host02", ..., "host09", "host10"]
29 Will return: [0,2,4,6,8]
31 NB Be explicit in including trailing zeros. Otherwise the underlying ruby function will fail.
34 Passing a third argument will cause the generated range to step by that
37 The Puppet Language support Integer and Float ranges by using the type system. Those are suitable for
38 iterating a given number of times.
41 the step() function in Puppet for skipping values.
43 Integer[0, 9].each |$x| { notice($x) } # notices 0, 1, 2, ... 9
47 raise(Puppet::ParseError, 'range(): Wrong number of arguments given (0 for 1)') if arguments.empty?
52 step = arguments[2].nil? ? 1 : arguments[2].to_i.abs
54 type = '..' # Use the simplest type of Range available in Ruby
56 else # arguments.size == 1
59 m = value.match(%r{^(\w+)(\.\.\.?|\-)(\w+)$})
66 elsif value =~ %r{^.+$}
67 raise(Puppet::ParseError, "range(): Unable to compute range from the value: #{value}")
69 raise(Puppet::ParseError, "range(): Unknown range format: #{value}")
73 # If we were given an integer, ensure we work with one
74 if start.to_s =~ %r{^\d+$}
83 when %r{^(..|-)$} then (start..stop)
84 when '...' then (start...stop) # Exclusive of last element
87 result = range.step(step).first(1_000_000).to_a
93 # vim: set ts=2 sw=2 et :