class Kamaze::Project::Debug

Provides colored pretty-printer automagically

@see ruby-doc.org/stdlib-2.0.0/libdoc/pp/rdoc/PP.html @see github.com/pry/pry

Attributes

warned[RW]

@return [Boolean]

printers[R]

@return [Array<PP>]

Public Class Methods

new() click to toggle source
# File lib/kamaze/project/debug.rb, line 16
def initialize
  self.tap do
    @printers = load_printers.yield_self { available_printers }.freeze
  end.freeze
end
warned?() click to toggle source

@return [Boolean]

# File lib/kamaze/project/debug.rb, line 24
def warned?
  @warned || false
end

Public Instance Methods

available_printers() click to toggle source

Get printers

First printer SHOULD be the color printer, secund is the default printer

@return [Array<PP>]

# File lib/kamaze/project/debug.rb, line 74
def available_printers
  require 'dry/inflector'

  '::PP'.yield_self do |default|
    # @formatter:off
    [
      'Pry::ColorPrinter'.yield_self do |cp|
        Kernel.const_defined?(cp) ? cp : default
      end,
      default
    ].map { |n| Dry::Inflector.new.constantize(n) }.freeze
    # @formatter:on
  end
end
dump(obj, out = $stdout, width = nil) click to toggle source

Outputs obj to out in pretty printed format of width columns in width.

If out is omitted, “STDOUT“ is assumed. If width is omitted, “79“ is assumed.

@param [Object] obj @param [IO] out @param [Fixnum] width @see ruby-doc.org/stdlib-2.2.0/libdoc/pp/rdoc/PP.html

# File lib/kamaze/project/debug.rb, line 51
def dump(obj, out = $stdout, width = nil)
  width ||= screen_width || 79

  unless out.respond_to?(:isatty)
    out.singleton_class.define_method(:isatty) { false }
  end

  printer_for(out).pp(obj, out, width)
end
printer_for(out) click to toggle source

Get printer for given output

@param [IO] out @return [PP]

# File lib/kamaze/project/debug.rb, line 65
def printer_for(out)
  printers.fetch(out.isatty ? 0 : 1)
end
warned?() click to toggle source

@return [Boolean]

# File lib/kamaze/project/debug.rb, line 38
def warned?
  self.class.warned?
end

Protected Instance Methods

load_printers() click to toggle source

Load printers requirements.

@return [self]

# File lib/kamaze/project/debug.rb, line 101
def load_printers
  self.tap do
    Object.const_set('Pry', Class.new) unless Kernel.const_defined?('::Pry')

    begin
      load_requirements
    rescue LoadError => e
      self.class.__send__('warned=', !!warn_error(e)) unless warned?
    end
  end
end
load_requirements() click to toggle source

Load requirements.

@raise [LoadError] @return [self]

# File lib/kamaze/project/debug.rb, line 117
def load_requirements
  self.tap do
    # noinspection RubyLiteralArrayInspection,RubyResolve
    ['pp', 'coderay', 'pry'].each { |req| require req }
  end
end
screen_width() click to toggle source

@return [Integer]

# File lib/kamaze/project/debug.rb, line 92
def screen_width
  require 'tty/screen'

  TTY::Screen.width
end
warn_error(error) click to toggle source

Display the given exception message (followed by a newline) on STDERR

unless warnings are disabled (for example with the -W0 flag).

@param [Exception] error @return [nil]

# File lib/kamaze/project/debug.rb, line 130
def warn_error(error)
  { from: caller(1..1).first, mssg: error.message }.tap do |formats|
    return warn('%<from>s: %<mssg>s' % formats)
  end
end