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
@return [Boolean]
@return [Array<PP>]
Public Class Methods
# File lib/kamaze/project/debug.rb, line 16 def initialize self.tap do @printers = load_printers.yield_self { available_printers }.freeze end.freeze end
@return [Boolean]
# File lib/kamaze/project/debug.rb, line 24 def warned? @warned || false end
Public Instance Methods
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
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
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
@return [Boolean]
# File lib/kamaze/project/debug.rb, line 38 def warned? self.class.warned? end
Protected Instance Methods
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.
@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
@return [Integer]
# File lib/kamaze/project/debug.rb, line 92 def screen_width require 'tty/screen' TTY::Screen.width end
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