source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/is_numeric.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 8 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 2.1 KB
Line 
1#
2# is_numeric.rb
3#
4
5module Puppet::Parser::Functions
6 newfunction(:is_numeric, :type => :rvalue, :doc => <<-EOS
7Returns true if the given argument is a Numeric (Integer or Float),
8or a String containing either a valid integer in decimal base 10 form, or
9a valid floating point string representation.
10
11The function recognizes only decimal (base 10) integers and float but not
12integers in hex (base 16) or octal (base 8) form.
13
14The string representation may start with a '-' (minus). If a decimal '.' is used,
15it must be followed by at least one digit.
16
17Valid examples:
18
19 77435
20 10e-12
21 -8475
22 0.2343
23 -23.561e3
24 EOS
25 ) do |arguments|
26
27 function_deprecation([:is_numeric, 'This method is deprecated, please use the stdlib validate_legacy function, with Stdlib::Compat::Numeric. There is further documentation for validate_legacy function in the README.'])
28
29 if (arguments.size != 1) then
30 raise(Puppet::ParseError, "is_numeric(): Wrong number of arguments "+
31 "given #{arguments.size} for 1")
32 end
33
34 value = arguments[0]
35
36 # Regex is taken from the lexer of puppet
37 # puppet/pops/parser/lexer.rb but modified to match also
38 # negative values and disallow invalid octal numbers or
39 # numbers prefixed with multiple 0's (except in hex numbers)
40 #
41 # TODO these parameters should be constants but I'm not sure
42 # if there is no risk to declare them inside of the module
43 # Puppet::Parser::Functions
44
45 # TODO decide if this should be used
46 # HEX numbers like
47 # 0xaa230F
48 # 0X1234009C
49 # 0x0012
50 # -12FcD
51 #numeric_hex = %r{^-?0[xX][0-9A-Fa-f]+$}
52
53 # TODO decide if this should be used
54 # OCTAL numbers like
55 # 01234567
56 # -045372
57 #numeric_oct = %r{^-?0[1-7][0-7]*$}
58
59 # Integer/Float numbers like
60 # -0.1234568981273
61 # 47291
62 # 42.12345e-12
63 numeric = %r{^-?(?:(?:[1-9]\d*)|0)(?:\.\d+)?(?:[eE]-?\d+)?$}
64
65 if value.is_a? Numeric or (value.is_a? String and (
66 value.match(numeric) #or
67 # value.match(numeric_hex) or
68 # value.match(numeric_oct)
69 ))
70 return true
71 else
72 return false
73 end
74 end
75end
Note: See TracBrowser for help on using the repository browser.