module Puppet::Pops::Utils
Public Class Methods
is the name absolute (i.e. starts with ::)
# File lib/puppet/pops/utils.rb 108 def self.is_absolute? name 109 name.start_with? "::" 110 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 10 def self.is_numeric?(o) 11 case o 12 when Numeric 13 true 14 else 15 !!Patterns::NUMERIC.match(relativize_name(o.to_s)) 16 end 17 end
Convert a match from Patterns::NUMERIC to floating point value if possible
# File lib/puppet/pops/utils.rb 21 def self.match_to_fp(match) 22 if match[5].to_s.length > 0 23 # Use default radix (default is decimal == 10) for floats 24 # Do not convert a value that is 0 raised to 10^somevalue to float - the value is always 0 25 # i.e. 0000.0e1, 0e1, 0.0000e1 26 if Integer(match[4]) == 0 && match[5] =~ /\A\.?0*[eE].*\z/ 27 nil 28 else 29 fp_value = Float(match[2]) 30 if fp_value != Float::INFINITY 31 match[1] == '-' ? -fp_value : fp_value 32 else 33 nil 34 end 35 end 36 end 37 end
# File lib/puppet/pops/utils.rb 112 def self.name_to_segments name 113 name.split("::") 114 end
# File lib/puppet/pops/utils.rb 116 def self.relativize_name name 117 is_absolute?(name) ? name[2..-1] : name 118 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 85 def self.to_n o 86 begin 87 case o 88 when String 89 match = Patterns::NUMERIC.match(relativize_name(o)) 90 if !match 91 nil 92 elsif match[5].to_s.length > 0 93 match_to_fp(match) 94 else 95 match[1] == '-' ? -Integer(match[2]) : Integer(match[2]) 96 end 97 when Numeric 98 o 99 else 100 nil 101 end 102 rescue ArgumentError 103 nil 104 end 105 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 46 def self.to_n_with_radix o 47 begin 48 case o 49 when String 50 match = Patterns::NUMERIC.match(relativize_name(o)) 51 if !match 52 nil 53 elsif match[5].to_s.length > 0 54 fp_value = match_to_fp(match) 55 fp_value.nil? ? nil : [fp_value, 10] 56 else 57 # Set radix (default is decimal == 10) 58 radix = 10 59 if match[3].to_s.length > 0 60 radix = 16 61 elsif match[4].to_s.length > 1 && match[4][0,1] == '0' 62 radix = 8 63 end 64 # Ruby 1.8.7 does not have a second argument to Kernel method that creates an 65 # integer from a string, it relies on the prefix 0x, 0X, 0 (and unsupported in puppet binary 'b') 66 # We have the correct string here, match[2] is safe to parse without passing on radix 67 match[1] == '-' ? [-Integer(match[2]), radix] : [Integer(match[2]), radix] 68 end 69 when Numeric 70 # Impossible to calculate radix, assume decimal 71 [o, 10] 72 else 73 nil 74 end 75 rescue ArgumentError 76 nil 77 end 78 end