dbg-rb 0.2.1 → 0.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bdcb38f9dc3080710eef9e91b78585a4e718e4ff421792aeb0114ca8a2dec5ec
4
- data.tar.gz: ccdee5e7ddfd22cb0008160e61a732dc7f71fb72f30369132a909be2baeec75b
3
+ metadata.gz: 32d1a708880ea34d5100e660696d864bb1aa5176612b9684152921348ffd0269
4
+ data.tar.gz: 1b0647a7409f8c4ac564b0ae69ac33ea0a591ed2de8dff2e5ca3967dc3037c6f
5
5
  SHA512:
6
- metadata.gz: 7b5d2c2e616746d05b0d45245faf85c3ad284b5089aae9858109f93c853575e8f6879e387bae97915e93671c615d559cab0d1eee298e8ceac30c66f587fdb630
7
- data.tar.gz: eb7161ea23dc92b939956a2279e3576f29fc7bbc90c6f8cbada1671a5c7c3d17481d169f651b9f3ce50ef096fd544b2c3e89117495fd5cc2253a6e1feb214e94
6
+ metadata.gz: 86c169bffb8ddafe0f382803b841ac0e5cbf02e86deab1b28c43b0d734e2aaac2d1df32619e5c5b8a4c384276612e059c45e60600bb18dc861354f675f4ba20f
7
+ data.tar.gz: 609749a0c676334dfe8d9bea10f4d57080dc9b38d442211134c20df37831ad13e03588b044ccdc077a3d4db70c36b95a4e66dfb9da8b729e17463c9787cf89f3
data/README.md CHANGED
@@ -23,12 +23,12 @@ gem "dbg-rb"
23
23
 
24
24
  ## Usage
25
25
 
26
- Gem adds a global `dbg!` method that you can use for puts debugging:
26
+ Gem adds global `dbg!` and `dbg` methods that you can use for puts debugging:
27
27
 
28
28
  ```ruby
29
29
  require "dbg-rb"
30
30
 
31
- dbg!(User.last.id)
31
+ dbg(User.last.id)
32
32
  # [web/user_sessions_controller.rb:37] 1972
33
33
 
34
34
  ```
data/lib/dbg-rb.rb CHANGED
@@ -3,82 +3,96 @@
3
3
  require "binding_of_caller"
4
4
 
5
5
  module DbgRb
6
- @@color_code = nil
7
- @@highlight = false
8
-
9
6
  def self.color_code=(val)
10
- @@color_code = val
7
+ Impl.color_code = val
11
8
  end
12
9
 
13
10
  def self.highlight!(wrapper = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
14
- @@highlight = wrapper
11
+ Impl.highlight!(wrapper)
15
12
  end
16
13
 
17
- def self.colorize(str, color_code)
18
- "\e[#{color_code}m#{str}\e[0m"
14
+ def self.dbg!(*msgs)
15
+ Impl.new.dbg!(*msgs)
19
16
  end
20
17
 
21
- def self.dbg!(*msgs)
22
- loc = caller_locations.first(2).last.to_s
23
- matching_loc = loc.match(/.+(rb)\:\d+\:(in)\s/)
24
- src = if !matching_loc.nil?
25
- matching_loc[0][0..-5]
26
- else
27
- loc
28
- end
29
- file, line = src.split(":")
30
- file = file.split("/").last(2).join("/")
31
- src = "[#{file}:#{line}]"
18
+ class Impl
19
+ @@color_code = nil
20
+ @@highlight = false
32
21
 
33
- msgs.each_with_index do |obj, i|
34
- first = i == 0
35
- last = i == (msgs.size - 1)
22
+ def self.color_code=(val)
23
+ @@color_code = val
24
+ end
36
25
 
37
- val = if obj.is_a?(Symbol)
38
- begin
39
- if (val = binding.of_caller(3).local_variable_get(obj))
40
- val = format_val(val)
26
+ def self.highlight!(wrapper)
27
+ @@highlight = wrapper
28
+ end
41
29
 
42
- "#{obj} = #{val}"
43
- end
44
- rescue NameError
45
- ":#{obj}"
46
- end
30
+ def dbg!(*msgs)
31
+ loc = caller_locations.first(3).last.to_s
32
+ matching_loc = loc.match(/.+(rb)\:\d+\:(in)\s/)
33
+ src = if !matching_loc.nil?
34
+ matching_loc[0][0..-5]
47
35
  else
48
- format_val(obj)
36
+ loc
49
37
  end
38
+ file, line = src.split(":")
39
+ file = file.split("/").last(2).join("/")
40
+ src = "[#{file}:#{line}]"
41
+
42
+ msgs.each_with_index do |obj, i|
43
+ first = i == 0
44
+ last = i == (msgs.size - 1)
45
+
46
+ val = if obj.is_a?(Symbol)
47
+ begin
48
+ if (val = binding.of_caller(4).local_variable_get(obj))
49
+ val = format_val(val)
50
+
51
+ "#{obj} = #{val}"
52
+ end
53
+ rescue NameError
54
+ ":#{obj}"
55
+ end
56
+ else
57
+ format_val(obj)
58
+ end
59
+
60
+ output = "#{src} #{val}"
50
61
 
51
- output = "#{src} #{val}"
62
+ if @@highlight
63
+ if first
64
+ output = "#{@@highlight}\n#{output}"
65
+ end
52
66
 
53
- if @@highlight
54
- if first
55
- output = "#{@@highlight}\n#{output}"
67
+ if last
68
+ output = "#{output}\n#{@@highlight}"
69
+ end
56
70
  end
57
71
 
58
- if last
59
- output = "#{output}\n#{@@highlight}"
72
+ if @@color_code != nil
73
+ output = colorize(output, @@color_code)
60
74
  end
61
- end
62
75
 
63
- if @@color_code != nil
64
- output = colorize(output, @@color_code)
76
+ puts output
65
77
  end
66
78
 
67
- puts output
79
+ nil
68
80
  end
69
81
 
70
- nil
71
- end
82
+ def colorize(str, color_code)
83
+ "\e[#{color_code}m#{str}\e[0m"
84
+ end
72
85
 
73
- def self.format_val(val)
74
- if val.nil?
75
- "nil"
76
- elsif val.is_a?(String)
77
- "\"#{val}\""
78
- elsif val.is_a?(Hash) || val.is_a?(Array)
79
- JSON.pretty_generate(val)
80
- else
81
- val
86
+ def format_val(val)
87
+ if val.nil?
88
+ "nil"
89
+ elsif val.is_a?(String)
90
+ "\"#{val}\""
91
+ elsif val.is_a?(Hash) || val.is_a?(Array)
92
+ JSON.pretty_generate(val)
93
+ else
94
+ val
95
+ end
82
96
  end
83
97
  end
84
98
  end
@@ -86,3 +100,5 @@ end
86
100
  def dbg!(*msgs)
87
101
  DbgRb.dbg!(*msgs)
88
102
  end
103
+
104
+ alias dgb dbg!
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DbgRb
4
- VERSION = "0.2.1"
4
+ VERSION = "0.2.2"
5
5
  end
data/spec/smoke_spec.rb CHANGED
@@ -47,4 +47,13 @@ describe DbgRb do
47
47
  DbgRb.color_code = 31
48
48
  expect { dbg!(123) }.to output("\e[31m!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n[spec/smoke_spec.rb:48] 123\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m\n").to_stdout
49
49
  end
50
+
51
+ it "alias" do
52
+ DbgRb.highlight!(false)
53
+ DbgRb.color_code = nil
54
+
55
+ expect {
56
+ dgb(123)
57
+ }.to output("[spec/smoke_spec.rb:56] 123\n").to_stdout
58
+ end
50
59
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbg-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-12-10 00:00:00.000000000 Z
11
+ date: 2024-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller