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

.warnedBoolean (protected)

Returns:

  • (Boolean)


31
32
33
# File 'lib/kamaze/project/debug.rb', line 31

def warned
  @warned
end

Instance Attribute Details

#printersArray<PP> (readonly)

Returns:

  • (Array<PP>)


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

def printers
  @printers
end

Class Method Details

.warned?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/kamaze/project/debug.rb', line 24

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


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.

Parameters:

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

See Also:



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

Load printers requirements.

Returns:

  • (self)


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

Load requirements.

Returns:

  • (self)

Raises:

  • (LoadError)


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

Parameters:

  • out (IO)

Returns:

  • (PP)


65
66
67
# File 'lib/kamaze/project/debug.rb', line 65

def printer_for(out)
  printers.fetch(out.isatty ? 0 : 1)
end

#screen_widthInteger (protected)

Returns:

  • (Integer)


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

Parameters:

  • error (Exception)

Returns:

  • (nil)


130
131
132
133
134
# 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

#warned?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/kamaze/project/debug.rb', line 38

def warned?
  self.class.warned?
end