Class: Kamaze::Project::Debug
Overview
Provides colored pretty-printer automagically
Class Attribute Summary collapse
- .warned ⇒ Boolean protected
Instance Attribute Summary collapse
- #printers ⇒ Array<PP> readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#available_printers ⇒ Array<PP>
Get printers.
-
#dump(obj, out = $stdout, width = nil) ⇒ Object
Outputs obj to out in pretty printed format of width columns in width.
-
#initialize ⇒ Debug
constructor
A new instance of Debug.
-
#load_printers ⇒ self
protected
Load printers requirements.
-
#load_requirements ⇒ self
protected
Load requirements.
-
#printer_for(out) ⇒ PP
Get printer for given output.
- #screen_width ⇒ Integer protected
-
#warn_error(error) ⇒ nil
protected
Display the given exception message (followed by a newline) on STDERR.
- #warned? ⇒ Boolean
Constructor Details
#initialize ⇒ Debug
Returns a new instance of Debug.
16 17 18 19 20 |
# File 'lib/kamaze/project/debug.rb', line 16 def initialize self.tap do @printers = load_printers.yield_self { available_printers }.freeze end.freeze end |
Class Attribute Details
.warned ⇒ Boolean (protected)
31 32 33 |
# File 'lib/kamaze/project/debug.rb', line 31 def warned @warned end |
Instance Attribute Details
#printers ⇒ Array<PP> (readonly)
35 36 37 |
# File 'lib/kamaze/project/debug.rb', line 35 def printers @printers end |
Class Method Details
.warned? ⇒ Boolean
24 25 26 |
# File 'lib/kamaze/project/debug.rb', line 24 def warned? @warned || false end |
Instance Method Details
#available_printers ⇒ Array<PP>
Get printers
First printer SHOULD be the color printer, secund is the default printer
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# 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) ⇒ Object
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.
51 52 53 54 55 56 57 58 59 |
# 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 |
#load_printers ⇒ self (protected)
Load printers requirements.
101 102 103 104 105 106 107 108 109 110 111 |
# 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 ⇒ self (protected)
Load requirements.
117 118 119 120 121 122 |
# 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 |
#printer_for(out) ⇒ PP
Get printer for given output
65 66 67 |
# File 'lib/kamaze/project/debug.rb', line 65 def printer_for(out) printers.fetch(out.isatty ? 0 : 1) end |
#screen_width ⇒ Integer (protected)
92 93 94 95 96 |
# File 'lib/kamaze/project/debug.rb', line 92 def screen_width require 'tty/screen' TTY::Screen.width end |
#warn_error(error) ⇒ nil (protected)
Display the given exception message (followed by a newline) on STDERR
unless warnings are disabled (for example with the -W0 flag).
130 131 132 133 134 |
# File 'lib/kamaze/project/debug.rb', line 130 def warn_error(error) { from: caller(1..1).first, mssg: error. }.tap do |formats| return warn('%<from>s: %<mssg>s' % formats) end end |
#warned? ⇒ Boolean
38 39 40 |
# File 'lib/kamaze/project/debug.rb', line 38 def warned? self.class.warned? end |