show_code 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 917027002bceb30270e19f1419fc1cb1e0d58f42
4
- data.tar.gz: a6eef9368dc7ee9b58c571386f25fa615d857fa8
3
+ metadata.gz: 860381c352a3583f9a08a13a459eedfe7484675a
4
+ data.tar.gz: 12a2a63183c690607678a83c01782591cca92d23
5
5
  SHA512:
6
- metadata.gz: 3b2d93838f1183ef011ce36cfe6c19d8aac52cf332ef0333b365f2d3202e017a9721875d33dbc032092e5ff0a7804a902f9113ef13b758058ebafe652254136f
7
- data.tar.gz: cdfa8bde87f2dce7c2cbf6547054f3188f0787e96d83bc3190794251f46f86301661c1fa303a1babeeb5d064399e01a8c8277db6b2a8f3998eda5215ed502b4f
6
+ metadata.gz: a32516a8ce21c77cbf98b6d9854f4f3afb44e347b1737325ba382e781358803ec5154cf0290009319e0475f570672f92a1e56a526571258d7f192bdeacc72870
7
+ data.tar.gz: 0515308a0ea0809e4c329926f6d8654495430affac86a6419ad6ce7000e24167465e9c5ee3e2ea52b01255faaeef578b126ab8b96801730ae6a0efdcf3cf484f
@@ -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 #{loc}"
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 = arr[0..-2].join('.')
11
- method = arr[-1]
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, 'cannot find the method source location'
27
+ raise SourceLocationError, 'Could not find the method source location'
27
28
  else
28
- @file, @line = @method.source_location
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
@@ -1,3 +1,3 @@
1
1
  module ShowCode
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: show_code
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - cenxky