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
- #warned ⇒ Boolean|nil readonly protected
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.
- #inflector ⇒ Dry::Inflector protected
-
#initialize ⇒ Debug
constructor
A new instance of Debug.
- #load_printer_requirements ⇒ self protected
-
#load_printers ⇒ self
protected
Load printers requirements (on demand).
-
#printer_for(out) ⇒ PP
Get printer for given output.
- #screen_width ⇒ Integer protected
-
#warn_error(error) ⇒ Array<String>
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 |
# File 'lib/kamaze/project/debug.rb', line 16 def initialize @printers = available_printers end |
Class Attribute Details
.warned ⇒ Boolean (protected)
29 30 31 |
# File 'lib/kamaze/project/debug.rb', line 29 def warned @warned end |
Instance Attribute Details
#printers ⇒ Array<PP> (readonly)
33 34 35 |
# File 'lib/kamaze/project/debug.rb', line 33 def printers @printers end |
#warned ⇒ Boolean|nil (readonly, protected)
88 89 90 |
# File 'lib/kamaze/project/debug.rb', line 88 def warned @warned end |
Class Method Details
.warned? ⇒ Boolean
22 23 24 |
# File 'lib/kamaze/project/debug.rb', line 22 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
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/kamaze/project/debug.rb', line 70 def available_printers load_printers default = '::PP' [ proc do target = '::Pry::ColorPrinter' Kernel.const_defined?(target) ? target : default end.call, default ].map { |n| inflector.constantize(n) }.freeze 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.
49 50 51 52 53 |
# File 'lib/kamaze/project/debug.rb', line 49 def dump(obj, out = $stdout, width = nil) width ||= screen_width || 79 printer_for(out).pp(obj, out, width) end |
#inflector ⇒ Dry::Inflector (protected)
91 92 93 94 95 |
# File 'lib/kamaze/project/debug.rb', line 91 def inflector require 'dry/inflector' Dry::Inflector.new end |
#load_printer_requirements ⇒ self (protected)
121 122 123 124 125 126 127 128 |
# File 'lib/kamaze/project/debug.rb', line 121 def load_printer_requirements ['pp', 'coderay', 'pry/pager', 'pry/color_printer'].each { |req| require req } self end |
#load_printers ⇒ self (protected)
Load printers requirements (on demand)
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/kamaze/project/debug.rb', line 107 def load_printers Object.const_set('Pry', Class.new) unless Kernel.const_defined?('::Pry') begin load_printer_requirements rescue LoadError => e self.class.__send__('warned=', !!warn_error(e)) unless warned? end self end |
#printer_for(out) ⇒ PP
Get printer for given output
59 60 61 62 63 |
# File 'lib/kamaze/project/debug.rb', line 59 def printer_for(out) out_tty = out.respond_to?(:isatty) and out.isatty printers[out_tty ? 0 : 1] end |
#screen_width ⇒ Integer (protected)
98 99 100 101 102 |
# File 'lib/kamaze/project/debug.rb', line 98 def screen_width require 'tty/screen' TTY::Screen.width end |
#warn_error(error) ⇒ Array<String> (protected)
Display the given exception message (followed by a newline) on STDERR
unless warnings are disabled (for example with the -W0 flag).
136 137 138 139 140 141 142 143 |
# File 'lib/kamaze/project/debug.rb', line 136 def warn_error(error) formats = { from: caller(1..1).first, mssg: error. } = '%<from>s: %<mssg>s' % formats warn() formats.values end |
#warned? ⇒ Boolean
36 37 38 |
# File 'lib/kamaze/project/debug.rb', line 36 def warned? self.class.warned? end |