]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/validate_numeric.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / validate_numeric.rb
1 module Puppet::Parser::Functions
2
3   newfunction(:validate_numeric, :doc => <<-'ENDHEREDOC') do |args|
4     Validate that the first argument is a numeric value (or an array of numeric values). Abort catalog compilation if any of the checks fail.
5     
6     The second argument is optional and passes a maximum. (All elements of) the first argument has to be less or equal to this max.
7
8     The third argument is optional and passes a minimum.  (All elements of) the first argument has to be greater or equal to this min.
9     If, and only if, a minimum is given, the second argument may be an empty string or undef, which will be handled to just check
10     if (all elements of) the first argument are greater or equal to the given minimum.
11
12     It will fail if the first argument is not a numeric (Integer or Float) or array of numerics, and if arg 2 and arg 3 are not convertable to a numeric.
13
14     For passing and failing usage, see `validate_integer()`. It is all the same for validate_numeric, yet now floating point values are allowed, too.
15
16     ENDHEREDOC
17
18     # tell the user we need at least one, and optionally up to two other parameters
19     raise Puppet::ParseError, "validate_numeric(): Wrong number of arguments; must be 1, 2 or 3, got #{args.length}" unless args.length > 0 and args.length < 4
20
21     input, max, min = *args
22
23     # check maximum parameter
24     if args.length > 1
25       max = max.to_s
26       # allow max to be empty (or undefined) if we have a minimum set
27       if args.length > 2 and max == ''
28         max = nil
29       else
30         begin
31           max = Float(max)
32         rescue TypeError, ArgumentError
33           raise Puppet::ParseError, "validate_numeric(): Expected second argument to be unset or a Numeric, got #{max}:#{max.class}"
34         end
35       end
36     else
37       max = nil
38     end
39
40     # check minimum parameter
41     if args.length > 2
42       begin
43         min = Float(min.to_s)
44       rescue TypeError, ArgumentError
45         raise Puppet::ParseError, "validate_numeric(): Expected third argument to be unset or a Numeric, got #{min}:#{min.class}"
46       end
47     else
48       min = nil
49     end
50
51     # ensure that min < max
52     if min and max and min > max
53       raise Puppet::ParseError, "validate_numeric(): Expected second argument to be larger than third argument, got #{max} < #{min}"
54     end
55
56     # create lamba validator function
57     validator = lambda do |num|
58       # check input < max
59       if max and num > max
60         raise Puppet::ParseError, "validate_numeric(): Expected #{input.inspect} to be smaller or equal to #{max}, got #{input.inspect}."
61       end
62       # check input > min (this will only be checked if no exception has been raised before)
63       if min and num < min
64         raise Puppet::ParseError, "validate_numeric(): Expected #{input.inspect} to be greater or equal to #{min}, got #{input.inspect}."
65       end
66     end
67
68     # if this is an array, handle it.
69     case input
70     when Array
71       # check every element of the array
72       input.each_with_index do |arg, pos|
73         begin
74           arg = Float(arg.to_s)
75           validator.call(arg)
76         rescue TypeError, ArgumentError
77           raise Puppet::ParseError, "validate_numeric(): Expected element at array position #{pos} to be a Numeric, got #{arg.class}"
78         end
79       end
80     # for the sake of compatibility with ruby 1.8, we need extra handling of hashes
81     when Hash
82       raise Puppet::ParseError, "validate_integer(): Expected first argument to be a Numeric or Array, got #{input.class}"
83     # check the input. this will also fail any stuff other than pure, shiny integers
84     else
85       begin
86         input = Float(input.to_s)
87         validator.call(input)
88       rescue TypeError, ArgumentError
89         raise Puppet::ParseError, "validate_numeric(): Expected first argument to be a Numeric or Array, got #{input.class}"
90       end
91     end
92   end
93 end