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