Module: PutsDebuggerer

Defined in:
lib/puts_debuggerer.rb

Constant Summary collapse

HEADER_DEFAULT =
'*'*80
'*'*80
:p
'print_engine must be a valid global method symbol (e.g. :p or :puts) or lambda/proc'
ANNOUNCER_DEFAULT =
'[PD]'
FORMATTER_DEFAULT =
-> (data) {
  puts data[:header] if data[:header]
  print "#{data[:announcer]} #{data[:file]}:#{data[:line_number]}#{__format_pd_expression__(data[:pd_expression], data[:object])} "
  data[:object_printer].call
  puts data[:caller].map {|l| '     ' + l} unless data[:caller].to_a.empty?
  puts data[:footer] if data[:footer]
}
CALLER_DEPTH_ZERO =

depth includes pd + with_options method + nested block + build_pd_data method

4

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.announcerObject

Announcer (e.g. [PD]) to announce every print out with (default: “[PD]”)

Example:

PutsDebuggerer.announcer = "*** PD ***\n  "
pd (x=1)

Prints out:

*** PD ***
   /Users/User/example.rb:2
   > pd x=1
  => 1


162
163
164
# File 'lib/puts_debuggerer.rb', line 162

def announcer
  @announcer
end

.app_pathObject

Application root path to exclude when printing out file path

Example:

# File Name: /Users/User/sample_app/lib/sample.rb
PutsDebuggerer.app_path = '/Users/User/sample_app'
pd (x=1)

Prints out:

[PD] lib/sample.rb:3
   > pd x=1
  => "1"


32
33
34
# File 'lib/puts_debuggerer.rb', line 32

def app_path
  @app_path
end

.callerObject

Caller backtrace included at the end of every print out Passed an argument of true/false, nil, or depth as an integer.

  • true and -1 means include full caller backtrace

  • false and nil means do not include caller backtrace

  • depth (0-based) means include limited caller backtrace depth

Example:

# File Name: /Users/User/sample_app/lib/sample.rb
PutsDebuggerer.caller = 3
pd (x=1)

Prints out:

[PD] /Users/User/sample_app/lib/sample.rb:3
   > pd x=1
  => "1"
     /Users/User/sample_app/lib/master_samples.rb:368:in `block (3 levels) in <top (required)>'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/workspace.rb:87:in `eval'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/workspace.rb:87:in `evaluate'
     /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/context.rb:381:in `evaluate'


237
238
239
# File 'lib/puts_debuggerer.rb', line 237

def caller
  @caller
end

Footer to include at the bottom of every print out.

  • Default value is ‘nil`

  • Value ‘true` enables footer as `’*‘*80`

  • Value ‘false`, `nil`, or empty string disables footer

  • Any other string value gets set as a custom footer

Example:

PutsDebuggerer.footer = true
pd (x=1)

Prints out:

[PD] /Users/User/example.rb:2
   > pd x=1
  => "1"
********************************************************************************


88
89
90
# File 'lib/puts_debuggerer.rb', line 88

def footer
  @footer
end

.formatterObject

Formatter used in every print out Passed a data argument with the following keys:

  • :announcer (string)

  • :caller (array)

  • :file (string)

  • :footer (string)

  • :header (string)

  • :line_number (string)

  • :pd_expression (string)

  • :object (object)

  • :object_printer (proc)

NOTE: data for :object_printer is not a string, yet a proc that must be called to output value. It is a proc as it automatically handles usage of print_engine and encapsulates its details. In any case, data for :object is available should one want to avoid altogether.

Example:

PutsDebuggerer.formatter = -> (data) {
  puts "-<#{data[:announcer]}>-"
  puts "HEADER: #{data[:header]}"
  puts "FILE: #{data[:file]}"
  puts "LINE: #{data[:line_number]}"
  puts "EXPRESSION: #{data[:pd_expression]}"
  print "PRINT OUT: "
  data[:object_printer].call
  puts "CALLER: #{data[:caller].to_a.first}"
  puts "FOOTER: #{data[:footer]}"
}
pd (x=1)

Prints out:

-<[PD]>-
FILE: /Users/User/example.rb
HEADER: ********************************************************************************
LINE: 9
EXPRESSION: x=1
PRINT OUT: 1
CALLER: #/Users/User/master_examples.rb:83:in `block (3 levels) in <top (required)>'
FOOTER: ********************************************************************************


210
211
212
# File 'lib/puts_debuggerer.rb', line 210

def formatter
  @formatter
end

.headerObject

Header to include at the top of every print out.

  • Default value is ‘nil`

  • Value ‘true` enables header as `’*‘*80`

  • Value ‘false`, `nil`, or empty string disables header

  • Any other string value gets set as a custom header

Example:

PutsDebuggerer.header = true
pd (x=1)

Prints out:

********************************************************************************
[PD] /Users/User/example.rb:2
   > pd x=1
  => "1"


55
56
57
# File 'lib/puts_debuggerer.rb', line 55

def header
  @header
end

Print engine to use in object printout (e.g. ‘p`, `ap`, `pp`). It is represented by the print engine’s global method name as a symbol (e.g. ‘:ap` for awesome_print). Defaults to Ruby’s built-in ‘p` method identified by the symbol `:p`. If it finds awesome_print loaded, it defaults to `ap` as `:ap` instead.

Example:

# File Name: /Users/User/example.rb
require 'awesome_print'
PutsDebuggerer.print_engine = :ap
array = [1, [2, 3]]
pd array

Prints out:

[PD] /Users/User/example.rb:5
   > pd array
  => [
    [0] 1,
    [1] [
        [0] 2,
        [1] 3
    ]
]


129
130
131
# File 'lib/puts_debuggerer.rb', line 129

def print_engine
  @print_engine
end

Class Method Details

.caller?Boolean

Returns:

  • (Boolean)


247
248
249
# File 'lib/puts_debuggerer.rb', line 247

def caller?
  !!caller
end

.footer?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/puts_debuggerer.rb', line 100

def footer?
  !!@footer
end

.header?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/puts_debuggerer.rb', line 67

def header?
  !!@header
end

.optionsObject

Options as a hash. Useful for reading and backing up options



253
254
255
256
257
258
259
260
261
262
263
# File 'lib/puts_debuggerer.rb', line 253

def options
  {
    header: header,
    footer: footer,
    print_engine: print_engine,
    app_path: app_path,
    announcer: announcer,
    formatter: formatter,
    caller: caller
  }
end

.options=(hash) ⇒ Object

Sets options included in hash



266
267
268
269
270
# File 'lib/puts_debuggerer.rb', line 266

def options=(hash)
  hash.each do |option, value|
    send("#{option}=", value)
  end
end