Puppet Function: simplib::debug::classtrace

Defined in:
lib/puppet/functions/simplib/debug/classtrace.rb
Function type:
Ruby 4.x API

Overview

simplib::debug::classtrace(Optional[Boolean] $print)Array

Prints out the stack of Puppet Classes and Defined Types that have been called up to this point

WARNING: Uses EXPERIMENTAL features from Puppet, may break at any time.

Parameters:

  • print (Optional[Boolean])

    Whether or not to print to the visual output

Returns:

  • (Array)

    The class trace



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
53
# File 'lib/puppet/functions/simplib/debug/classtrace.rb', line 5

Puppet::Functions.create_function(:'simplib::debug::classtrace', Puppet::Functions::InternalFunction) do

  # @param print
  #   Whether or not to print to the visual output
  #
  # @return [Array]
  #   The class trace
  dispatch :classtrace do
    scope_param()
    optional_param 'Boolean', :print
  end

  def classtrace(scope, print=true)
    class_stack = collate(scope).reverse

    if print
      msg = [
        "Simplib::Debug::Classtrace:",
        '    => ' + class_stack.join("\n    => ")
      ].join("\n")

      # This is only required when rspec is loaded
      if defined?(RSpec)
        $stderr.puts(msg)
      end

      Puppet.warning(msg)
    end

    return class_stack
  end

  private

  def collate(scope, retval=[])
    scope_s = scope.to_s
    if scope_s.start_with?('Scope(')
      scope_s = scope_s.split('Scope(').last[0..-2]
    end

    retval << scope_s

    unless scope.is_topscope?
      collate(scope.parent, retval)
    end

    retval
  end
end