Sha256: d9efb57989e6616dfd848ffc2cf2d71b12b358d1323d0d3a0b4d75a17ddb9203

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

module Kernel
  class Hash
    def to_h
      hash = extra_hash_attributes.dup

      self.class.hash_attribute_names.each do |name|
        hash[name] = __send__(name)
      end

      hash
    end
  end
end

module AwesomePrint
  module Puppet
    def self.included(base)
      base.send :alias_method, :cast_without_puppet_resource, :cast
      base.send :alias_method, :cast, :cast_with_puppet_resource
    end

    # this tells ap how to cast our object so we can be specific
    # about printing different puppet objects
    def cast_with_puppet_resource(object, type)
      cast = cast_without_puppet_resource(object, type)
      # check the object to see if it has an acestor (< ) of the specified type
      if defined?(::Puppet::Type) && (object.class < ::Puppet::Type)
        cast = :puppet_type
      elsif defined?(::Puppet::Pops::Types) && (object.class < ::Puppet::Pops::Types)
        cast = :puppet_type
      elsif defined?(::Puppet::Parser::Resource) && (object.class < ::Puppet::Parser::Resource)
        cast = :puppet_resource
      elsif /Puppet::Pops::Types/.match(object.class.to_s)
        cast = :puppet_type
      end
      cast
    end

    def awesome_puppet_resource(object)
      return '' if object.nil?
      resource_object = object.to_ral
      awesome_puppet_type(resource_object)
    end

    def awesome_puppet_type(object)
      return '' if object.nil?
      return object.to_s unless object.respond_to?(:name) && object.respond_to?(:title) && object.respond_to?(:to_hash)
      if Array.new.respond_to?(:to_h)
        # to_h is only supported in ruby 2.1+
        h = object.to_hash.merge(name: object.name, title: object.title).sort.to_h
      else
        h = object.to_hash.merge(name: object.name, title: object.title)
      end
      res_str = awesome_hash(h)
      "#{object.class} #{res_str.delete(':')}"
    end
  end
end

AwesomePrint::Formatter.send(:include, AwesomePrint::Puppet)

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
puppet-debugger-0.12.3 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.12.2 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.12.1 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.12.0 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.11.0 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.10.3 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.10.2 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.10.1 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.10.0 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.9.1 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.9.0 lib/awesome_print/ext/awesome_puppet.rb