]> git.donarmstrong.com Git - dsa-puppet.git/blob - 3rdparty/modules/stdlib/lib/puppet/parser/functions/is_integer.rb
upgrade to stdlib 4.6.1
[dsa-puppet.git] / 3rdparty / modules / stdlib / lib / puppet / parser / functions / is_integer.rb
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     if (arguments.size != 1) then
17       raise(Puppet::ParseError, "is_integer(): Wrong number of arguments "+
18         "given #{arguments.size} for 1")
19     end
20
21     value = arguments[0]
22
23     # Regex is taken from the lexer of puppet
24     # puppet/pops/parser/lexer.rb but modified to match also
25     # negative values and disallow numbers prefixed with multiple
26     # 0's
27     #
28     # TODO these parameter should be a constant but I'm not sure
29     # if there is no risk to declare it inside of the module
30     # Puppet::Parser::Functions
31
32     # Integer numbers like
33     # -1234568981273
34     # 47291
35     numeric = %r{^-?(?:(?:[1-9]\d*)|0)$}
36
37     if value.is_a? Integer or (value.is_a? String and value.match numeric)
38       return true
39     else
40       return false
41     end
42   end
43 end
44
45 # vim: set ts=2 sw=2 et :