Skip to content

Commit

Permalink
Fiat lux
Browse files Browse the repository at this point in the history
  • Loading branch information
mblumtritt committed May 12, 2024
0 parents commit 1567237
Show file tree
Hide file tree
Showing 17 changed files with 1,049 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tmp/
pkg/
local/
doc/
.yardoc/
.vscode/
*.lock
.rubocop.*
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require helper
10 changes: 10 additions & 0 deletions .yardopts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
--readme README.md
--title 'im-lost'
--charset utf-8
--markup markdown
--tag comment
--hide-tag comment
lib/**/*.rb
-
README.md
LICENSE
19 changes: 19 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

source 'https://rubygems.org'

group :development, :test do
gem 'bundler', require: false
gem 'rake', require: false
end

group :test do
gem 'rspec', require: false
end

group :development do
gem 'webrick', require: false
gem 'yard', require: false
end

gemspec
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Mike Blumtritt

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
144 changes: 144 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# ImLost ![version](https://img.shields.io/gem/v/im-lost?label=)

If you have overlooked something again and don't really understand what your code is doing. If you have to maintain this application but can't really find your way around and certainly can't track down that stupid error. If you feel lost in all that code, here's the gem to help you out!

ImLost helps you by analyzing function calls of objects, informing you about exceptions and logging your way through your code. In short, ImLost is your debugging helper!

- Gem: [rubygems.org](https://rubygems.org/gems/im-lost)
- Source: [github.com](https://github.com/mblumtritt/im-lost)
- Help: [rubydoc.info](https://rubydoc.info/gems/im-lost/ImLost)

## Description

If you like to undertsand method call details you get a call trace with `ImLost.trace`:

```ruby
File.open('test.txt', 'w') do |file|
ImLost.trace(file) do
file << 'hello '
file.puts(:world!)
end
end
# output will look like
# > IO#<<(?)
# /projects/test.rb:1
# > IO#write(*)
# /projects/test.rb:1
# > IO#puts(*)
# /projects/test.rb:2
# > IO#write(*)
# /projects/test.rb:2
```

When you need to know if exceptions are raised and handled you can use `ImLost.trace_exceptions`:

```ruby
ImLost.trace_exceptions do
File.write('/', 'test')
rescue SystemCallError
raise('something went wrong!')
end
# output will look like
# x Errno::EEXIST: File exists @ rb_sysopen - /
# /projects/test.rb:2
# ! Errno::EEXIST: File exists @ rb_sysopen - /
# /projects/test.rb:3
# x RuntimeError: something went wrong!
# /projects/test.rb:4
```

When you like to know if and when a code point is reached, `ImLost.here` will help:

```ruby
ImLost.here
```

## Example

```ruby
require 'im-lost'

class Foo
def self.create(value:) = new(value)

attr_reader :value

def initialize(value)
@value = value
end

def foo(arg, *args, key: nil, **kw_args, &block)
@value = "#{arg}-#{key}-[#{args.join(',')}]-#{kw_args.inspect}-#{bar}"
block ? block.call(@value) : @value
end

def bar = :bar
end

ImLost.trace_results = true
ImLost.trace(Foo)

my_foo = Foo.create(value: :foo!)
ImLost.trace(my_foo)

my_foo.foo(1, key: :none)
my_foo.foo(2, :a, :b, :c, key: :some, name: :value)
my_foo.foo(3) { puts _1 }

# output will look like
# > Foo.create(:foo!)
# /projects/foo.rb:25
# > Foo.new(*)
# /projects/foo.rb:6
# < Foo.new(*)
# = #<Foo:0x0000000100902418 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x0000000100902418 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb:28
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# /projects/foo.rb:29
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# = "2-some-[a,b,c]-{:name=>:value}-bar"
# > Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
# /projects/foo.rb:30
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# 3--[]-{}-bar
# < Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
# = nil
```

See [examples dir](./examples) for more…

## Installation

You can install the gem in your system with

```shell
gem install im-lost
```

or you can use [Bundler](http://gembundler.com/) to add ImLost to your own project:

```shell
bundle add im-lost
```

After that you only need one line of code to have everything together

```ruby
require 'im-lost'
```
20 changes: 20 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

$stdout.sync = $stderr.sync = true

require 'bundler/gem_tasks'

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:test) { _1.ruby_opts = %w[-w] }

require 'yard'

CLEAN << '.yardoc'
CLOBBER << 'doc'

YARD::Rake::YardocTask.new(:doc) { _1.stats_options = %w[--list-undoc] }

desc 'Run YARD development server'
task('doc:dev' => :clobber) { exec('yard server --reload') }

task(:default) { exec('rake --tasks') }
65 changes: 65 additions & 0 deletions examples/foo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require_relative '../lib/im-lost'

class Foo
def self.create(value:) = new(value)

attr_reader :value

def initialize(value)
@value = value
end

def foo(arg, *args, key: nil, **kw_args, &block)
@value = "#{arg}-#{key}-[#{args.join(',')}]-#{kw_args.inspect}-#{bar}"
block ? block.call(@value) : @value
end

def bar = :bar
end

ImLost.trace_results = true
ImLost.trace(Foo)

my_foo = Foo.create(value: :foo!)
ImLost.trace(my_foo)

my_foo.foo(1, key: :none)
my_foo.foo(2, :a, :b, :c, key: :some, name: :value)
my_foo.foo(3) { puts _1 }

# output will look like
# > Foo.create(:foo!)
# /projects/foo.rb:25
# > Foo.new(*)
# /projects/foo.rb:6
# < Foo.new(*)
# = #<Foo:0x0000000100902418 @value=:foo!>
# < Foo.create(:foo!)
# = #<Foo:0x0000000100902418 @value=:foo!>
# > Foo#foo(1, *[], :none, **{}, &nil)
# /projects/foo.rb:28
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(1, *[], :none, **{}, &nil)
# = "1-none-[]-{}-bar"
# > Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# /projects/foo.rb:29
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# < Foo#foo(2, *[:a, :b, :c], :some, **{:name=>:value}, &nil)
# = "2-some-[a,b,c]-{:name=>:value}-bar"
# > Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
# /projects/foo.rb:30
# > Foo#bar()
# /projects/foo.rb:15
# < Foo#bar()
# = :bar
# 3--[]-{}-bar
# < Foo#foo(3, *[], nil, **{}, &#<Proc:0x0000000100900578 /projects/foo.rb:30>)
# = nil
17 changes: 17 additions & 0 deletions examples/kernel_calls.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

puts <<~INFO
This example traces calls for very basic Ruby objects when a new Class is
generated and pretty_print is automatically loaded.
INFO

require 'im-lost'

ImLost.trace_results = true
ImLost.trace(Kernel, Object, Module, Class, self) do
puts '=' * 79
pp Class.new
puts '=' * 79
end
32 changes: 32 additions & 0 deletions im-lost.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require_relative 'lib/im-lost/version'

Gem::Specification.new do |spec|
spec.name = 'im-lost'
spec.version = ImLost::VERSION
spec.summary = 'Your debugging helper.'
spec.description = <<~DESCRIPTION
If you have overlooked something again and don't really understand what
your code is doing. If you have to maintain this application but can't
really find your way around and certainly can't track down that stupid
error. If you feel lost in all that code, here's the gem to help you out!
ImLost helps you by analyzing function calls of objects, informing you
about exceptions and logging your way through your code. In short, ImLost
is your debugging helper!
DESCRIPTION

spec.author = 'Mike Blumtritt'
spec.license = 'MIT'
spec.homepage = 'https://github.com/mblumtritt/im-lost'
spec.metadata['source_code_uri'] = spec.homepage
spec.metadata['bug_tracker_uri'] = "#{spec.homepage}/issues"
spec.metadata['documentation_uri'] = 'https://rubydoc.info/gems/im-lost'
spec.metadata['rubygems_mfa_required'] = 'true'

spec.required_ruby_version = '>= 3.0'

spec.files = Dir['lib/**/*'] + Dir['examples/**/*']
spec.extra_rdoc_files = %w[README.md LICENSE]
end
Loading

0 comments on commit 1567237

Please sign in to comment.