puts_debuggerer 0.7.1 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +121 -28
  3. data/lib/puts_debuggerer.rb +72 -24
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 15dfcef6a6e1e5beb4239710ba4b0fa7e7577b1b
4
- data.tar.gz: cf41610d09b62afd8792dfe7665cca246b89402c
3
+ metadata.gz: 0f63870f52c5a1fcfde9556bca624180ad098c43
4
+ data.tar.gz: 5e4b9f5af1b8f2d3daabb519ecc5bb490779e7c8
5
5
  SHA512:
6
- metadata.gz: e5b7a1e346bd3e8d084d367fabbacaf396b9117ead8c071c3cabaf2d6645222c2e521c63876e73a710fe740121472559a174a1e44b5f54dc067199293113ce8b
7
- data.tar.gz: eac965aa078e785bc9a99b337e981dc43820af6a8c4a7bb1de8a5567b9937359315d303c221fedfa0ebd832658aac531deea44332de07d057c8ae71d960a7888
6
+ metadata.gz: 29c620c7faec7a524ee775cf3108aaad57a21ec3891763733a5222b552b7dc6507881b0b8dd173b42dacd668783d3d0fd95d4b63123943b02509b0cfb432ad62
7
+ data.tar.gz: f53bdfb0185323d065bca4dc15f8bbbf319d28c545b03441102f91d05ad5db3bb336997598334e0a5682d0aec972f91ffffa7df4c17828b15861ba6ef4b87a85
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # puts_debuggerer v0.7.1
1
+ # puts_debuggerer v0.8.0
2
2
  [![Gem Version](https://badge.fury.io/rb/puts_debuggerer.svg)](http://badge.fury.io/rb/puts_debuggerer)
3
3
  [![Build Status](https://travis-ci.org/AndyObtiva/puts_debuggerer.svg?branch=master)](https://travis-ci.org/AndyObtiva/puts_debuggerer)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/AndyObtiva/puts_debuggerer/badge.svg?branch=master)](https://coveralls.io/github/AndyObtiva/puts_debuggerer?branch=master)
@@ -8,15 +8,79 @@ statements like an umbrella in a stormy day.
8
8
  Why not make it official and have puts debugging become its own perfectly
9
9
  legitimate thing?!!
10
10
 
11
- Enter puts_debuggerer. A guilt-free puts debugger Ruby gem FTW!
12
-
13
- In other words, puts_debuggerer is a Ruby library that provides improved puts debugging, automatically displaying bonus useful information such as source line numbers and source code, among many other goodies (mentioned in the README.)
11
+ Enter puts_debuggerer. A guilt-free puts debugging Ruby gem FTW!
14
12
 
15
13
  Partially inspired (only partially ;) by this blog post:
16
14
  https://tenderlovemaking.com/2016/02/05/i-am-a-puts-debuggerer.html
17
15
  (Credit to Tenderlove.)
18
16
 
19
- Love PD?! Why not promote with [merchandise](https://www.zazzle.com/i+heart+pd+gifts)?
17
+ Love PD?! Why not promote with [merchandise](https://www.zazzle.com/i+heart+pd+gifts)? Maybe I'll buy everyone wearing the merchandise a beer at software conferences.
18
+
19
+ ## Background
20
+
21
+ It can be quite frustrating to lose puts statements in a large output or log file. One way to help find them is add a header (e.g. `puts "The Order Total"`) or an announcer (e.g. `puts '*'*80`) before every puts statement, leading to repetitive wasteful effort.
22
+
23
+ puts_debuggerer automates that work via the short and simple `pd` command, automatically printing meaningful headers for output.
24
+
25
+ Example without pd:
26
+
27
+ ```ruby
28
+ puts order_total
29
+ ```
30
+
31
+ Output:
32
+ ```
33
+ 195.50
34
+ ```
35
+
36
+ Which gets lost in a logging stream such as:
37
+
38
+ ```
39
+ (2.7ms) CREATE TABLE "ar_internal_metadata" ("key" character varying PRIMARY KEY, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
40
+ ActiveRecord::InternalMetadata Load (0.4ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
41
+ (0.2ms) BEGIN
42
+ SQL (0.3ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key" [["key", "environment"], ["value", "development"], ["created_at", 2017-08-24 22:56:52 UTC], ["updated_at", 2017-08-24 22:56:52 UTC]]
43
+ (0.3ms) COMMIT
44
+ 195.50
45
+ ActiveRecord::InternalMetadata Load (0.3ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
46
+ (0.2ms) BEGIN
47
+ (0.2ms) COMMIT
48
+ ```
49
+
50
+ Example with pd:
51
+
52
+ ```ruby
53
+ pd order_total
54
+ ```
55
+
56
+ Output:
57
+
58
+ ```
59
+ [PD] /Users/User/ordering/order.rb:39
60
+ > pd order_total
61
+ => 195.50
62
+ ```
63
+
64
+ This is not only easy to locate in a logging stream such as the one below, but also includes the `order_total` variable for easy findability among other pd statements.
65
+
66
+ ```
67
+ (2.7ms) CREATE TABLE "ar_internal_metadata" ("key" character varying PRIMARY KEY, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
68
+ ActiveRecord::InternalMetadata Load (0.4ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
69
+ [PD] /Users/User/ordering/order.rb:39
70
+ > pd order_total
71
+ => 195.50
72
+ (0.2ms) BEGIN
73
+ SQL (0.3ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key" [["key", "environment"], ["value", "development"], ["created_at", 2017-08-24 22:56:52 UTC], ["updated_at", 2017-08-24 22:56:52 UTC]]
74
+ (0.3ms) COMMIT
75
+ [PD] /Users/User/ordering/order.rb:72
76
+ > pd order_subtotal
77
+ => 181.00
78
+ ActiveRecord::InternalMetadata Load (0.3ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
79
+ (0.2ms) BEGIN
80
+ (0.2ms) COMMIT
81
+ ```
82
+
83
+ And it is easy to search for using the `[PD]` announcer.
20
84
 
21
85
  ## Instructions
22
86
 
@@ -25,7 +89,7 @@ Love PD?! Why not promote with [merchandise](https://www.zazzle.com/i+heart+pd+g
25
89
  Add the following to bundler's `Gemfile`.
26
90
 
27
91
  ```ruby
28
- gem 'puts_debuggerer', '~> 0.7.1'
92
+ gem 'puts_debuggerer', '~> 0.8.0'
29
93
  ```
30
94
 
31
95
  This is the recommended way for [Rails](rubyonrails.org) apps. Optionally, you may create an initializer under `config/initializers` named `puts_debuggerer_options.rb` to enable further customizations as per the [Options](#options) section below.
@@ -35,7 +99,7 @@ This is the recommended way for [Rails](rubyonrails.org) apps. Optionally, you m
35
99
  Or manually install and require library.
36
100
 
37
101
  ```bash
38
- gem install puts_debuggerer -v0.7.1
102
+ gem install puts_debuggerer -v0.8.0
39
103
  ```
40
104
 
41
105
  ```ruby
@@ -44,11 +108,9 @@ require 'puts_debuggerer'
44
108
 
45
109
  ### Usage
46
110
 
47
- Simply invoke global `pd` method anywhere in your code passing an object or an expression argument.
111
+ First, add `pd` method anywhere in your code to display details about an object or expression (if you're used to awesome_print, you're in luck! puts_debuggerer includes awesome_print as the default print engine for output).
48
112
 
49
- It will then provide helpful debugging information by printing the source file, line number, and source code in addition to output (works even in IRB).
50
-
51
- Example Code:
113
+ Example:
52
114
 
53
115
  ```ruby
54
116
  # /Users/User/finance_calculator_app/pd_test.rb # line 1
@@ -57,7 +119,7 @@ pd "Show me the source of the bug: #{bug}" # line 3
57
119
  pd "Show me the result of the calculation: #{(12.0/3.0)}" # line 4
58
120
  ```
59
121
 
60
- Example Printout:
122
+ Output:
61
123
 
62
124
  ```bash
63
125
  [PD] /Users/User/finance_calculator_app/pd_test.rb:3
@@ -68,17 +130,18 @@ Example Printout:
68
130
  => "Show me the result of the calculation: 4.0"
69
131
  ```
70
132
 
71
- Quickly locate printed lines using Find feature (e.g. CTRL+F) by looking for:
133
+ In addition to the main object/expression output, you get to see the source file name, line number, and source code to help you debug and troubleshoot problems quicker (it even works in IRB).
134
+
135
+ Second, quickly locate printed lines using the Find feature (e.g. CTRL+F) by looking for:
72
136
  * [PD]
73
137
  * file:line_number
74
138
  * known ruby expression.
75
139
 
76
- This gives you the added benefit of easily removing your `pd` statements later
77
- on once done debugging.
140
+ Third, easily remove your ` pd ` statements via the source code Find feature once done debugging.
78
141
 
79
142
  Note that `pd` returns the passed in object or expression argument unchanged, permitting debugging with shorter syntax than tap, and supporting chaining of extra method invocations afterward.
80
143
 
81
- Example Code:
144
+ Example:
82
145
 
83
146
  ```ruby
84
147
  # /Users/User/greeting_app/pd_test.rb # line 1
@@ -86,7 +149,7 @@ name = 'Robert' # line 2
86
149
  greeting = "Hello #{pd(name)}" # line 3
87
150
  ```
88
151
 
89
- Example Printout:
152
+ Output:
90
153
 
91
154
  ```bash
92
155
  [PD] /Users/User/greeting_app/pd_test.rb:3
@@ -216,19 +279,50 @@ Prints out:
216
279
  ********************************************************************************
217
280
  ```
218
281
 
282
+ #### `PutsDebuggerer.printer`
283
+ (default = `:puts`)
284
+
285
+ Printer is a global method symbol or lambda expression to use in printing to the user.
286
+ Examples of global methods are `:puts` and `:print`.
287
+ An example of a lambda expression is `lambda {|output| Rails.logger.info(output)}`
288
+
289
+ Defaults to `:puts`
290
+ In Rails, it defaults to: `lambda {|output| Rails.logger.debug(output)}`
291
+
292
+ Example:
293
+
294
+ ```ruby
295
+ # File Name: /Users/User/example.rb
296
+ PutsDebuggerer.printer = lambda {|output| Rails.logger.error(output)}
297
+ str = "Hello"
298
+ pd str
299
+ ```
300
+
301
+ Prints out in the Rails app log as error lines:
302
+
303
+ ```bash
304
+ [PD] /Users/User/example.rb:5
305
+ > pd str
306
+ => Hello
307
+ ```
308
+
219
309
  #### `PutsDebuggerer.print_engine`
220
310
  (default = `:ap`)
221
311
 
222
- Print engine is a global method symbol or lambda expression to use in object printout. Examples of global methods are `:p`, `:ap`, and `:pp`. An example of a lambda expression is `lambda {|o| Rails.logger.info(o)}`
312
+ Print engine is similar to `printer`, except it is focused on the scope of formatting
313
+ the data object being printed (excluding metadata such as file name, line number,
314
+ and expression, which are handled by the `printer`).
315
+ As such, it is also a global method symbol or lambda expression.
316
+ Examples of global methods are `:p`, `:ap`, and `:pp`.
317
+ An example of a lambda expression is `lambda {|object| puts object.to_a.join(" | ")}`
223
318
 
224
- Defaults to [awesome_print](https://github.com/awesome-print/awesome_print). If it finds Rails loaded it defaults to `lambda {|o| Rails.logger.ap(o)}` instead
319
+ Defaults to [awesome_print](https://github.com/awesome-print/awesome_print).
225
320
 
226
321
  Example:
227
322
 
228
323
  ```ruby
229
324
  # File Name: /Users/User/example.rb
230
- require 'awesome_print'
231
- PutsDebuggerer.print_engine = :ap
325
+ PutsDebuggerer.print_engine = :p
232
326
  array = [1, [2, 3]]
233
327
  pd array
234
328
  ```
@@ -238,13 +332,7 @@ Prints out:
238
332
  ```bash
239
333
  [PD] /Users/User/example.rb:5
240
334
  > pd array
241
- => [
242
- [0] 1,
243
- [1] [
244
- [0] 2,
245
- [1] 3
246
- ]
247
- ]
335
+ => [1, [2, 3]]
248
336
  ```
249
337
 
250
338
  #### `PutsDebuggerer.announcer`
@@ -475,6 +563,7 @@ Prints out `puts __caller_source_line__`
475
563
 
476
564
  ## Release Notes
477
565
 
566
+ * v0.8.0: `printer` option support
478
567
  * v0.7.1: default print engine to :ap (AwesomePrint)
479
568
  * v0.7.0: `run_at` option, global and piecemeal.
480
569
  * v0.6.1: updated README and broke apart specs
@@ -486,6 +575,10 @@ Prints out `puts __caller_source_line__`
486
575
  * v0.2.0: App path exclusion support, Rails root support, improved format
487
576
  * v0.1.0: File/line/expression print out
488
577
 
578
+ ## TODO
579
+
580
+ * display run_at run number in printout
581
+
489
582
  ## Contributing
490
583
 
491
584
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
@@ -4,8 +4,10 @@ require 'awesome_print'
4
4
  module PutsDebuggerer
5
5
  HEADER_DEFAULT = '*'*80
6
6
  FOOTER_DEFAULT = '*'*80
7
+ PRINTER_DEFAULT = :puts
7
8
  PRINT_ENGINE_DEFAULT = :ap
8
- PRINT_ENGINE_MESSAGE_INVALID = 'print_engine must be a valid global method symbol (e.g. :p or :puts) or lambda/proc'
9
+ PRINTER_MESSAGE_INVALID = 'printer must be a valid global method symbol (e.g. :puts) or lambda/proc receiving a text arg'
10
+ 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'
9
11
  ANNOUNCER_DEFAULT = '[PD]'
10
12
  FORMATTER_DEFAULT = -> (data) {
11
13
  puts data[:header] if data[:header]
@@ -105,10 +107,49 @@ module PutsDebuggerer
105
107
  !!@footer
106
108
  end
107
109
 
108
- # Print engine to use in object printout (e.g. `p`, `ap`, `pp`).
109
- # It is represented by the print engine's global method name as a symbol
110
- # (e.g. `:ap` for awesome_print).
111
- # Defaults to awesome_print loaded.
110
+ # Printer is a global method symbol or lambda expression to use in printing to the user.
111
+ # Examples of global methods are `:puts` and `:print`.
112
+ # An example of a lambda expression is `lambda {|output| Rails.logger.ap(output)}`
113
+ #
114
+ # Defaults to `:puts`
115
+ # In Rails, it defaults to: `lambda {|output| Rails.logger.ap(output)}`
116
+ #
117
+ # Example:
118
+ #
119
+ # # File Name: /Users/User/example.rb
120
+ # PutsDebuggerer.printer = lambda {|output| Rails.logger.error(output)}
121
+ # str = "Hello"
122
+ # pd str
123
+ #
124
+ # Prints out in the Rails app log as error lines:
125
+ #
126
+ # [PD] /Users/User/example.rb:5
127
+ # > pd str
128
+ # => Hello
129
+ attr_reader :printer
130
+
131
+ def printer=(printer)
132
+ if printer.nil?
133
+ if Object.const_defined?(:Rails)
134
+ @printer = lambda {|output| Rails.logger.debug(output)}
135
+ else
136
+ @printer = PRINTER_DEFAULT
137
+ end
138
+ elsif printer.is_a?(Proc)
139
+ @printer = printer
140
+ else
141
+ @printer = method(printer).name rescue raise(PRINTER_MESSAGE_INVALID)
142
+ end
143
+ end
144
+
145
+ # Print engine is similar to `printer`, except it is focused on the scope of formatting
146
+ # the data object being printed (excluding metadata such as file name, line number,
147
+ # and expression, which are handled by the `printer`).
148
+ # As such, it is also a global method symbol or lambda expression.
149
+ # Examples of global methods are `:p`, `:ap`, and `:pp`.
150
+ # An example of a lambda expression is `lambda {|object| puts object.to_a.join(" | ")}`
151
+ #
152
+ # Defaults to [awesome_print](https://github.com/awesome-print/awesome_print).
112
153
  #
113
154
  # Example:
114
155
  #
@@ -128,11 +169,7 @@ module PutsDebuggerer
128
169
 
129
170
  def print_engine=(engine)
130
171
  if engine.nil?
131
- if Object.const_defined?(:Rails)
132
- @print_engine = lambda {|object| Rails.logger.ap(object)}
133
- else
134
- @print_engine = PRINT_ENGINE_DEFAULT
135
- end
172
+ @print_engine = PRINT_ENGINE_DEFAULT
136
173
  elsif engine.is_a?(Proc)
137
174
  @print_engine = engine
138
175
  else
@@ -248,6 +285,7 @@ module PutsDebuggerer
248
285
  {
249
286
  header: header,
250
287
  footer: footer,
288
+ printer: printer,
251
289
  print_engine: print_engine,
252
290
  app_path: app_path,
253
291
  announcer: announcer,
@@ -331,15 +369,15 @@ module PutsDebuggerer
331
369
  @run_at_global_number = value
332
370
  end
333
371
 
334
- def init_run_at_global_number(object, run_at)
372
+ def init_run_at_global_number
335
373
  @run_at_global_number = 1
336
374
  end
337
375
 
338
- def increment_run_at_global_number(object, run_at)
376
+ def increment_run_at_global_number
339
377
  @run_at_global_number += 1
340
378
  end
341
379
 
342
- def reset_run_at_global_number(object, run_at)
380
+ def reset_run_at_global_number
343
381
  @run_at_global_number = nil
344
382
  end
345
383
 
@@ -366,9 +404,11 @@ module PutsDebuggerer
366
404
  end
367
405
  end
368
406
 
369
- PutsDebuggerer.print_engine = PutsDebuggerer::PRINT_ENGINE_DEFAULT
370
- PutsDebuggerer.announcer = PutsDebuggerer::ANNOUNCER_DEFAULT
371
- PutsDebuggerer.formatter = PutsDebuggerer::FORMATTER_DEFAULT
407
+ # setting values to nil defaults them properly
408
+ PutsDebuggerer.printer = nil
409
+ PutsDebuggerer.print_engine = nil
410
+ PutsDebuggerer.announcer = nil
411
+ PutsDebuggerer.formatter = nil
372
412
  PutsDebuggerer.app_path = nil
373
413
  PutsDebuggerer.caller = nil
374
414
  PutsDebuggerer.run_at = nil
@@ -408,7 +448,15 @@ def pd(object, options=nil)
408
448
  if __run_pd__(object, run_at)
409
449
  __with_pd_options__(options) do |print_engine_options|
410
450
  formatter_pd_data = __build_pd_data__(object, print_engine_options) #depth adds build method
451
+ stdout = $stdout
452
+ $stdout = sio = StringIO.new
411
453
  PutsDebuggerer.formatter.call(formatter_pd_data)
454
+ $stdout = stdout
455
+ if PutsDebuggerer.printer.is_a?(Proc)
456
+ PutsDebuggerer.printer.call(sio.string)
457
+ else
458
+ send(PutsDebuggerer.send(:printer), sio.string)
459
+ end
412
460
  end
413
461
  end
414
462
 
@@ -420,20 +468,20 @@ def __run_pd__(object, run_at)
420
468
  if run_at.nil?
421
469
  run_pd = true
422
470
  else
423
- if PutsDebuggerer.run_at_global_number.nil?
424
- if PutsDebuggerer.run_at_number(object, run_at).nil?
425
- PutsDebuggerer.init_run_at_number(object, run_at)
426
- else
427
- PutsDebuggerer.increment_run_at_number(object, run_at)
428
- end
429
- run_number = PutsDebuggerer.run_at_number(object, run_at)
430
- else
471
+ if PutsDebuggerer.run_at?
431
472
  if PutsDebuggerer.run_at_global_number.nil?
432
473
  PutsDebuggerer.init_run_at_global_number
433
474
  else
434
475
  PutsDebuggerer.increment_run_at_global_number
435
476
  end
436
477
  run_number = PutsDebuggerer.run_at_global_number
478
+ else
479
+ if PutsDebuggerer.run_at_number(object, run_at).nil?
480
+ PutsDebuggerer.init_run_at_number(object, run_at)
481
+ else
482
+ PutsDebuggerer.increment_run_at_number(object, run_at)
483
+ end
484
+ run_number = PutsDebuggerer.run_at_number(object, run_at)
437
485
  end
438
486
  if run_at.is_a?(Integer)
439
487
  run_pd = true if run_at == run_number
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puts_debuggerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-25 00:00:00.000000000 Z
11
+ date: 2017-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print