Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
MadBomber committed Sep 18, 2014
0 parents commit 926af01
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/.bundle/
/.yardoc
/Gemfile.lock
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
*.bundle
*.so
*.o
*.a
mkmf.log
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in debug_me.gemspec
gemspec
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
If you want it, its yours.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# DebugMe

This thing is pretty old. There are much better
ways of debugging in a complex application. But,
you know, I keep returning to this little method
time after time. I guess that marks me as a geezer.

A tool to print the labeled value of variables.

Works with local, instance and class variables.

## Installation

Add this line to your application's Gemfile:

```ruby
gem 'debug_me'
```

And then execute:

$ bundle

Or install it yourself as:

$ gem install debug_me

## Examples Usage

```ruby
require 'debug_me'
include DebugMe

debug_me # Prints only the header banner consisting of tag, method name, file name and line number

debug_me('INFO') # Also prints only the header but with a different tag

debug_me {} # prints the default header and __ALL__ variables

debug_me {:just_this_variable} # prints the default header and the value of only one specific variable

debug_me { [:this_one, :that_one, :that_other_one] } # prints default header and three specific variables

# Use an array of symbols and strings to pass multiple variables for output
# Each element of the array is 'eval'ed with the context binding of the caller
debug_me(){[ :my_var, 'my_complex_var_or_method[my_var]' ]}

debug_me(:header => false) {} # disables the printing of the header; prints all variables

debug_me(:tag => 'MyTag', :header => false) {} # disables header, sets different tag, prints all variables

debug_me('=== LOOK ===') {} # changes the tag and prints all variables with a header line

debug_me('=== LOOK ===') {:@foo} # changes the tag, prints a header line and a specific instance variable

debug_me('=== LOOK ===') {:@@foo} # changes the tag, prints a header line and a specific class variable

debug_me(:ivar => false, :cvar => false) {} # print only the local variables with the default tag and a header line

```

## Contributing

1. Fork it ( https://github.com/[my-github-username]/debug_me/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "bundler/gem_tasks"

26 changes: 26 additions & 0 deletions debug_me.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'debug_me/version'

Gem::Specification.new do |spec|
spec.name = "debug_me"
spec.version = DebugMe::VERSION
spec.authors = ["Dewayne VanHoozer"]
spec.email = ["dvanhoozer@gmail.com"]
spec.summary = "A tool to print the labeled value of variables."
spec.description = %q{This thing is pretty old. There are much better
ways of debugging in a complex application. But,
you know, I keep returning to this little method
time after time. I guess that marks me as a geezer.}
spec.homepage = "http://github.com/MadBomber/debug_me"
spec.license = "You want it, its yours"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_development_dependency "bundler", "~> 1.7"
spec.add_development_dependency "rake", "~> 10.0"
end
60 changes: 60 additions & 0 deletions lib/debug_me.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'pp'
require "debug_me/version"

module DebugMe

def debug_me( options={}, &block )

default_options = {
:tag => 'DEBUG:', # A tag to prepend to each output line
:time => true, # Include a time-stamp in front of the tag
:header => true, # Print a header string before printing the variables
:ivar => true, # Include instance variables in the output
:cvar => true, # Include class variables in the output
:file => $stdout # The output file
}

if 'Hash' == options.class.to_s
options = default_options.merge(options)
else
options = default_options.merge({:tag => options})
end

f = options[:file]
s = ""
s += "#{sprintf('%010.6f', Time.now.to_f)} " if options[:time]
s += " #{options[:tag]}"
wf = caller # where_from under 1.8.6 its a stack trace array under 1.8.7 is a string
wf = wf[0] if 'Array' == wf.class.to_s

f.puts "#{s} Source: #{wf}" if options[:header]

if block_given?

block_value = [ block.call ].flatten.compact

if block_value.empty?
block_value = eval('local_variables', block.binding)
block_value += [ eval('instance_variables', block.binding) ] if options[:ivar]
block_value += [ self.class.send('class_variables') ] if options[:cvar]
block_value = block_value.flatten.compact
else
block_value.map! { |v| v.to_s }
end

block_value.each do |v|
ev = eval(v, block.binding)
f.puts "#{s} #{v} -=> #{pp ev}" #.pretty_inspect}"
end

end ## if block_given?

f.flush

end ## def debug_me( options={}, &block )

# def log_me(msg, opts={})
# debug_me({:tag => msg, :header => false}.merge(opts))
# end

end # module DebugMe
3 changes: 3 additions & 0 deletions lib/debug_me/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module DebugMe
VERSION = "1.0.0"
end

0 comments on commit 926af01

Please sign in to comment.