method_source 0.6.0 → 0.6.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +28 -1
- data/lib/method_source/source_location.rb +74 -52
- data/lib/method_source/version.rb +1 -1
- data/test/test.rb +19 -17
- data/test/test_helper.rb +5 -0
- metadata +39 -47
data/README.markdown
CHANGED
@@ -15,7 +15,8 @@ Method comments can also be extracted using the `comment` method.
|
|
15
15
|
|
16
16
|
It is written in pure Ruby (no C).
|
17
17
|
|
18
|
-
* Some Ruby 1.8 support now available.
|
18
|
+
* Some Ruby 1.8 support now available.
|
19
|
+
* Support for MRI, RBX, JRuby.
|
19
20
|
|
20
21
|
`method_source` provides the `source` and `comment` methods to the `Method` and
|
21
22
|
`UnboundMethod` and `Proc` classes.
|
@@ -60,3 +61,29 @@ Special Thanks
|
|
60
61
|
[Adam Sanderson](https://github.com/adamsanderson) for `comment` functionality.
|
61
62
|
|
62
63
|
[Dmitry Elastic](https://github.com/dmitryelastic) for the brilliant Ruby 1.8 `source_location` hack.
|
64
|
+
|
65
|
+
License
|
66
|
+
-------
|
67
|
+
|
68
|
+
(The MIT License)
|
69
|
+
|
70
|
+
Copyright (c) 2011 John Mair (banisterfiend)
|
71
|
+
|
72
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
73
|
+
a copy of this software and associated documentation files (the
|
74
|
+
'Software'), to deal in the Software without restriction, including
|
75
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
76
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
77
|
+
permit persons to whom the Software is furnished to do so, subject to
|
78
|
+
the following conditions:
|
79
|
+
|
80
|
+
The above copyright notice and this permission notice shall be
|
81
|
+
included in all copies or substantial portions of the Software.
|
82
|
+
|
83
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
84
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
85
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
86
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
87
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
88
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
89
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,31 +1,42 @@
|
|
1
1
|
module MethodSource
|
2
2
|
module SourceLocation
|
3
3
|
module MethodExtensions
|
4
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
5
|
+
require 'java'
|
4
6
|
|
5
|
-
|
6
|
-
return
|
7
|
-
|
7
|
+
# JRuby version source_location hack
|
8
|
+
# @return [Array] A two element array containing the source location of the method
|
9
|
+
def source_location
|
10
|
+
to_java.source_location(Thread.current.to_java.getContext())
|
11
|
+
end
|
12
|
+
else
|
8
13
|
|
9
|
-
@file, @line = file, line
|
10
|
-
raise :found
|
11
|
-
end
|
12
14
|
|
13
|
-
|
15
|
+
def trace_func(event, file, line, id, binding, classname)
|
16
|
+
return unless event == 'call'
|
17
|
+
set_trace_func nil
|
18
|
+
|
19
|
+
@file, @line = file, line
|
20
|
+
raise :found
|
21
|
+
end
|
14
22
|
|
15
|
-
|
16
|
-
# @return [Array] A two element array. First element is the
|
17
|
-
# file, second element is the line in the file where the
|
18
|
-
# method definition is found.
|
19
|
-
def source_location
|
20
|
-
if @file.nil?
|
21
|
-
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
|
23
|
+
private :trace_func
|
22
24
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
25
|
+
# Return the source location of a method for Ruby 1.8.
|
26
|
+
# @return [Array] A two element array. First element is the
|
27
|
+
# file, second element is the line in the file where the
|
28
|
+
# method definition is found.
|
29
|
+
def source_location
|
30
|
+
if @file.nil?
|
31
|
+
args =[*(1..(arity<-1 ? -arity-1 : arity ))]
|
32
|
+
|
33
|
+
set_trace_func method(:trace_func).to_proc
|
34
|
+
call(*args) rescue nil
|
35
|
+
set_trace_func nil
|
36
|
+
@file = File.expand_path(@file) if @file && File.exist?(File.expand_path(@file))
|
37
|
+
end
|
38
|
+
return [@file, @line] if File.exist?(@file.to_s)
|
27
39
|
end
|
28
|
-
return [@file, @line] if File.exist?(@file.to_s)
|
29
40
|
end
|
30
41
|
end
|
31
42
|
|
@@ -56,43 +67,54 @@ module MethodSource
|
|
56
67
|
end
|
57
68
|
|
58
69
|
module UnboundMethodExtensions
|
70
|
+
if defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /jruby/
|
71
|
+
require 'java'
|
59
72
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
def source_location
|
65
|
-
klass = case owner
|
66
|
-
when Class
|
67
|
-
owner
|
68
|
-
when Module
|
69
|
-
method_owner = owner
|
70
|
-
Class.new { include(method_owner) }
|
71
|
-
end
|
72
|
-
|
73
|
-
# deal with immediate values
|
74
|
-
case
|
75
|
-
when klass == Symbol
|
76
|
-
return :a.method(name).source_location
|
77
|
-
when klass == Fixnum
|
78
|
-
return 0.method(name).source_location
|
79
|
-
when klass == TrueClass
|
80
|
-
return true.method(name).source_location
|
81
|
-
when klass == FalseClass
|
82
|
-
return false.method(name).source_location
|
83
|
-
when klass == NilClass
|
84
|
-
return nil.method(name).source_location
|
73
|
+
# JRuby version source_location hack
|
74
|
+
# @return [Array] A two element array containing the source location of the method
|
75
|
+
def source_location
|
76
|
+
to_java.source_location(Thread.current.to_java.getContext())
|
85
77
|
end
|
78
|
+
else
|
79
|
+
|
80
|
+
|
81
|
+
# Return the source location of an instance method for Ruby 1.8.
|
82
|
+
# @return [Array] A two element array. First element is the
|
83
|
+
# file, second element is the line in the file where the
|
84
|
+
# method definition is found.
|
85
|
+
def source_location
|
86
|
+
klass = case owner
|
87
|
+
when Class
|
88
|
+
owner
|
89
|
+
when Module
|
90
|
+
method_owner = owner
|
91
|
+
Class.new { include(method_owner) }
|
92
|
+
end
|
93
|
+
|
94
|
+
# deal with immediate values
|
95
|
+
case
|
96
|
+
when klass == Symbol
|
97
|
+
return :a.method(name).source_location
|
98
|
+
when klass == Fixnum
|
99
|
+
return 0.method(name).source_location
|
100
|
+
when klass == TrueClass
|
101
|
+
return true.method(name).source_location
|
102
|
+
when klass == FalseClass
|
103
|
+
return false.method(name).source_location
|
104
|
+
when klass == NilClass
|
105
|
+
return nil.method(name).source_location
|
106
|
+
end
|
86
107
|
|
87
|
-
|
88
|
-
|
89
|
-
|
108
|
+
begin
|
109
|
+
klass.allocate.method(name).source_location
|
110
|
+
rescue TypeError
|
90
111
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
112
|
+
# Assume we are dealing with a Singleton Class:
|
113
|
+
# 1. Get the instance object
|
114
|
+
# 2. Forward the source_location lookup to the instance
|
115
|
+
instance ||= ObjectSpace.each_object(owner).first
|
116
|
+
instance.method(name).source_location
|
117
|
+
end
|
96
118
|
end
|
97
119
|
end
|
98
120
|
end
|
data/test/test.rb
CHANGED
@@ -8,10 +8,12 @@ require "#{direc}/test_helper"
|
|
8
8
|
|
9
9
|
describe MethodSource do
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
if !jruby?
|
12
|
+
describe "emitted warnings" do
|
13
|
+
it 'should emit no warnings' do
|
14
|
+
Open4.popen4 'ruby -I lib -rubygems -r"method_source" -W -e "exit"' do |pid,stdin,stdout,stderr|
|
15
|
+
stderr.read.empty?.should == true
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -81,23 +83,23 @@ describe MethodSource do
|
|
81
83
|
end
|
82
84
|
|
83
85
|
# if RUBY_VERSION =~ /1.9/ || is_rbx?
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
86
|
+
describe "Lambdas and Procs" do
|
87
|
+
it 'should return source for proc' do
|
88
|
+
MyProc.source.should == @proc_source
|
89
|
+
end
|
88
90
|
|
89
|
-
|
90
|
-
|
91
|
-
|
91
|
+
it 'should return an empty string if there is no comment' do
|
92
|
+
MyProc.comment.should == ''
|
93
|
+
end
|
92
94
|
|
93
|
-
|
94
|
-
|
95
|
-
|
95
|
+
it 'should return source for lambda' do
|
96
|
+
MyLambda.source.should == @lambda_source
|
97
|
+
end
|
96
98
|
|
97
|
-
|
98
|
-
|
99
|
-
end
|
99
|
+
it 'should return comment for lambda' do
|
100
|
+
MyLambda.comment.should == @lambda_comment
|
100
101
|
end
|
102
|
+
end
|
101
103
|
# end
|
102
104
|
describe "Comment tests" do
|
103
105
|
before do
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,59 +1,55 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_source
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.5
|
4
5
|
prerelease:
|
5
|
-
version: 0.6.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- John Mair (banisterfiend)
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-09-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: ruby_parser
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &70251253612220 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 2.0.5
|
24
22
|
type: :runtime
|
25
|
-
version_requirements: *id001
|
26
|
-
- !ruby/object:Gem::Dependency
|
27
|
-
name: bacon
|
28
23
|
prerelease: false
|
29
|
-
|
24
|
+
version_requirements: *70251253612220
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: bacon
|
27
|
+
requirement: &70251253611520 !ruby/object:Gem::Requirement
|
30
28
|
none: false
|
31
|
-
requirements:
|
32
|
-
- -
|
33
|
-
- !ruby/object:Gem::Version
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
34
32
|
version: 1.1.0
|
35
33
|
type: :development
|
36
|
-
version_requirements: *id002
|
37
|
-
- !ruby/object:Gem::Dependency
|
38
|
-
name: open4
|
39
34
|
prerelease: false
|
40
|
-
|
35
|
+
version_requirements: *70251253611520
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: open4
|
38
|
+
requirement: &70251253610760 !ruby/object:Gem::Requirement
|
41
39
|
none: false
|
42
|
-
requirements:
|
40
|
+
requirements:
|
43
41
|
- - ~>
|
44
|
-
- !ruby/object:Gem::Version
|
42
|
+
- !ruby/object:Gem::Version
|
45
43
|
version: 1.0.1
|
46
44
|
type: :development
|
47
|
-
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70251253610760
|
48
47
|
description: retrieve the sourcecode for a method
|
49
48
|
email: jrmair@gmail.com
|
50
49
|
executables: []
|
51
|
-
|
52
50
|
extensions: []
|
53
|
-
|
54
51
|
extra_rdoc_files: []
|
55
|
-
|
56
|
-
files:
|
52
|
+
files:
|
57
53
|
- lib/method_source/source_location.rb
|
58
54
|
- lib/method_source/version.rb
|
59
55
|
- lib/method_source.rb
|
@@ -64,30 +60,26 @@ files:
|
|
64
60
|
- .gemtest
|
65
61
|
homepage: http://banisterfiend.wordpress.com
|
66
62
|
licenses: []
|
67
|
-
|
68
63
|
post_install_message:
|
69
64
|
rdoc_options: []
|
70
|
-
|
71
|
-
require_paths:
|
65
|
+
require_paths:
|
72
66
|
- lib
|
73
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
68
|
none: false
|
75
|
-
requirements:
|
76
|
-
- -
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version:
|
79
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ! '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
74
|
none: false
|
81
|
-
requirements:
|
82
|
-
- -
|
83
|
-
- !ruby/object:Gem::Version
|
84
|
-
version:
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: '0'
|
85
79
|
requirements: []
|
86
|
-
|
87
80
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.
|
81
|
+
rubygems_version: 1.8.6
|
89
82
|
signing_key:
|
90
83
|
specification_version: 3
|
91
84
|
summary: retrieve the sourcecode for a method
|
92
85
|
test_files: []
|
93
|
-
|