Module: Puppet::Pops::Utils

Defined in:
lib/puppet/pops/utils.rb

Overview

Provides utility methods

Class Method Summary collapse

Class Method Details

.find_adapter(o, adapter) ⇒ Object

Finds an adapter for o or for one of its containers, or nil, if none of the containers was adapted with the given adapter. This method can only be used with objects that respond to :eContainer. with true, and Adaptable#adapters.



98
99
100
101
102
103
# File 'lib/puppet/pops/utils.rb', line 98

def self.find_adapter(o, adapter)
  return nil unless o
  a = adapter.get(o)
  return a if a
  return find_adapter(o.eContainer, adapter)
end

.is_absolute?(name) ⇒ Boolean

is the name absolute (i.e. starts with ::)

Returns:

  • (Boolean)


81
82
83
# File 'lib/puppet/pops/utils.rb', line 81

def self.is_absolute? name
  name.start_with? "::"
end

.is_numeric?(o) ⇒ Boolean

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.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
# File 'lib/puppet/pops/utils.rb', line 8

def self.is_numeric?(o)
  case o
  when Numeric, Integer, Fixnum, Float
    !!o
  else
    !!Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o.to_s))
  end
end

.name_to_segments(name) ⇒ Object



85
86
87
# File 'lib/puppet/pops/utils.rb', line 85

def self.name_to_segments name
  name.split("::")
end

.relativize_name(name) ⇒ Object



89
90
91
# File 'lib/puppet/pops/utils.rb', line 89

def self.relativize_name name
  is_absolute?(name) ? name[2..-1] : name
end

.to_n(o) ⇒ Object

To Numeric (or already numeric) Returns nil if value is not numeric, else an Integer or Float A leading ‘::’ is accepted (and ignored)



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/puppet/pops/utils.rb', line 58

def self.to_n o
  begin
    case o
    when String
      match = Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o))
      if !match
        nil
      elsif match[3].to_s.length > 0
        Float(match[0])
      else
        Integer(match[0])
      end
    when Numeric, Fixnum, Integer, Float
      o
    else
      nil
    end
  rescue ArgumentError
    nil
  end
end

.to_n_with_radix(o) ⇒ Array<Number, Integer>?

To LiteralNumber with radix, or nil if not a number. If the value is already a number it is returned verbatim with a radix of 10.

Parameters:

  • o (String, Number)

    a string containing a number in octal, hex, integer (decimal) or floating point form

Returns:

  • (Array<Number, Integer>, nil)

    array with converted number and radix, or nil if not possible to convert



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/pops/utils.rb', line 23

def self.to_n_with_radix o
  begin
    case o
    when String
      match = Puppet::Pops::Patterns::NUMERIC.match(relativize_name(o))
      if !match
        nil
      elsif match[3].to_s.length > 0
        # Use default radix (default is decimal == 10) for floats
        [Float(match[0]), 10]
      else
        # Set radix (default is decimal == 10)
        radix = 10
        if match[1].to_s.length > 0
          radix = 16
        elsif match[2].to_s.length > 1 && match[2][0] == '0'
          radix = 8
        end
        [Integer(match[0], radix), radix]
      end
    when Numeric, Fixnum, Integer, Float
      # Impossible to calculate radix, assume decimal
      [o, 10]
    else
      nil
    end
  rescue ArgumentError
    nil
  end
end