dbg-rb 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '09b1254dce075461fa3f871339727687bb71c3ed3c665a402f4eb391186f3d73'
4
+ data.tar.gz: 534dc75a914ad52ee594a298f6be6ec7e8b44cda80ac5749dab66a28956732a9
5
+ SHA512:
6
+ metadata.gz: 52ec38c67acc0cd2c8876d9168262d5d5293fb881cc2192a70cd39578ee82d4814e60bdacf48b8f5967b3e13c839c1e4bcf54bfcdaae9c4103d1562d9d38dd96
7
+ data.tar.gz: 2850472008c78ac1d28e1a38360c29b61bc2234c0f7f925f11a18dcc7a931b504b51595b522c36e11bce595960f28f5da59062faabd445165ca20a23ec27794b
@@ -0,0 +1,30 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7', '2.6']
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+ - name: Set up Ruby ${{ matrix.ruby-version }}
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby-version }}
22
+ - name: Setup dependencies
23
+ run: |
24
+ gem install bundler -v 2.4.22
25
+ sudo apt-get update --allow-releaseinfo-change
26
+ bundle config set --local path 'vendor/bundle'
27
+ bundle install
28
+ - name: Run tests
29
+ run: |
30
+ bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ Gemfile.lock
2
+ .bundle/
3
+ .ruby-version
4
+ pkg/
5
+ *.gem
6
+ docker-compose.yml
7
+ .byebug_history
8
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright © Paweł Urbanek 2024
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # dbg! [![Gem Version](https://badge.fury.io/rb/dbg-rb.svg)](https://badge.fury.io/rb/dbg-rb) [![GH Actions](https://github.com/pawurb/dbg-rb/actions/workflows/ci.yml/badge.svg)](https://github.com/pawurb/dbg-rb/actions)
2
+
3
+ ![Dbg base](https://github.com/pawurb/dbg-rb/raw/main/dbg_base3.png)
4
+
5
+ Because I wrote:
6
+
7
+ ```ruby
8
+ p '!!!!!!!!!!!!!!!'
9
+ p msg
10
+ p '!!!!!!!!!!!!!!!'
11
+ ```
12
+
13
+ too many times already.
14
+
15
+ `dbg!` is a minimal, [Rust inspired](https://doc.rust-lang.org/std/macro.dbg.html), *puts debugging* command for Ruby. It provides caller context and formatting helpful in everyday debugging tasks.
16
+
17
+ ## Installation
18
+
19
+ `Gemfile`
20
+ ```ruby
21
+ gem "dbg-rb"
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ Gem adds a global `dbg!` method that you can use for puts debugging:
27
+
28
+ ```ruby
29
+ require "dbg-rb"
30
+
31
+ dbg!(User.last.id)
32
+ # [web/user_sessions_controller.rb:37] 1972
33
+
34
+ ```
35
+
36
+ It appends a caller file and line info to the debug output.
37
+
38
+ You can use symbols to output local variable names together with their values:
39
+
40
+ ```ruby
41
+ a = 1
42
+ b = 2
43
+
44
+ dbg!(:a, :b)
45
+ # [models/user.rb:22] a = 1
46
+ # [models/user.rb:22] b = 2
47
+ ```
48
+
49
+ Hash values are pretty printed:
50
+
51
+ ```ruby
52
+ user = User.last.as_json
53
+ dbg!(:user)
54
+ # [web/users_controller.rb:10 user = {
55
+ # "id": 160111,
56
+ # "team_id": 1,
57
+ # "pseudonym": "Anonymous-CBWE",
58
+ # ...
59
+ # }
60
+ ```
61
+
62
+ You can color the output:
63
+
64
+ `config/initializers/dbg_rb.rb`
65
+ ```ruby
66
+ require "dbg-rb"
67
+
68
+ DbgRb.color_code = 35
69
+ # 31 red
70
+ # 32 green
71
+ # 33 yellow
72
+ # 34 blue
73
+ # 35 pink
74
+ # 36 light blue
75
+ ```
76
+
77
+ ```ruby
78
+ user = User.last.as_json.slice("id", "slack_id")
79
+ dbg!("User last", :user)
80
+ ```
81
+
82
+ ![Dbg color](https://github.com/pawurb/dbg-rb/raw/main/dbg_base3.png)
83
+
84
+ If it does not stand out enough, you can enable `dbg!` highlighting:
85
+
86
+ `config/initializers/dbg_rb.rb`
87
+ ```ruby
88
+ require "dbg-rb"
89
+
90
+ DbgRb.highlight!("🎉💔💣🕺🚀🧨🙈🤯🥳🌈🦄")
91
+ ```
92
+
93
+ ![Dbg emoji](https://github.com/pawurb/dbg-rb/raw/main/dbg_emoji2.png)
94
+
95
+ You can also use `DbgRb.dbg!(*msgs)` directly or wrap it to rename the helper method:
96
+
97
+ ```ruby
98
+ def dd(*msgs)
99
+ DbgRb.dbg!(*msgs)
100
+ end
101
+ ```
102
+
103
+ ## Status
104
+
105
+ Contributions & ideas very welcome!
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ desc "Run tests"
9
+ task :default => :test
data/dbg_base3.png ADDED
Binary file
data/dbg_color.png ADDED
Binary file
data/dbg_emoji2.png ADDED
Binary file
data/lib/dbg-rb.rb ADDED
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "binding_of_caller"
4
+
5
+ module DbgRb
6
+ @@color_code = nil
7
+ @@highlight = false
8
+
9
+ def self.color_code=(val)
10
+ @@color_code = val
11
+ end
12
+
13
+ def self.highlight!(wrapper = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
14
+ @@highlight = wrapper
15
+ end
16
+
17
+ def self.colorize(str, color_code)
18
+ "\e[#{color_code}m#{str}\e[0m"
19
+ end
20
+
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}]"
32
+
33
+ msgs.each_with_index do |obj, i|
34
+ first = i == 0
35
+ last = i == (msgs.size - 1)
36
+
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)
41
+
42
+ "#{obj} = #{val}"
43
+ end
44
+ rescue NameError
45
+ ":#{obj}"
46
+ end
47
+ else
48
+ format_val(obj)
49
+ end
50
+
51
+ output = "#{src} #{val}"
52
+
53
+ if @@highlight
54
+ if first
55
+ output = "#{@@highlight}\n#{output}"
56
+ end
57
+
58
+ if last
59
+ output = "#{output}\n#{@@highlight}"
60
+ end
61
+ end
62
+
63
+ if @@color_code != nil
64
+ output = colorize(output, @@color_code)
65
+ end
66
+
67
+ puts output
68
+ end
69
+
70
+ nil
71
+ end
72
+
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)
79
+ JSON.pretty_generate(val)
80
+ else
81
+ val
82
+ end
83
+ end
84
+ end
85
+
86
+ def dbg!(*msgs)
87
+ DbgRb.dbg!(*msgs)
88
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DbgRb
4
+ VERSION = "0.2.0"
5
+ end
data/ruby-dbg.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "dbg_rb/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "dbg-rb"
8
+ s.version = DbgRb::VERSION
9
+ s.authors = ["pawurb"]
10
+ s.email = ["contact@pawelurbanek.com"]
11
+ s.summary = "Simple debuging helper"
12
+ s.description = "Rust-inspired, puts debugging helper, adding caller info and optional coloring."
13
+ s.homepage = "http://github.com/pawurb/dbg-rb"
14
+ s.files = `git ls-files`.split("\n").reject { |f| f.match?(/\.db$/) }
15
+ s.require_paths = ["lib"]
16
+ s.license = "MIT"
17
+ s.add_dependency "binding_of_caller"
18
+ s.add_development_dependency "rake"
19
+ s.add_development_dependency "rspec"
20
+ s.add_development_dependency "rufo"
21
+
22
+ if s.respond_to?(:metadata=)
23
+ s.metadata = { "rubygems_mfa_required" => "true" }
24
+ end
25
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "dbg-rb"
5
+ require "ostruct"
6
+
7
+ describe DbgRb do
8
+ it "variable values" do
9
+ expect { dbg!("123") }.to output("[spec/smoke_spec.rb:9] \"123\"\n").to_stdout
10
+ end
11
+
12
+ it "binded variables" do
13
+ b = 123
14
+ expect { dbg!(:b) }.to output("[spec/smoke_spec.rb:14] b = 123\n").to_stdout
15
+ end
16
+
17
+ it "missing binded variables" do
18
+ b = 123
19
+ expect { dbg!(:c) }.to output("[spec/smoke_spec.rb:19] :c\n").to_stdout
20
+ end
21
+
22
+ it "complex objects" do
23
+ s = OpenStruct.new(a: 1, b: 2)
24
+ expect { dbg!(s) }.to output("[spec/smoke_spec.rb:24] #<OpenStruct a=1, b=2>\n").to_stdout
25
+ end
26
+
27
+ it "binded complex objects" do
28
+ s = OpenStruct.new(a: 1, b: 2)
29
+ expect { dbg!(:s) }.to output("[spec/smoke_spec.rb:29] s = #<OpenStruct a=1, b=2>\n").to_stdout
30
+ end
31
+
32
+ it "multiple msg" do
33
+ s = OpenStruct.new(a: 1, b: 2)
34
+ expect { dbg!(:s, "other msg") }.to output("[spec/smoke_spec.rb:34] s = #<OpenStruct a=1, b=2>\n[spec/smoke_spec.rb:34] \"other msg\"\n").to_stdout
35
+ end
36
+
37
+ it "nil" do
38
+ expect { dbg!(nil) }.to output("[spec/smoke_spec.rb:38] nil\n").to_stdout
39
+ end
40
+
41
+ it "higlight" do
42
+ DbgRb.highlight!
43
+ expect { dbg!("123") }.to output("!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n[spec/smoke_spec.rb:43] \"123\"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n").to_stdout
44
+ end
45
+
46
+ it "color_code" do
47
+ DbgRb.color_code = 31
48
+ expect { dbg!(123) }.to output("\e[31m!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n[spec/smoke_spec.rb:48] 123\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!\e[0m\n").to_stdout
49
+ end
50
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+ require_relative "../lib/dbg-rb"
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dbg-rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - pawurb
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-12-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: binding_of_caller
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rufo
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rust-inspired, puts debugging helper, adding caller info and optional
70
+ coloring.
71
+ email:
72
+ - contact@pawelurbanek.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".github/workflows/ci.yml"
78
+ - ".gitignore"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - dbg_base3.png
84
+ - dbg_color.png
85
+ - dbg_emoji2.png
86
+ - lib/dbg-rb.rb
87
+ - lib/dbg_rb/version.rb
88
+ - ruby-dbg.gemspec
89
+ - spec/smoke_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: http://github.com/pawurb/dbg-rb
92
+ licenses:
93
+ - MIT
94
+ metadata:
95
+ rubygems_mfa_required: 'true'
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubygems_version: 3.5.23
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Simple debuging helper
115
+ test_files: []