method_introspection 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
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/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 +1 -0
- 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: 71f0b7babf80da00df5632f3891dc8c1fbda6808
|
4
|
+
data.tar.gz: eefb7cdfe55129089a22248a683b47dfce63c5cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 766763a24f639427cac36d785bf73789a198f98e6f4bcfa123a40851093106050a9644eb2ae7272492b2107bfb218886f519d7b9233a2705ccabe61b2d5c9ca9
|
7
|
+
data.tar.gz: 97c5f41a44df2d9aafbe2c93fd8072839c2b36ce98080f4694e69abcc65a5b2e8ac9a70ebb265835d1d02039ed850f46d4cbb1101acba1ebb7fd78a86a7ad7f8
|
@@ -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
|
@@ -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
@@ -26,6 +26,7 @@ module MethodIntrospection
|
|
26
26
|
MethodIntrospection.source_helper(source_location, defined?(name) ? name : inspect)
|
27
27
|
end
|
28
28
|
|
29
|
+
# ======================================================================= #
|
29
30
|
# === comment
|
30
31
|
#
|
31
32
|
# Return the comments associated with the method as a string.
|
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.5
|
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-08-
|
11
|
+
date: 2015-08-31 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.
|