method_introspection 0.0.2 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of method_introspection might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/method_introspection/code_helpers.rb +69 -57
- data/lib/method_introspection/load_project.rb +0 -0
- data/lib/method_introspection/module_methods.rb +30 -15
- data/lib/method_introspection/source_location.rb +2 -2
- data/lib/method_introspection/version/version.rb +1 -1
- data/lib/method_introspection.rb +11 -2
- data/test/test.rb +4 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5568e7b6d6019161fe15bc2d2e2b1a8fab014096
|
4
|
+
data.tar.gz: 8908d1e7888d9189d4b4b3f0a8183e8df75affe8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f951b118a85c7b0bd7922ae0398c4024e82f681739054b539b6a83cd13168644239355fdf949a958d4a6d63cd82bb9a0013c649fe25a1f14b4ce5132f166cdc4
|
7
|
+
data.tar.gz: 97612912cfa8e08264333f83e92baba4bae9fd1acab912b8e1647adf8e0a14cf80845408d6f5bfaaf704e182193bf57ef4eb4aca4375c6fc8cf971bcb6fd5431
|
@@ -2,6 +2,62 @@ module MethodIntrospection
|
|
2
2
|
|
3
3
|
module CodeHelpers
|
4
4
|
|
5
|
+
# ========================================================================= #
|
6
|
+
# === extract_last_comment
|
7
|
+
#
|
8
|
+
# Get the last comment from the input. We will build up the result.
|
9
|
+
#
|
10
|
+
# @param [Array<String>] lines
|
11
|
+
# @return [String]
|
12
|
+
# ========================================================================= #
|
13
|
+
def extract_last_comment(lines)
|
14
|
+
result = ''
|
15
|
+
lines.each { |line|
|
16
|
+
# ===================================================================== #
|
17
|
+
# Add any line that is a valid ruby comment but stop the moment
|
18
|
+
# we hit a non-comment line.
|
19
|
+
# ===================================================================== #
|
20
|
+
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
|
21
|
+
# =================================================================== #
|
22
|
+
# Append onto result next. In the past we called .lstrip() here but
|
23
|
+
# this is no longer legal as we modify the original comment.
|
24
|
+
# =================================================================== #
|
25
|
+
result << line
|
26
|
+
else
|
27
|
+
result.replace("")
|
28
|
+
end
|
29
|
+
}
|
30
|
+
return result
|
31
|
+
end; private :extract_last_comment
|
32
|
+
|
33
|
+
# ========================================================================= #
|
34
|
+
# === comment_describing
|
35
|
+
#
|
36
|
+
# This method will retrieve the comment describing the expression on the
|
37
|
+
# given line of the given file.
|
38
|
+
#
|
39
|
+
# This is useful to get the module or method documentation, in String
|
40
|
+
# format.
|
41
|
+
#
|
42
|
+
# @param [Array<String>, File, String] file The file to parse, either as a File or as
|
43
|
+
# a String or an Array of lines.
|
44
|
+
# @param [Fixnum] line_number The line number at which to look.
|
45
|
+
# NOTE: The first line in a file is line 1!
|
46
|
+
# @return [String] The comment
|
47
|
+
# ========================================================================= #
|
48
|
+
def comment_describing(file, line_number)
|
49
|
+
if file.is_a? Array
|
50
|
+
lines = file
|
51
|
+
else
|
52
|
+
lines = file.each_line.to_a
|
53
|
+
end
|
54
|
+
# ======================================================================= #
|
55
|
+
# At this point, lines is now an Array. We extract the proper lines
|
56
|
+
# by making use of the line number.
|
57
|
+
# ======================================================================= #
|
58
|
+
extract_last_comment(lines[0..(line_number - 2)])
|
59
|
+
end
|
60
|
+
|
5
61
|
# ========================================================================= #
|
6
62
|
# === expression_at
|
7
63
|
#
|
@@ -55,30 +111,6 @@ module CodeHelpers
|
|
55
111
|
end
|
56
112
|
end
|
57
113
|
|
58
|
-
# ========================================================================= #
|
59
|
-
# === comment_describing
|
60
|
-
#
|
61
|
-
# This method will retrieve the comment describing the expression on the
|
62
|
-
# given line of the given file.
|
63
|
-
#
|
64
|
-
# This is useful to get the module or method documentation, in String
|
65
|
-
# format.
|
66
|
-
#
|
67
|
-
# @param [Array<String>, File, String] file The file to parse, either as a File or as
|
68
|
-
# a String or an Array of lines.
|
69
|
-
# @param [Fixnum] line_number The line number at which to look.
|
70
|
-
# NOTE: The first line in a file is line 1!
|
71
|
-
# @return [String] The comment
|
72
|
-
# ========================================================================= #
|
73
|
-
def comment_describing(file, line_number)
|
74
|
-
if file.is_a? Array
|
75
|
-
lines = file
|
76
|
-
else
|
77
|
-
lines = file.each_line.to_a
|
78
|
-
end
|
79
|
-
extract_last_comment(lines[0..(line_number - 2)])
|
80
|
-
end
|
81
|
-
|
82
114
|
# ========================================================================= #
|
83
115
|
# === complete_expression?
|
84
116
|
#
|
@@ -87,10 +119,12 @@ module CodeHelpers
|
|
87
119
|
# @param [String] code The code to validate.
|
88
120
|
# @return [Boolean] Whether or not the code is a complete Ruby expression.
|
89
121
|
# @raise [SyntaxError] Any SyntaxError that does not represent incompleteness.
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
# complete_expression?("class
|
122
|
+
#
|
123
|
+
# @examples
|
124
|
+
#
|
125
|
+
# complete_expression?("class Hello") # => false
|
126
|
+
# complete_expression?("class Hello; end") # => true
|
127
|
+
# complete_expression?("class 123") # => SyntaxError: unexpected tINTEGER
|
94
128
|
# ========================================================================= #
|
95
129
|
def complete_expression?(i)
|
96
130
|
old_verbose = $VERBOSE
|
@@ -103,10 +137,9 @@ module CodeHelpers
|
|
103
137
|
rescue IncompleteExpression
|
104
138
|
false
|
105
139
|
ensure
|
106
|
-
$VERBOSE = old_verbose
|
140
|
+
$VERBOSE = old_verbose # Reinstate the old verbosity level here.
|
107
141
|
end
|
108
142
|
|
109
|
-
private
|
110
143
|
# ========================================================================= #
|
111
144
|
# === extract_first_expression
|
112
145
|
#
|
@@ -126,39 +159,18 @@ module CodeHelpers
|
|
126
159
|
return code if complete_expression?(block ? block.call(code) : code)
|
127
160
|
}
|
128
161
|
raise SyntaxError, "unexpected $end"
|
129
|
-
end
|
130
|
-
|
131
|
-
# ========================================================================= #
|
132
|
-
# === extract_last_comment
|
133
|
-
#
|
134
|
-
# Get the last comment from the input. We will build up the result.
|
135
|
-
#
|
136
|
-
# @param [Array<String>] lines
|
137
|
-
# @return [String]
|
138
|
-
# ========================================================================= #
|
139
|
-
def extract_last_comment(lines)
|
140
|
-
result = ''
|
141
|
-
lines.each { |line|
|
142
|
-
# ===================================================================== #
|
143
|
-
# Add any line that is a valid ruby comment but stop the moment
|
144
|
-
# we hit a non-comment line.
|
145
|
-
# ===================================================================== #
|
146
|
-
if (line =~ /^\s*#/) || (line =~ /^\s*$/)
|
147
|
-
result << line.lstrip
|
148
|
-
else
|
149
|
-
result.replace("")
|
150
|
-
end
|
151
|
-
}
|
152
|
-
return result
|
153
|
-
end
|
162
|
+
end; private :extract_first_expression
|
154
163
|
|
155
164
|
# ========================================================================= #
|
156
165
|
# === IncompleteExpression
|
157
166
|
#
|
158
|
-
#
|
159
|
-
# fixed by adding more input to the buffer.
|
167
|
+
# IncompleteExpression is an exception matcher that matches only subsets
|
168
|
+
# of SyntaxErrors that can be fixed by adding more input to the buffer.
|
160
169
|
# ========================================================================= #
|
161
170
|
module IncompleteExpression
|
171
|
+
# ======================================================================= #
|
172
|
+
# === GENERIC_REGEXPS
|
173
|
+
# ======================================================================= #
|
162
174
|
GENERIC_REGEXPS = [
|
163
175
|
/unexpected (\$end|end-of-file|end-of-input|END_OF_FILE)/, # mri, jruby, ruby-2.0, ironruby
|
164
176
|
/embedded document meets end of file/, # =begin
|
File without changes
|
@@ -44,7 +44,8 @@ module MethodIntrospection # The top namespace of this project.
|
|
44
44
|
# ========================================================================= #
|
45
45
|
# === MethodIntrospection.raise_this
|
46
46
|
#
|
47
|
-
#
|
47
|
+
# The second argument to this method is optional, and allows for a longer
|
48
|
+
# description of the error at hand.
|
48
49
|
# ========================================================================= #
|
49
50
|
def self.raise_this(name, optional_extra = nil)
|
50
51
|
result = "We could not locate the source for #{name}"
|
@@ -63,14 +64,22 @@ module MethodIntrospection # The top namespace of this project.
|
|
63
64
|
#
|
64
65
|
# This is defined here to avoid polluting the `Method` class.
|
65
66
|
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
#
|
67
|
+
# @param [Array] source_location The array returned by Method#source_location
|
68
|
+
# @param [String] method_name
|
69
|
+
# @return [String] The method body
|
69
70
|
# ========================================================================= #
|
70
71
|
def self.source_helper(source_location, name = nil)
|
71
72
|
raise_this(name) unless source_location
|
72
|
-
|
73
|
-
|
73
|
+
this_file, line_number = *source_location
|
74
|
+
# ======================================================================= #
|
75
|
+
# Set the file.
|
76
|
+
# ======================================================================= #
|
77
|
+
MethodIntrospection.set_this_file(this_file)
|
78
|
+
# ======================================================================= #
|
79
|
+
# Set the line number in that file.
|
80
|
+
# ======================================================================= #
|
81
|
+
MethodIntrospection.set_line_number(line_number)
|
82
|
+
expression_at(lines_for(this_file), line_number)
|
74
83
|
rescue SyntaxError => error
|
75
84
|
raise SourceNotFoundError,
|
76
85
|
"Could not parse source for #{name}: #{error.message}"
|
@@ -96,18 +105,19 @@ module MethodIntrospection # The top namespace of this project.
|
|
96
105
|
# ========================================================================= #
|
97
106
|
# === MethodIntrospection.comment_helper
|
98
107
|
#
|
99
|
-
#
|
100
|
-
# the comments for a specified method.
|
101
|
-
# polluting the `Method` class.
|
108
|
+
# This is a helper method responsible for opening source file and
|
109
|
+
# buffering up the comments for a specified method. It is defined here
|
110
|
+
# to avoid polluting the `Method` class.
|
102
111
|
#
|
103
112
|
# The expected input should be an Array, which is returned by the
|
104
113
|
# method Method#source_location.
|
105
114
|
#
|
106
115
|
# This will typically be in the form of:
|
107
116
|
#
|
108
|
-
#
|
117
|
+
# ["/Programs/Ruby/2.2.3/lib/ruby/site_ruby/2.2.0/config.rb", 297]
|
109
118
|
#
|
110
|
-
# The first one yields to us the name
|
119
|
+
# The first one yields to us the name of the file in question, and the
|
120
|
+
# second gives us the specific line number.
|
111
121
|
#
|
112
122
|
# @param [String] method_name
|
113
123
|
# @return [String] The comments up to the point of the method.
|
@@ -115,12 +125,17 @@ module MethodIntrospection # The top namespace of this project.
|
|
115
125
|
def self.comment_helper(source_location, name = nil)
|
116
126
|
raise_this(name) unless source_location
|
117
127
|
this_file, line_number = *source_location
|
118
|
-
pp source_location
|
119
|
-
pp this_file
|
120
|
-
pp line_number
|
121
128
|
MethodIntrospection.set_this_file(this_file)
|
122
129
|
MethodIntrospection.set_line_number(line_number)
|
123
|
-
|
130
|
+
# ======================================================================= #
|
131
|
+
# Next, we extract all the lines of a given file. lines will thus be a
|
132
|
+
# very big Array if we have a big file.
|
133
|
+
# ======================================================================= #
|
134
|
+
lines = lines_for(this_file)
|
135
|
+
# ======================================================================= #
|
136
|
+
# Next tap into comment_describing(). This resides in code_helpers.rb
|
137
|
+
# ======================================================================= #
|
138
|
+
comment_describing(lines, line_number)
|
124
139
|
end
|
125
140
|
|
126
141
|
end
|
@@ -16,8 +16,8 @@ module MethodIntrospection
|
|
16
16
|
# ===================================================================== #
|
17
17
|
# === source_location
|
18
18
|
#
|
19
|
-
# Return the source location for a Proc (in implementations
|
20
|
-
#
|
19
|
+
# Return the source location for a Proc (in implementations without
|
20
|
+
# Proc#source_location)
|
21
21
|
#
|
22
22
|
# @return [Array] A two element array. First element is the
|
23
23
|
# file, second element is the line in the file where the
|
data/lib/method_introspection.rb
CHANGED
@@ -4,9 +4,13 @@ module MethodIntrospection
|
|
4
4
|
|
5
5
|
extend MethodIntrospection::CodeHelpers
|
6
6
|
|
7
|
+
# ========================================================================= #
|
7
8
|
# This module is to be included by `Method` and `UnboundMethod` and
|
8
9
|
# provides the `#source` functionality
|
10
|
+
# ========================================================================= #
|
9
11
|
module MethodExtensions
|
12
|
+
|
13
|
+
# ======================================================================= #
|
10
14
|
# === source
|
11
15
|
#
|
12
16
|
# The method source() will return the sourcecode for the method as
|
@@ -22,10 +26,14 @@ module MethodIntrospection
|
|
22
26
|
# @hash.clear
|
23
27
|
# self
|
24
28
|
# end
|
29
|
+
# ======================================================================= #
|
25
30
|
def source
|
26
|
-
MethodIntrospection.source_helper(
|
27
|
-
|
31
|
+
MethodIntrospection.source_helper(
|
32
|
+
source_location, defined?(name) ? name : inspect
|
33
|
+
)
|
34
|
+
end; alias source? source
|
28
35
|
|
36
|
+
# ======================================================================= #
|
29
37
|
# === comment
|
30
38
|
#
|
31
39
|
# Return the comments associated with the method as a string.
|
@@ -36,6 +44,7 @@ module MethodIntrospection
|
|
36
44
|
# Set.instance_method(:clear).comment.display
|
37
45
|
# =>
|
38
46
|
# # Removes all elements and returns self.
|
47
|
+
# ======================================================================= #
|
39
48
|
def comment
|
40
49
|
MethodIntrospection.comment_helper(source_location, defined?(name) ? name : inspect)
|
41
50
|
end
|
data/test/test.rb
CHANGED
@@ -1,11 +1,8 @@
|
|
1
|
-
direc = File.expand_path(File.dirname(__FILE__))
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
1
|
require 'bacon'
|
5
|
-
require 'method_introspection
|
6
|
-
|
2
|
+
require 'method_introspection'
|
3
|
+
require_relative './test_helper.rb'
|
7
4
|
|
8
|
-
describe MethodIntrospection
|
5
|
+
describe MethodIntrospection {
|
9
6
|
|
10
7
|
describe "source_location (testing 1.8 implementation)" do
|
11
8
|
it 'should return correct source_location for a method' do
|
@@ -135,4 +132,4 @@ describe MethodIntrospection do
|
|
135
132
|
method(:comment_test5).comment.should == @comment5
|
136
133
|
end
|
137
134
|
end
|
138
|
-
|
135
|
+
}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_introspection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robert A. Heiler
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Allows us to inspect the source code for a method and the documentation.
|
14
14
|
email: shevegen@gmail.com
|
@@ -47,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
47
|
version: '0'
|
48
48
|
requirements: []
|
49
49
|
rubyforge_project:
|
50
|
-
rubygems_version: 2.4.
|
50
|
+
rubygems_version: 2.4.8
|
51
51
|
signing_key:
|
52
52
|
specification_version: 4
|
53
53
|
summary: Allows us to inspect the source code for a method and the documentation.
|