Class: Kamaze::Project::Debug

Inherits:
Object
  • Object
show all
Defined in:
lib/kamaze/project/debug.rb

Overview

Provides colored pretty-printer automagically

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDebug

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

.warnedBoolean (protected)

Returns:

  • (Boolean)


29
30
31
# File 'lib/kamaze/project/debug.rb', line 29

def warned
  @warned
end

Instance Attribute Details

#printersArray<PP> (readonly)

Returns:

  • (Array<PP>)


33
34
35
# File 'lib/kamaze/project/debug.rb', line 33

def printers
  @printers
end

#warnedBoolean|nil (readonly, protected)

Returns:

  • (Boolean|nil)


88
89
90
# File 'lib/kamaze/project/debug.rb', line 88

def warned
  @warned
end

Class Method Details

.warned?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/kamaze/project/debug.rb', line 22

def warned?
  @warned || false
end

Instance Method Details

#available_printersArray<PP>

Get printers

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

Returns:

  • (Array<PP>)


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.

Parameters:

  • obj (Object)
  • out (IO) (defaults to: $stdout)
  • width (Fixnum) (defaults to: nil)

See Also:



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

#inflectorDry::Inflector (protected)

Returns:

  • (Dry::Inflector)


91
92
93
94
95
# File 'lib/kamaze/project/debug.rb', line 91

def inflector
  require 'dry/inflector'

  Dry::Inflector.new
end

#load_printer_requirementsself (protected)

Returns:

  • (self)

Raises:

  • (LoadError)


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_printersself (protected)

Load printers requirements (on demand)

Returns:

  • (self)


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

Parameters:

  • out (IO)

Returns:

  • (PP)


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_widthInteger (protected)

Returns:

  • (Integer)


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).

Parameters:

  • error (Exception)

Returns:

  • (Array<String>)


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.message }
  message = '%<from>s: %<mssg>s' % formats

  warn(message)

  formats.values
end

#warned?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/kamaze/project/debug.rb', line 36

def warned?
  self.class.warned?
end