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