module Puppet::Pops::Utils
Public Class Methods
is the name absolute (i.e. starts with ::)
# File lib/puppet/pops/utils.rb 107 def self.is_absolute? name 108 name.start_with? "::".freeze 109 end
Can the given o be converted to numeric? (or is numeric already) Accepts a leading '::' Returns a boolean if the value is numeric If testing if value can be converted it is more efficient to call {#to_n} or {#to_n_with_radix} directly and check if value is nil.
# File lib/puppet/pops/utils.rb 9 def self.is_numeric?(o) 10 case o 11 when Numeric 12 true 13 else 14 !!Patterns::NUMERIC.match(relativize_name(o.to_s)) 15 end 16 end
Convert a match from Patterns::NUMERIC to floating point value if possible
# File lib/puppet/pops/utils.rb 20 def self.match_to_fp(match) 21 if match[5].to_s.length > 0 22 # Use default radix (default is decimal == 10) for floats 23 # Do not convert a value that is 0 raised to 10^somevalue to float - the value is always 0 24 # i.e. 0000.0e1, 0e1, 0.0000e1 25 if Integer(match[4]) == 0 && match[5] =~ /\A\.?0*[eE].*\z/ 26 nil 27 else 28 fp_value = Float(match[2]) 29 if fp_value != Float::INFINITY 30 match[1] == '-' ? -fp_value : fp_value 31 else 32 nil 33 end 34 end 35 end 36 end
# File lib/puppet/pops/utils.rb 111 def self.name_to_segments name 112 name.split("::".freeze) 113 end
# File lib/puppet/pops/utils.rb 115 def self.relativize_name name 116 is_absolute?(name) ? name[2..-1] : name 117 end
To Numeric (or already numeric) Returns nil if value is not numeric, else an Integer or Float. A String may have an optional sign.
A leading '::' is accepted (and ignored)
# File lib/puppet/pops/utils.rb 84 def self.to_n o 85 begin 86 case o 87 when String 88 match = Patterns::NUMERIC.match(relativize_name(o)) 89 if !match 90 nil 91 elsif match[5].to_s.length > 0 92 match_to_fp(match) 93 else 94 match[1] == '-' ? -Integer(match[2]) : Integer(match[2]) 95 end 96 when Numeric 97 o 98 else 99 nil 100 end 101 rescue ArgumentError 102 nil 103 end 104 end
To Numeric with radix, or nil if not a number. If the value is already Numeric it is returned verbatim with a radix of 10. @param o [String, Number] a string containing a number in octal, hex, integer (decimal) or floating point form
with optional sign +/-
@return [Array<Number, Integer>, nil] array with converted number and radix, or nil if not possible to convert @api public
# File lib/puppet/pops/utils.rb 45 def self.to_n_with_radix o 46 begin 47 case o 48 when String 49 match = Patterns::NUMERIC.match(relativize_name(o)) 50 if !match 51 nil 52 elsif match[5].to_s.length > 0 53 fp_value = match_to_fp(match) 54 fp_value.nil? ? nil : [fp_value, 10] 55 else 56 # Set radix (default is decimal == 10) 57 radix = 10 58 if match[3].to_s.length > 0 59 radix = 16 60 elsif match[4].to_s.length > 1 && match[4][0,1] == '0' 61 radix = 8 62 end 63 # Ruby 1.8.7 does not have a second argument to Kernel method that creates an 64 # integer from a string, it relies on the prefix 0x, 0X, 0 (and unsupported in puppet binary 'b') 65 # We have the correct string here, match[2] is safe to parse without passing on radix 66 match[1] == '-' ? [-Integer(match[2]), radix] : [Integer(match[2]), radix] 67 end 68 when Numeric 69 # Impossible to calculate radix, assume decimal 70 [o, 10] 71 else 72 nil 73 end 74 rescue ArgumentError 75 nil 76 end 77 end