Sha256: c1d598797c626fb1470d91ddf2b65d34694aaa91ab7fe5d993c0432421411db7

Contents?: true

Size: 1.45 KB

Versions: 20

Compression:

Stored size: 1.45 KB

Contents

module Puppet
  module Pops
    # Utility class for keeping track of the "Puppet stack", ie the file
    # and line numbers of Puppet Code that created the current context.
    #
    # To use this make a call with:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stack(file, line, receiver, message, args)
    # ```
    #
    # To get the stack call:
    #
    # ```rb
    # Puppet::Pops::PuppetStack.stacktrace
    # ```
    #
    # or
    #
    # ```rb
    # Puppet::Pops::PuppetStack.top_of_stack
    # ```
    #
    # To support testing, a given file that is an empty string, or nil
    # as well as a nil line number are supported. Such stack frames
    # will be represented with the text `unknown` and `0ยด respectively.
    module PuppetStack
      @stack = Array.new

      def self.stack(file, line, obj, message, args, &block)
        file = 'unknown' if (file.nil? || file == '')
        line = 0 if line.nil?

        result = nil
        @stack.unshift([file, line])
        begin
          if block_given?
            result = obj.send(message, *args, &block)
          else
            result = obj.send(message, *args)
          end
        ensure
          @stack.shift()
        end
        result
      end

      def self.stacktrace
        @stack.dup
      end

      # Returns an Array with the top of the puppet stack, or an empty
      # Array if there was no such entry.
      def self.top_of_stack
        @stack.first || []
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
puppet-5.5.22 lib/puppet/pops/puppet_stack.rb
puppet-5.5.22-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.22-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.22-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-5.5.21 lib/puppet/pops/puppet_stack.rb
puppet-5.5.21-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.21-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.21-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-5.5.20 lib/puppet/pops/puppet_stack.rb
puppet-5.5.20-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.20-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.20-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-5.5.19 lib/puppet/pops/puppet_stack.rb
puppet-5.5.19-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.19-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.19-universal-darwin lib/puppet/pops/puppet_stack.rb
puppet-5.5.18 lib/puppet/pops/puppet_stack.rb
puppet-5.5.18-x86-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.18-x64-mingw32 lib/puppet/pops/puppet_stack.rb
puppet-5.5.18-universal-darwin lib/puppet/pops/puppet_stack.rb