show_code 0.1.6 → 0.1.7
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.
- checksums.yaml +4 -4
- data/lib/show_code/code.rb +12 -2
- data/lib/show_code/source_location.rb +23 -4
- data/lib/show_code/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 860381c352a3583f9a08a13a459eedfe7484675a
|
4
|
+
data.tar.gz: 12a2a63183c690607678a83c01782591cca92d23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a32516a8ce21c77cbf98b6d9854f4f3afb44e347b1737325ba382e781358803ec5154cf0290009319e0475f570672f92a1e56a526571258d7f192bdeacc72870
|
7
|
+
data.tar.gz: 0515308a0ea0809e4c329926f6d8654495430affac86a6419ad6ce7000e24167465e9c5ee3e2ea52b01255faaeef578b126ab8b96801730ae6a0efdcf3cf484f
|
data/lib/show_code/code.rb
CHANGED
@@ -6,15 +6,25 @@ module ShowCode
|
|
6
6
|
|
7
7
|
def initialize(location)
|
8
8
|
@file = location.file
|
9
|
-
@line = location.line
|
10
9
|
file_lines = File.readlines(@file) if @file
|
11
10
|
|
11
|
+
@line = real_start_line file_lines, location.line
|
12
12
|
related_lines = file_lines[(@line - 1)..-1] || []
|
13
|
+
|
13
14
|
codes = extract_method_code related_lines
|
14
15
|
@lines = codes.count
|
15
16
|
@content = codes.join
|
16
17
|
end
|
17
18
|
|
19
|
+
def real_start_line(file_lines, line)
|
20
|
+
return line unless line.zero?
|
21
|
+
|
22
|
+
file_lines.each do |loc|
|
23
|
+
line += 1
|
24
|
+
return line if /^\s*(class|module)\s+[A-Z]/ === loc
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
18
28
|
def extract_method_code(related_lines)
|
19
29
|
codes = []
|
20
30
|
related_lines.each do |loc|
|
@@ -49,7 +59,7 @@ module ShowCode
|
|
49
59
|
line_number = line - 1
|
50
60
|
output.split("\n").map do |loc|
|
51
61
|
line_number += 1
|
52
|
-
"\e[33m#{line_number}\e[0m
|
62
|
+
"\e[33m#{line_number.to_s.ljust(4, ' ')}\e[0m#{loc}"
|
53
63
|
end.join("\n")
|
54
64
|
end
|
55
65
|
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module ShowCode
|
2
2
|
class SourceLocationError < StandardError; end
|
3
|
+
class ModuleNotFound < StandardError; end
|
3
4
|
|
4
5
|
class SourceLocation
|
5
6
|
attr :method, :file, :line
|
@@ -7,8 +8,8 @@ module ShowCode
|
|
7
8
|
def initialize(target)
|
8
9
|
if target.is_a?(String)
|
9
10
|
arr = target.gsub('.new.', '.allocate.').split('.')
|
10
|
-
klass
|
11
|
-
method = arr
|
11
|
+
klass, method = arr[0..-2].join('.'), arr[-1]
|
12
|
+
klass, method = based_on_klass_method(target) if arr.size == 1 # if hope view Class/Module source codes
|
12
13
|
|
13
14
|
# refer:
|
14
15
|
# Object.instance_method(:method).bind(User).call(:current).source_location
|
@@ -23,12 +24,30 @@ module ShowCode
|
|
23
24
|
end
|
24
25
|
|
25
26
|
if @method.source_location.nil?
|
26
|
-
raise SourceLocationError, '
|
27
|
+
raise SourceLocationError, 'Could not find the method source location'
|
27
28
|
else
|
28
|
-
@file,
|
29
|
+
@file, line = @method.source_location
|
30
|
+
@line ||= line
|
29
31
|
end
|
30
32
|
|
31
33
|
end
|
32
34
|
|
35
|
+
private
|
36
|
+
|
37
|
+
# find class first singleton method or first instance_method
|
38
|
+
def based_on_klass_method(klass)
|
39
|
+
_class = eval(klass)
|
40
|
+
|
41
|
+
regroup_klass = if !(method = _class.instance_methods(:false)[0]).nil?
|
42
|
+
klass + '.allocate'
|
43
|
+
elsif !(method = _class.methods(:false)[0]).nil?
|
44
|
+
klass
|
45
|
+
else
|
46
|
+
raise ModuleNotFound, "Could not find #{klass}"
|
47
|
+
end
|
48
|
+
|
49
|
+
@line = 0; [regroup_klass, method]
|
50
|
+
end
|
51
|
+
|
33
52
|
end
|
34
53
|
end
|
data/lib/show_code/version.rb
CHANGED