Suggest different variables to use if we want to tunnel both v4 and v6
[mirror/dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_integer.rb
1 #
2 # is_integer.rb
3 #
4 module Puppet::Parser::Functions
5   newfunction(:is_integer, :type => :rvalue, :doc => <<-DOC
6     Returns true if the variable passed to this function is an Integer or
7     a decimal (base 10) integer in String form. The string may
8     start with a '-' (minus). A value of '0' is allowed, but a leading '0' digit may not
9     be followed by other digits as this indicates that the value is octal (base 8).
10
11     If given any other argument `false` is returned.
12     DOC
13              ) do |arguments|
14
15     function_deprecation([:is_integer, 'This method is deprecated, please use the stdlib validate_legacy function,
16                             with Stdlib::Compat::Integer. There is further documentation for validate_legacy function in the README.'])
17
18     if arguments.size != 1
19       raise(Puppet::ParseError, "is_integer(): Wrong number of arguments given #{arguments.size} for 1")
20     end
21
22     value = arguments[0]
23
24     # Regex is taken from the lexer of puppet
25     # puppet/pops/parser/lexer.rb but modified to match also
26     # negative values and disallow numbers prefixed with multiple
27     # 0's
28     #
29     # TODO these parameter should be a constant but I'm not sure
30     # if there is no risk to declare it inside of the module
31     # Puppet::Parser::Functions
32
33     # Integer numbers like
34     # -1234568981273
35     # 47291
36     numeric = %r{^-?(?:(?:[1-9]\d*)|0)$}
37
38     return true if value.is_a?(Integer) || (value.is_a?(String) && value.match(numeric))
39     return false
40   end
41 end
42
43 # vim: set ts=2 sw=2 et :