Module: PutsDebuggerer
- Defined in:
- lib/puts_debuggerer.rb
Constant Summary collapse
- SOURCE_LINE_COUNT_DEFAULT =
1- HEADER_DEFAULT =
'*'*80
- WRAPPER_DEFAULT =
'*'*80
- FOOTER_DEFAULT =
'*'*80
- PRINTER_DEFAULT =
:puts- PRINTER_RAILS =
lambda do |output| puts output if Rails.env.test? Rails.logger.debug(output) end
- PRINT_ENGINE_DEFAULT =
:ap- PRINTER_MESSAGE_INVALID =
'printer must be a valid global method symbol (e.g. :puts) or lambda/proc receiving a text arg'- PRINT_ENGINE_MESSAGE_INVALID =
'print_engine must be a valid global method symbol (e.g. :p, :ap or :pp) or lambda/proc receiving an object arg'- ANNOUNCER_DEFAULT =
'[PD]'- FORMATTER_DEFAULT =
-> (data) { puts data[:wrapper] if data[:wrapper] puts data[:header] if data[:header] print "#{data[:announcer]} #{data[:file]}:#{data[:line_number]}#{" (run:#{data[:run_number]})" if data[:run_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] puts data[:wrapper] if data[:wrapper] }
- CALLER_DEPTH_ZERO =
depth includes pd + with_options method + nested block + build_pd_data method
4- OBJECT_RUN_AT =
{}
- STACK_TRACE_CALL_LINE_NUMBER_REGEX =
/\:(\d+)\:in /- STACK_TRACE_CALL_SOURCE_FILE_REGEX =
/[ ]*([^:]+)\:\d+\:in /
Class Attribute Summary collapse
-
.announcer ⇒ Object
Announcer (e.g. [PD]) to announce every print out with (default: “[PD]”).
-
.app_path ⇒ Object
Application root path to exclude when printing out file path.
-
.caller ⇒ Object
Caller backtrace included at the end of every print out Passed an argument of true/false, nil, or depth as an integer.
-
.footer ⇒ Object
Footer to include at the bottom of every print out.
-
.formatter ⇒ Object
Formatter used in every print out Passed a data argument with the following keys: * :announcer (string) * :caller (array) * :file (string) * :wrapper (string) * :footer (string) * :header (string) * :line_number (string) * :pd_expression (string) * :object (object) * :object_printer (proc) * :source_line_count (integer).
-
.header ⇒ Object
Header to include at the top of every print out.
-
.print_engine ⇒ Object
Print engine is similar to
printer, except it is focused on the scope of formatting the data object being printed (excluding metadata such as file name, line number, and expression, which are handled by theprinter). -
.printer ⇒ Object
Printer is a global method symbol or lambda expression to use in printing to the user.
-
.run_at ⇒ Object
When to run as specified by an index, array, or range.
-
.run_at_global_number ⇒ Object
Returns the value of attribute run_at_global_number.
-
.source_line_count ⇒ Object
Source Line Count.
-
.wrapper ⇒ Object
Wrapper to include at the top and bottom of every print out (both header and footer).
Class Method Summary collapse
- .caller? ⇒ Boolean
- .footer? ⇒ Boolean
- .header? ⇒ Boolean
- .increment_run_at_global_number ⇒ Object
- .increment_run_at_number(object, run_at) ⇒ Object
- .init_run_at_global_number ⇒ Object
- .init_run_at_number(object, run_at) ⇒ Object
-
.options ⇒ Object
Options as a hash.
-
.options=(hash) ⇒ Object
Sets options included in hash.
- .reset_run_at_global_number ⇒ Object
- .reset_run_at_number(object, run_at) ⇒ Object
- .reset_run_at_numbers ⇒ Object
- .run_at? ⇒ Boolean
- .run_at_number(object, run_at) ⇒ Object
- .wrapper? ⇒ Boolean
Class Attribute Details
.announcer ⇒ Object
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
256 257 258 |
# File 'lib/puts_debuggerer.rb', line 256 def announcer @announcer end |
.app_path ⇒ Object
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"
46 47 48 |
# File 'lib/puts_debuggerer.rb', line 46 def app_path @app_path end |
.caller ⇒ Object
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'
333 334 335 |
# File 'lib/puts_debuggerer.rb', line 333 def caller @caller end |
.footer ⇒ Object
Footer to include at the bottom of every print out.
-
Default value is
nil -
Value
trueenables footer as ‘’*‘*80` -
Value
false,nil, or empty string disables footer -
Any other string value gets set as a custom footer
Example:
PutsDebuggerer. = true
pd (x=1)
Prints out:
[PD] /Users/User/example.rb:2
> pd x=1
=> "1"
********************************************************************************
157 158 159 |
# File 'lib/puts_debuggerer.rb', line 157 def end |
.formatter ⇒ Object
Formatter used in every print out Passed a data argument with the following keys:
-
:announcer (string)
-
:caller (array)
-
:file (string)
-
:wrapper (string)
-
:footer (string)
-
:header (string)
-
:line_number (string)
-
:pd_expression (string)
-
:object (object)
-
:object_printer (proc)
-
:source_line_count (integer)
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: ********************************************************************************
306 307 308 |
# File 'lib/puts_debuggerer.rb', line 306 def formatter @formatter end |
.header ⇒ Object
Header to include at the top of every print out.
-
Default value is
nil -
Value
trueenables 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"
91 92 93 |
# File 'lib/puts_debuggerer.rb', line 91 def header @header end |
.print_engine ⇒ Object
Print engine is similar to printer, except it is focused on the scope of formatting the data object being printed (excluding metadata such as file name, line number, and expression, which are handled by the printer). As such, it is also a global method symbol or lambda expression. Examples of global methods are :p, :ap, and :pp. An example of a lambda expression is ‘lambda {|object| puts object.to_a.join(“ | ”)}`
Defaults to [awesome_print](github.com/awesome-print/awesome_print).
Example:
# File Name: /Users/User/example.rb
require 'awesome_print'
PutsDebuggerer.print_engine = :p
array = [1, [2, 3]]
pd array
Prints out:
[PD] /Users/User/example.rb:5
> pd array
=> [1, [2, 3]]
]
231 232 233 |
# File 'lib/puts_debuggerer.rb', line 231 def print_engine @print_engine end |
.printer ⇒ Object
Printer is a global method symbol or lambda expression to use in printing to the user. Examples of global methods are :puts and :print. An example of a lambda expression is ‘lambda {|output| Rails.logger.ap(output)}`
Defaults to :puts In Rails, it defaults to: ‘lambda {|output| Rails.logger.ap(output)}`
Example:
# File Name: /Users/User/example.rb PutsDebuggerer.printer = lambda {|output| Rails.logger.error(output)} str = “Hello” pd str
Prints out in the Rails app log as error lines:
- PD
-
/Users/User/example.rb:5
> pd str
=> Hello
192 193 194 |
# File 'lib/puts_debuggerer.rb', line 192 def printer @printer end |
.run_at ⇒ Object
When to run as specified by an index, array, or range.
-
Default value is
nilmeaning always -
Value as an Integer index (1-based) specifies at which run to print once
-
Value as an Array of indices specifies at which runs to print multiple times
-
Value as a range specifies at which runs to print multiple times, indefinitely if it ends with ..-1
Example:
PutsDebuggerer.run_at = 1
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
PutsDebuggerer.run_at = 2
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
PutsDebuggerer.run_at = [1, 3]
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
PutsDebuggerer.run_at = 3..5
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
pd (x=1) # prints nothing
PutsDebuggerer.run_at = 3...6
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints standard PD output
pd (x=1) # prints nothing
PutsDebuggerer.run_at = 3..-1
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) ... continue printing indefinitely on all subsequent runs
PutsDebuggerer.run_at = 3...-1
pd (x=1) # prints nothing
pd (x=1) # prints nothing
pd (x=1) # prints standard PD output
pd (x=1) ... continue printing indefinitely on all subsequent runs
423 424 425 |
# File 'lib/puts_debuggerer.rb', line 423 def run_at @run_at end |
.run_at_global_number ⇒ Object
Returns the value of attribute run_at_global_number.
433 434 435 |
# File 'lib/puts_debuggerer.rb', line 433 def run_at_global_number @run_at_global_number end |
.source_line_count ⇒ Object
Source Line Count.
-
Default value is
1
Example:
PutsDebuggerer.source_line_count = 2
pd (true ||
false), source_line_count: 2
Prints out:
********************************************************************************
[PD] /Users/User/example.rb:2
> pd (true ||
false), source_line_count: 2
=> "true"
68 69 70 |
# File 'lib/puts_debuggerer.rb', line 68 def source_line_count @source_line_count end |
.wrapper ⇒ Object
Wrapper to include at the top and bottom of every print out (both header and footer).
-
Default value is
nil -
Value
trueenables wrapper as ‘’*‘*80` -
Value
false,nil, or empty string disables wrapper -
Any other string value gets set as a custom wrapper
Example:
PutsDebuggerer.wrapper = true
pd (x=1)
Prints out:
[PD] /Users/User/example.rb:2
> pd x=1
=> "1"
********************************************************************************
124 125 126 |
# File 'lib/puts_debuggerer.rb', line 124 def wrapper @wrapper end |
Class Method Details
.caller? ⇒ Boolean
343 344 345 |
# File 'lib/puts_debuggerer.rb', line 343 def caller? !!caller end |
.footer? ⇒ Boolean
169 170 171 |
# File 'lib/puts_debuggerer.rb', line 169 def !! end |
.header? ⇒ Boolean
103 104 105 |
# File 'lib/puts_debuggerer.rb', line 103 def header? !!@header end |
.increment_run_at_global_number ⇒ Object
443 444 445 |
# File 'lib/puts_debuggerer.rb', line 443 def increment_run_at_global_number @run_at_global_number += 1 end |
.increment_run_at_number(object, run_at) ⇒ Object
459 460 461 |
# File 'lib/puts_debuggerer.rb', line 459 def increment_run_at_number(object, run_at) PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]] += 1 end |
.init_run_at_global_number ⇒ Object
439 440 441 |
# File 'lib/puts_debuggerer.rb', line 439 def init_run_at_global_number @run_at_global_number = 1 end |
.init_run_at_number(object, run_at) ⇒ Object
455 456 457 |
# File 'lib/puts_debuggerer.rb', line 455 def init_run_at_number(object, run_at) PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]] = 1 end |
.options ⇒ Object
Options as a hash. Useful for reading and backing up options
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/puts_debuggerer.rb', line 349 def { header: header, wrapper: wrapper, footer: , printer: printer, print_engine: print_engine, source_line_count: source_line_count, app_path: app_path, announcer: announcer, formatter: formatter, caller: caller, run_at: run_at } end |
.options=(hash) ⇒ Object
Sets options included in hash
366 367 368 369 370 |
# File 'lib/puts_debuggerer.rb', line 366 def (hash) hash.each do |option, value| send("#{option}=", value) end end |
.reset_run_at_global_number ⇒ Object
447 448 449 |
# File 'lib/puts_debuggerer.rb', line 447 def reset_run_at_global_number @run_at_global_number = nil end |
.reset_run_at_number(object, run_at) ⇒ Object
463 464 465 |
# File 'lib/puts_debuggerer.rb', line 463 def reset_run_at_number(object, run_at) PutsDebuggerer::OBJECT_RUN_AT.delete([object, run_at]) end |
.reset_run_at_numbers ⇒ Object
467 468 469 |
# File 'lib/puts_debuggerer.rb', line 467 def reset_run_at_numbers PutsDebuggerer::OBJECT_RUN_AT.clear end |
.run_at? ⇒ Boolean
429 430 431 |
# File 'lib/puts_debuggerer.rb', line 429 def run_at? !!@run_at end |
.run_at_number(object, run_at) ⇒ Object
451 452 453 |
# File 'lib/puts_debuggerer.rb', line 451 def run_at_number(object, run_at) PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]] end |
.wrapper? ⇒ Boolean
136 137 138 |
# File 'lib/puts_debuggerer.rb', line 136 def wrapper? !!@wrapper end |