defly 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "shoulda", ">= 0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.6.4"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.6.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rake (0.9.2)
10
+ rcov (0.9.9)
11
+ shoulda (2.11.3)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (~> 1.0.0)
18
+ jeweler (~> 1.6.4)
19
+ rcov
20
+ shoulda
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Andrew Liu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # defly
2
+
3
+ A ruby debugging tool. Easy way to trace function calls and variables.
4
+
5
+ ## Install
6
+
7
+ Just `gem install defly`.
8
+
9
+ ## Sample Usage
10
+
11
+ ### Input
12
+
13
+ ```ruby
14
+ require 'defly'
15
+ class Warrior
16
+ attr_accessor :hp, :mp
17
+
18
+ def sleep
19
+ self.hp += 10
20
+ self.mp += 2
21
+ end
22
+ end
23
+
24
+ Warrior.new.trace([:hp, :hp=, :mp, :mp=], [:@hp, :@mp]) do |warrior|
25
+ warrior.hp = 10
26
+ warrior.mp = 20
27
+ warrior.sleep
28
+ end
29
+ ```
30
+
31
+ ### Output (Run on IRB)
32
+
33
+ Tracing hp, hp=, mp, mp= on Warrior instance
34
+ Tracing @hp, @mp on Warrior instance
35
+ <<<<< Warrior#hp=(10) # (irb):12:in `block in irb_binding'
36
+ @hp = 10 # undefined
37
+ @mp = nil # undefined
38
+ >>>>> 10
39
+ <<<<< Warrior#mp=(20) # (irb):13:in `block in irb_binding'
40
+ @mp = 20 # undefined
41
+ >>>>> 20
42
+ <<<<< Warrior#hp() # (irb):6:in `sleep'
43
+ >>>>> 10
44
+ <<<<< Warrior#hp=(20) # (irb):6:in `sleep'
45
+ @hp = 20 # 10 -> 20
46
+ >>>>> 20
47
+ <<<<< Warrior#mp() # (irb):7:in `sleep'
48
+ >>>>> 20
49
+ <<<<< Warrior#mp=(22) # (irb):7:in `sleep'
50
+ @mp = 22 # 20 -> 22
51
+ >>>>> 22
52
+ => #<Warrior:0x00000100987488 @defly_methods=[:hp, :hp=, :mp, :mp=], @defly_variables=[:@hp, :@mp], @__defly_level=0, @hp=20, @__defly={:@hp=>20, :@mp=>22}, @mp=22>
53
+
54
+
55
+ ## Copyright
56
+
57
+ Copyright (c) 2011 Andrew Liu. See LICENSE.txt for
58
+ further details.
59
+
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "defly"
18
+ gem.homepage = "http://github.com/eggegg/defly"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Debug and trace tool for Ruby}
21
+ gem.description = %Q{Trace methods and instance variables with ease!}
22
+ gem.email = "andrewliu33@gmail.com"
23
+ gem.authors = ["Andrew Liu"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ require 'rcov/rcovtask'
36
+ Rcov::RcovTask.new do |test|
37
+ test.libs << 'test'
38
+ test.pattern = 'test/**/test_*.rb'
39
+ test.verbose = true
40
+ test.rcov_opts << '--exclude "gems/*"'
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "defly #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{defly}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Andrew Liu"]
12
+ s.date = %q{2011-08-01}
13
+ s.description = %q{Trace methods and instance variables with ease!}
14
+ s.email = %q{andrewliu33@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "defly.gemspec",
28
+ "lib/defly.rb",
29
+ "lib/tracable.rb",
30
+ "test/helper.rb",
31
+ "test/test_defly.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/eggegg/defly}
34
+ s.licenses = ["MIT"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.6.2}
37
+ s.summary = %q{Debug and trace tool for Ruby}
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_development_dependency(%q<shoulda>, [">= 0"])
44
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
45
+ s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
46
+ s.add_development_dependency(%q<rcov>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<shoulda>, [">= 0"])
49
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
50
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
51
+ s.add_dependency(%q<rcov>, [">= 0"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<shoulda>, [">= 0"])
55
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
57
+ s.add_dependency(%q<rcov>, [">= 0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,6 @@
1
+ require 'tracable'
2
+
3
+ module Defly
4
+ end
5
+
6
+ Object.send(:include, Defly::Tracable)
@@ -0,0 +1,93 @@
1
+ module Defly
2
+ module Tracable
3
+ def trace(methods, variables, &block)
4
+ trace_method!(methods)
5
+ trace_variable!(variables)
6
+ yield self
7
+ untrace!
8
+
9
+ self
10
+ end
11
+
12
+ def trace_method!(methods)
13
+ @defly_methods ||= []
14
+
15
+ methods = public_methods(false) if methods.size == 0
16
+ methods.map! {|m| m.to_sym }
17
+ methods -= @defly_methods
18
+ @defly_methods |= methods
19
+
20
+ STDERR << "Tracing #{methods.join(', ')} on #{self.class} instance\n"
21
+
22
+ methods.each do |m|
23
+ eigenclass.class_eval <<-RUBY, __FILE__, __LINE__
24
+ def #{m}(*args, &block)
25
+ @__defly_level ||= 0
26
+ indent = ' ' * @__defly_level
27
+
28
+ begin
29
+ STDERR << "\#{indent}<<<<< #{self.class}##{m}(\#{args.join(', ')}) # \#{caller[0]}\n"
30
+ @__defly_level += 1
31
+ result = super
32
+ @__defly_level -= 1
33
+ defly_check_var(indent) if defined? defly_check_var
34
+ STDERR << "\#{indent}>>>>> \#{result}\n"
35
+ result
36
+ rescue
37
+ STDERR << "#{m}: \#{$!.class}: \#{$!.message}\n"
38
+ raise
39
+ end
40
+ end
41
+ RUBY
42
+ end
43
+ end
44
+
45
+ def trace_variable!(variables)
46
+ @defly_variables ||= []
47
+
48
+ variables = instance_variables if variables.size == 0
49
+ variables.map! {|v| v.to_sym }
50
+ variables -= @defly_variables
51
+ @defly_variables |= variables
52
+
53
+ STDERR << "Tracing #{variables.join(', ')} on #{self.class} instance\n"
54
+
55
+ eigenclass.remove_method :defly_check_var_list if eigenclass.instance_methods.include? :defly_check_var_list
56
+
57
+ eigenclass.class_eval <<-RUBY, __FILE__, __LINE__
58
+ def defly_check_var_list
59
+ [#{@defly_variables.map(&:inspect).join(', ')}]
60
+ end
61
+ RUBY
62
+
63
+ unless eigenclass.instance_methods.include? :defly_check_var
64
+ eigenclass.class_eval <<-RUBY, __FILE__, __LINE__
65
+ def defly_check_var(indent)
66
+ @__defly ||= {}
67
+
68
+ var_list = defly_check_var_list
69
+ var_list.each do |v|
70
+ new_val = instance_variable_get(v)
71
+ if @__defly[v]
72
+ STDERR << " \#{indent}\#{v.to_s} = \#{new_val.inspect} # \#{@__defly[v].inspect} -> \#{new_val.inspect}\n" unless @__defly[v] == new_val
73
+ else
74
+ STDERR << " \#{indent}\#{v.to_s} = \#{new_val.inspect} # undefined\n"
75
+ end
76
+ @__defly[v] = new_val
77
+ end
78
+ end
79
+ RUBY
80
+ end
81
+ end
82
+
83
+ def untrace!
84
+ [:defly_check_var_list, :defly_check_var].concat(@defly_methods) do |m|
85
+ eigenclass.remove_method(m) if eigenclass.instance_methods.include? m
86
+ end
87
+ end
88
+
89
+ def eigenclass
90
+ class << self; self; end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+ require 'shoulda'
12
+
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
15
+ require 'defly'
16
+
17
+ class Test::Unit::TestCase
18
+ end
@@ -0,0 +1,20 @@
1
+ require 'helper'
2
+
3
+ class TestDefly < Test::Unit::TestCase
4
+ should "properly trace" do
5
+ class C
6
+ attr_accessor :a
7
+
8
+ def b c
9
+ self.a = c
10
+ @a = 10
11
+ end
12
+ end
13
+
14
+ C.new.trace([:a, :a=, :b], [:@a]) do |d|
15
+ d.a = 10
16
+ d.a = 20
17
+ d.b 40
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: defly
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Liu
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-01 00:00:00.000000000 +08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: shoulda
17
+ requirement: &2156047920 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2156047920
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &2156046920 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *2156046920
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &2156046080 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.6.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *2156046080
48
+ - !ruby/object:Gem::Dependency
49
+ name: rcov
50
+ requirement: &2156045280 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *2156045280
59
+ description: Trace methods and instance variables with ease!
60
+ email: andrewliu33@gmail.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files:
64
+ - LICENSE.txt
65
+ - README.md
66
+ files:
67
+ - .document
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - VERSION
74
+ - defly.gemspec
75
+ - lib/defly.rb
76
+ - lib/tracable.rb
77
+ - test/helper.rb
78
+ - test/test_defly.rb
79
+ has_rdoc: true
80
+ homepage: http://github.com/eggegg/defly
81
+ licenses:
82
+ - MIT
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ segments:
94
+ - 0
95
+ hash: -1991178412336427869
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.6.2
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Debug and trace tool for Ruby
108
+ test_files: []