Sha256: e1b827bd76963b34b74efa15ca53b0e6ff3286ad01d94227106f3a839dcef2ee

Contents?: true

Size: 1.96 KB

Versions: 5

Compression:

Stored size: 1.96 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(JSON.parse(h.to_json)) #converting to json removes symbols
      "#{object.class} #{res_str}"
    end
  end
end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
puppet-debugger-0.15.2 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.15.1 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.15.0 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.14.0 lib/awesome_print/ext/awesome_puppet.rb
puppet-debugger-0.13.0 lib/awesome_print/ext/awesome_puppet.rb