puts_debuggerer 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +74 -3
  3. data/lib/puts_debuggerer.rb +147 -10
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23082ac8b052016747f52fc3ed37c17ee1f9c8d1
4
- data.tar.gz: 8dd8dd68fabe0aaef0c6ba78912688cfa2160897
3
+ metadata.gz: 6051847f09b1366d57fe622110440f277c85a872
4
+ data.tar.gz: d8ee66acfbb3daea757a5bb5599aed56f9f7d182
5
5
  SHA512:
6
- metadata.gz: 2b798fcdf87f121abda0664bbf190fb2de2ba258eb431dfd7a287061aaa03dc408e7101c5d96ceaa725a31294324eb68cb36fdeb44165b13abd434f296e1b12d
7
- data.tar.gz: c884b97e883fb7b5e85c4e0a094dc87a463df2a4a7efef72b34fd7110d4358bbb5ff5ed7402895c79e4a6f3f6bdf47f4a98c78364779723508d4a521f8ee35a3
6
+ metadata.gz: 7df9317401c68f2f90bc10f3bccc5410883e3c712339432299aacf6761f516576c62d58477bfa00cb8648a68c7d94a6b166f21d36e81a26fcc0f73fc1b9ad60f
7
+ data.tar.gz: 02f85ec1f441cfc02785b5701a4b0104203fd0e2b2bdc020301b54d66435d87d12ce634b235f1be8338289cf88d2f033c34f2b078a7020853a474ed47b4a2fec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # puts_debuggerer v0.6.0
1
+ # puts_debuggerer v0.7.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)
@@ -25,7 +25,7 @@ Love PD?! Why not promote with [merchandise](https://www.zazzle.com/i+heart+pd+g
25
25
  Add the following to bundler's `Gemfile`.
26
26
 
27
27
  ```ruby
28
- gem 'puts_debuggerer', '~> 0.6.0'
28
+ gem 'puts_debuggerer', '~> 0.7.0'
29
29
  ```
30
30
 
31
31
  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 +35,7 @@ This is the recommended way for [Rails](rubyonrails.org) apps. Optionally, you m
35
35
  Or manually install and require library.
36
36
 
37
37
  ```bash
38
- gem install puts_debuggerer -v0.6.0
38
+ gem install puts_debuggerer -v0.7.0
39
39
  ```
40
40
 
41
41
  ```ruby
@@ -348,6 +348,75 @@ Prints out:
348
348
  /Users/User/.rvm/rubies/ruby-2.4.0/lib/ruby/2.4.0/irb/context.rb:381:in \`evaluate\'
349
349
  ```
350
350
 
351
+ #### `PutsDebuggerer.run_at`
352
+ (default = nil)
353
+
354
+ Set condition for when to run as specified by an index, array, or range.
355
+ * Default value is `nil` meaning always
356
+ * Value as an Integer index (1-based) specifies at which run to print once
357
+ * Value as an Array of indices specifies at which runs to print multiple times
358
+ * Value as a range specifies at which runs to print multiple times,
359
+ indefinitely if it ends with ..-1 or ...-1
360
+
361
+ Can be set globally via `PutsDebuggerer.run_at` or piecemeal via `pd object, run_at: run_at_value`
362
+
363
+ Global usage should be good enough for most cases. When there is a need to track
364
+ a single expression among several, you may add the option piecemeal, but it expects
365
+ the same exact `object` passed to `pd` for counting.
366
+
367
+ Examples (global):
368
+
369
+ PutsDebuggerer.run_at = 1
370
+ pd (x=1) # prints standard PD output
371
+ pd (x=1) # prints nothing
372
+
373
+ PutsDebuggerer.run_at = 2
374
+ pd (x=1) # prints nothing
375
+ pd (x=1) # prints standard PD output
376
+
377
+ PutsDebuggerer.run_at = [1, 3]
378
+ pd (x=1) # prints standard PD output
379
+ pd (x=1) # prints nothing
380
+ pd (x=1) # prints standard PD output
381
+ pd (x=1) # prints nothing
382
+
383
+ PutsDebuggerer.run_at = 3..5
384
+ pd (x=1) # prints nothing
385
+ pd (x=1) # prints nothing
386
+ pd (x=1) # prints standard PD output
387
+ pd (x=1) # prints standard PD output
388
+ pd (x=1) # prints standard PD output
389
+ pd (x=1) # prints nothing
390
+ pd (x=1) # prints nothing
391
+
392
+ PutsDebuggerer.run_at = 3...6
393
+ pd (x=1) # prints nothing
394
+ pd (x=1) # prints nothing
395
+ pd (x=1) # prints standard PD output
396
+ pd (x=1) # prints standard PD output
397
+ pd (x=1) # prints standard PD output
398
+ pd (x=1) # prints nothing
399
+
400
+ PutsDebuggerer.run_at = 3..-1
401
+ pd (x=1) # prints nothing
402
+ pd (x=1) # prints nothing
403
+ pd (x=1) # prints standard PD output
404
+ pd (x=1) ... continue printing indefinitely on all subsequent runs
405
+
406
+ PutsDebuggerer.run_at = 3...-1
407
+ pd (x=1) # prints nothing
408
+ pd (x=1) # prints nothing
409
+ pd (x=1) # prints standard PD output
410
+ pd (x=1) ... continue printing indefinitely on all subsequent runs
411
+
412
+ You may reset the run_at number counter via:
413
+ `PutsDebuggerer.reset_run_at_global_number` for global usage.
414
+
415
+ And:
416
+ `PutsDebuggerer.reset_run_at_number` or
417
+ `PutsDebuggerer.reset_run_at_numbers`
418
+ for piecemeal usage.
419
+
351
420
  ### Bonus
352
421
 
353
422
  puts_debuggerer comes with a number of bonus goodies.
@@ -405,6 +474,8 @@ Prints out `puts __caller_source_line__`
405
474
 
406
475
  ## Release Notes
407
476
 
477
+ * v0.7.0: `run_at` option, global and piecemeal.
478
+ * v0.6.1: updated README and broke apart specs
408
479
  * v0.6.0: unofficial erb support, returning evaluated object/expression, removed static syntax support (replaced with header support)
409
480
  * v0.5.1: support for print engine lambdas and smart defaults for leveraging Rails and AwesomePrint debuggers in Rails
410
481
  * v0.5.0: custom formatter, caller backtrace, per-puts piecemeal options, and multi-line support
@@ -14,6 +14,9 @@ module PutsDebuggerer
14
14
  puts data[:footer] if data[:footer]
15
15
  }
16
16
  CALLER_DEPTH_ZERO = 4 #depth includes pd + with_options method + nested block + build_pd_data method
17
+ OBJECT_RUN_AT = {}
18
+ STACK_TRACE_CALL_LINE_NUMBER_REGEX = /\:(\d+)\:in /
19
+ STACK_TRACE_CALL_SOURCE_FILE_REGEX = /[ ]*([^:]+)\:\d+\:in /
17
20
 
18
21
  class << self
19
22
  # Application root path to exclude when printing out file path
@@ -258,7 +261,8 @@ module PutsDebuggerer
258
261
  app_path: app_path,
259
262
  announcer: announcer,
260
263
  formatter: formatter,
261
- caller: caller
264
+ caller: caller,
265
+ run_at: run_at
262
266
  }
263
267
  end
264
268
 
@@ -268,14 +272,115 @@ module PutsDebuggerer
268
272
  send("#{option}=", value)
269
273
  end
270
274
  end
275
+
276
+ # When to run as specified by an index, array, or range.
277
+ # * Default value is `nil` meaning always
278
+ # * Value as an Integer index (1-based) specifies at which run to print once
279
+ # * Value as an Array of indices specifies at which runs to print multiple times
280
+ # * Value as a range specifies at which runs to print multiple times,
281
+ # indefinitely if it ends with ..-1
282
+ #
283
+ # Example:
284
+ #
285
+ # PutsDebuggerer.run_at = 1
286
+ # pd (x=1) # prints standard PD output
287
+ # pd (x=1) # prints nothing
288
+ #
289
+ # PutsDebuggerer.run_at = 2
290
+ # pd (x=1) # prints nothing
291
+ # pd (x=1) # prints standard PD output
292
+ #
293
+ # PutsDebuggerer.run_at = [1, 3]
294
+ # pd (x=1) # prints standard PD output
295
+ # pd (x=1) # prints nothing
296
+ # pd (x=1) # prints standard PD output
297
+ # pd (x=1) # prints nothing
298
+ #
299
+ # PutsDebuggerer.run_at = 3..5
300
+ # pd (x=1) # prints nothing
301
+ # pd (x=1) # prints nothing
302
+ # pd (x=1) # prints standard PD output
303
+ # pd (x=1) # prints standard PD output
304
+ # pd (x=1) # prints standard PD output
305
+ # pd (x=1) # prints nothing
306
+ # pd (x=1) # prints nothing
307
+ #
308
+ # PutsDebuggerer.run_at = 3...6
309
+ # pd (x=1) # prints nothing
310
+ # pd (x=1) # prints nothing
311
+ # pd (x=1) # prints standard PD output
312
+ # pd (x=1) # prints standard PD output
313
+ # pd (x=1) # prints standard PD output
314
+ # pd (x=1) # prints nothing
315
+ #
316
+ # PutsDebuggerer.run_at = 3..-1
317
+ # pd (x=1) # prints nothing
318
+ # pd (x=1) # prints nothing
319
+ # pd (x=1) # prints standard PD output
320
+ # pd (x=1) ... continue printing indefinitely on all subsequent runs
321
+ #
322
+ # PutsDebuggerer.run_at = 3...-1
323
+ # pd (x=1) # prints nothing
324
+ # pd (x=1) # prints nothing
325
+ # pd (x=1) # prints standard PD output
326
+ # pd (x=1) ... continue printing indefinitely on all subsequent runs
327
+ attr_reader :run_at
328
+
329
+ def run_at=(value)
330
+ @run_at = value
331
+ end
332
+
333
+ def run_at?
334
+ !!@run_at
335
+ end
336
+
337
+ attr_reader :run_at_global_number
338
+
339
+ def run_at_global_number=(value)
340
+ @run_at_global_number = value
341
+ end
342
+
343
+ def init_run_at_global_number(object, run_at)
344
+ @run_at_global_number = 1
345
+ end
346
+
347
+ def increment_run_at_global_number(object, run_at)
348
+ @run_at_global_number += 1
349
+ end
350
+
351
+ def reset_run_at_global_number(object, run_at)
352
+ @run_at_global_number = nil
353
+ end
354
+
355
+ def run_at_number(object, run_at)
356
+ PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]]
357
+ end
358
+
359
+ def init_run_at_number(object, run_at)
360
+ PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]] = 1
361
+ end
362
+
363
+ def increment_run_at_number(object, run_at)
364
+ PutsDebuggerer::OBJECT_RUN_AT[[object,run_at]] += 1
365
+ end
366
+
367
+ def reset_run_at_number(object, run_at)
368
+ PutsDebuggerer::OBJECT_RUN_AT.delete([object, run_at])
369
+ end
370
+
371
+ def reset_run_at_numbers
372
+ PutsDebuggerer::OBJECT_RUN_AT.clear
373
+ end
374
+
271
375
  end
272
376
  end
273
377
 
274
378
  PutsDebuggerer.print_engine = PutsDebuggerer::PRINT_ENGINE_DEFAULT
275
379
  PutsDebuggerer.announcer = PutsDebuggerer::ANNOUNCER_DEFAULT
276
380
  PutsDebuggerer.formatter = PutsDebuggerer::FORMATTER_DEFAULT
277
- PutsDebuggerer.app_path = nil #TODO also have it set Rails root on first PD
381
+ PutsDebuggerer.app_path = nil
278
382
  PutsDebuggerer.caller = nil
383
+ PutsDebuggerer.run_at = nil
279
384
 
280
385
  # Prints object with bonus info such as file name, line number and source
281
386
  # expression. Optionally prints out header and footer.
@@ -307,16 +412,48 @@ PutsDebuggerer.caller = nil
307
412
  # => "Show me the source of the bug: beattle"
308
413
  # [PD] /Users/User/finance_calculator_app/pd_test.rb:4 "What line number am I?"
309
414
  def pd(object, options=nil)
310
- __with_pd_options__(options) do |print_engine_options|
311
- formatter_pd_data = __build_pd_data__(object, print_engine_options) #depth adds build method
312
- PutsDebuggerer.formatter.call(formatter_pd_data)
415
+ run_at = ((options && options[:run_at]) || PutsDebuggerer.run_at)
416
+
417
+ if __run_pd__(object, run_at)
418
+ __with_pd_options__(options) do |print_engine_options|
419
+ formatter_pd_data = __build_pd_data__(object, print_engine_options) #depth adds build method
420
+ PutsDebuggerer.formatter.call(formatter_pd_data)
421
+ end
313
422
  end
423
+
314
424
  object
315
425
  end
316
426
 
317
-
318
- STACK_TRACE_CALL_LINE_NUMBER_REGEX = /\:(\d+)\:in /
319
- STACK_TRACE_CALL_SOURCE_FILE_REGEX = /[ ]*([^:]+)\:\d+\:in /
427
+ def __run_pd__(object, run_at)
428
+ run_pd = false
429
+ if run_at.nil?
430
+ run_pd = true
431
+ else
432
+ if PutsDebuggerer.run_at_global_number.nil?
433
+ if PutsDebuggerer.run_at_number(object, run_at).nil?
434
+ PutsDebuggerer.init_run_at_number(object, run_at)
435
+ else
436
+ PutsDebuggerer.increment_run_at_number(object, run_at)
437
+ end
438
+ run_number = PutsDebuggerer.run_at_number(object, run_at)
439
+ else
440
+ if PutsDebuggerer.run_at_global_number.nil?
441
+ PutsDebuggerer.init_run_at_global_number
442
+ else
443
+ PutsDebuggerer.increment_run_at_global_number
444
+ end
445
+ run_number = PutsDebuggerer.run_at_global_number
446
+ end
447
+ if run_at.is_a?(Integer)
448
+ run_pd = true if run_at == run_number
449
+ elsif run_at.is_a?(Array)
450
+ run_pd = true if run_at.include?(run_number)
451
+ elsif run_at.is_a?(Range)
452
+ run_pd = true if run_at.cover?(run_number) || (run_at.end == -1 && run_number >= run_at.begin)
453
+ end
454
+ end
455
+ run_pd
456
+ end
320
457
 
321
458
  # Provides caller line number starting 1 level above caller of
322
459
  # this method.
@@ -329,7 +466,7 @@ STACK_TRACE_CALL_SOURCE_FILE_REGEX = /[ ]*([^:]+)\:\d+\:in /
329
466
  #
330
467
  # prints out `3`
331
468
  def __caller_line_number__(caller_depth=0)
332
- caller[caller_depth][STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
469
+ caller[caller_depth][PutsDebuggerer::STACK_TRACE_CALL_LINE_NUMBER_REGEX, 1].to_i
333
470
  end
334
471
 
335
472
  # Provides caller file starting 1 level above caller of
@@ -342,7 +479,7 @@ end
342
479
  #
343
480
  # prints out `lib/example.rb`
344
481
  def __caller_file__(caller_depth=0)
345
- caller[caller_depth][STACK_TRACE_CALL_SOURCE_FILE_REGEX, 1]
482
+ caller[caller_depth][PutsDebuggerer::STACK_TRACE_CALL_SOURCE_FILE_REGEX, 1]
346
483
  end
347
484
 
348
485
 
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.6.1
4
+ version: 0.7.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-20 00:00:00.000000000 Z
11
+ date: 2017-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print