where_is 0.3.2 → 0.4.0
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/README.md +13 -0
- data/lib/where_is.rb +8 -58
- data/lib/where_is/extractors.rb +69 -0
- data/lib/where_is/version.rb +1 -1
- data/where_is.gemspec +1 -1
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fafe31e85d2512640fca1b7ee5b1825a98cfab24
|
4
|
+
data.tar.gz: a065ccec36e61485627f51e83d04cac52974f472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fcb11e5fd41273ff08c25e7b93d8ff40cbad9e1070f9bb313a2868913660809aeaddfc7ff7ca609897a5d06798f7a4b0404017bf6bb62a002f8a1d920b9f1bfb
|
7
|
+
data.tar.gz: b5fa1f0e3d1fe01eda1bb100445ea4b7ae1c46b8529c84d0474e9e33444662767214b6547b4738014458af02c47516adf80b746b3764f176825ae49c527eae38
|
data/README.md
CHANGED
@@ -50,6 +50,19 @@ Where.are(IRB)
|
|
50
50
|
# ]
|
51
51
|
|
52
52
|
|
53
|
+
# Where.ignore sets a global array of ignored regexes or partial strings.
|
54
|
+
# Any item with a `file` key that matches the ignore will be omitted.
|
55
|
+
Where.ignore = [/inspector/]
|
56
|
+
# => [/inspector/]
|
57
|
+
|
58
|
+
Where.are(IRB)
|
59
|
+
# => [
|
60
|
+
# {:file=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/workspace.rb", :line=>112, :path=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/workspace.rb:112"},
|
61
|
+
# {:file=>"(eval)", :line=>2, :path=>"(eval):2"},
|
62
|
+
# {:file=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb.rb", :line=>350, :path=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb.rb:350"},
|
63
|
+
# {:file=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/init.rb", :line=>16, :path=>"/Users/david/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/irb/init.rb:16"}
|
64
|
+
# ]
|
65
|
+
|
53
66
|
Where.is(String, :unknown_method)
|
54
67
|
# NameError: undefined method `unknown_method' for class `#<Class:String>'
|
55
68
|
```
|
data/lib/where_is.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
require 'where_is/extractors'
|
1
2
|
require 'where_is/version'
|
2
3
|
|
3
4
|
module Where
|
4
5
|
class << self
|
6
|
+
include Where::Extractors
|
7
|
+
|
5
8
|
def is(klass, method = nil)
|
6
9
|
are(klass, method)[0]
|
7
10
|
end
|
@@ -30,6 +33,10 @@ module Where
|
|
30
33
|
are_instance_methods(klass, method_name)[0]
|
31
34
|
end
|
32
35
|
|
36
|
+
def is_class_primarily(klass)
|
37
|
+
is_class(klass)[0]
|
38
|
+
end
|
39
|
+
|
33
40
|
def are_methods(klass, method_name)
|
34
41
|
ensured_class = ensure_class(klass)
|
35
42
|
methods = are_via_extractor(:method, ensured_class, method_name)
|
@@ -54,10 +61,6 @@ module Where
|
|
54
61
|
source_locations
|
55
62
|
end
|
56
63
|
|
57
|
-
def is_class_primarily(klass)
|
58
|
-
is_class(klass)[0]
|
59
|
-
end
|
60
|
-
|
61
64
|
def is_class(klass)
|
62
65
|
ensured_class = ensure_class(klass)
|
63
66
|
methods = defined_methods(ensured_class)
|
@@ -66,63 +69,10 @@ module Where
|
|
66
69
|
if source_locations.empty?
|
67
70
|
raise NameError, "#{ensured_class} has no methods" if methods.empty?
|
68
71
|
raise NameError, "#{ensured_class} only has built-in methods " \
|
69
|
-
|
72
|
+
"(#{methods.size} in total)"
|
70
73
|
end
|
71
74
|
|
72
75
|
source_locations
|
73
76
|
end
|
74
|
-
|
75
|
-
private
|
76
|
-
|
77
|
-
def ensure_class(klass)
|
78
|
-
[Class, Module].include?(klass.class) ? klass : klass.class
|
79
|
-
end
|
80
|
-
|
81
|
-
def source_location(method)
|
82
|
-
source_location = method.source_location
|
83
|
-
source_location = [method.to_s[/: (.*)>/, 1]] if source_location.nil?
|
84
|
-
|
85
|
-
# source_location is a 2 element array
|
86
|
-
# [filename, line_number]
|
87
|
-
# some terminals (eg. iterm) will jump to the file if you cmd+click it
|
88
|
-
# but they can jump to the specific line if you concat file & line number
|
89
|
-
filename, line = source_location
|
90
|
-
build_location_hash(filename, line)
|
91
|
-
end
|
92
|
-
|
93
|
-
def group_and_combine_source_locations(source_locations)
|
94
|
-
file_groups = source_locations.group_by { |src_loc| src_loc[:file] }.to_a
|
95
|
-
|
96
|
-
file_groups.map! do |file, src_locs|
|
97
|
-
lines = src_locs.map { |sl| sl[:line] }
|
98
|
-
count = lines.size
|
99
|
-
line = lines.min
|
100
|
-
{ count: count, data: build_location_hash(file, line) }
|
101
|
-
end
|
102
|
-
|
103
|
-
file_groups.sort_by { |fc| fc[:count] }.map { |fc| fc[:data] }
|
104
|
-
end
|
105
|
-
|
106
|
-
def are_via_extractor(extractor, klass, method_name)
|
107
|
-
klass.ancestors.map do |ancestor|
|
108
|
-
begin
|
109
|
-
source_location(ancestor.send(extractor, method_name))
|
110
|
-
rescue NameError
|
111
|
-
nil
|
112
|
-
end
|
113
|
-
end.compact
|
114
|
-
end
|
115
|
-
|
116
|
-
def defined_methods(klass)
|
117
|
-
methods = klass.methods(false).map { |m| klass.method(m) }
|
118
|
-
methods += klass.instance_methods(false)
|
119
|
-
.map { |m| klass.instance_method(m) }
|
120
|
-
source_locations = methods.map(&:source_location).compact
|
121
|
-
source_locations.map { |(file, line)| build_location_hash(file, line) }
|
122
|
-
end
|
123
|
-
|
124
|
-
def build_location_hash(file, line)
|
125
|
-
{ file: file, line: line, path: [file, line].compact.join(':') }
|
126
|
-
end
|
127
77
|
end
|
128
78
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Where
|
2
|
+
module Extractors
|
3
|
+
attr_accessor :ignore
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def ensure_class(klass)
|
8
|
+
[Class, Module].include?(klass.class) ? klass : klass.class
|
9
|
+
end
|
10
|
+
|
11
|
+
def source_location(method)
|
12
|
+
source_location = method.source_location
|
13
|
+
source_location = [method.to_s[/: (.*)>/, 1]] if source_location.nil?
|
14
|
+
|
15
|
+
# source_location is a 2 element array
|
16
|
+
# [filename, line_number]
|
17
|
+
# some terminals (eg. iterm) will jump to the file if you cmd+click it
|
18
|
+
# but they can jump to the specific line if you concat file & line number
|
19
|
+
filename, line = source_location
|
20
|
+
build_location_hash(filename, line)
|
21
|
+
end
|
22
|
+
|
23
|
+
def group_and_combine_source_locations(source_locations)
|
24
|
+
file_groups = source_locations.group_by { |src_loc| src_loc[:file] }.to_a
|
25
|
+
|
26
|
+
file_groups.map! do |file, src_locs|
|
27
|
+
lines = src_locs.map { |sl| sl[:line] }
|
28
|
+
count = lines.size
|
29
|
+
line = lines.min
|
30
|
+
{ count: count, data: build_location_hash(file, line) }
|
31
|
+
end
|
32
|
+
|
33
|
+
locations = file_groups.sort_by { |fc| fc[:count] }.map { |fc| fc[:data] }
|
34
|
+
process_ignores(locations)
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_ignores(locations)
|
38
|
+
[@ignore].flatten.compact.each do |ign|
|
39
|
+
locations.reject! do |location|
|
40
|
+
location[:file].match(ign)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
locations
|
45
|
+
end
|
46
|
+
|
47
|
+
def are_via_extractor(extractor, klass, method_name)
|
48
|
+
klass.ancestors.map do |ancestor|
|
49
|
+
begin
|
50
|
+
source_location(ancestor.send(extractor, method_name))
|
51
|
+
rescue NameError
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
end.compact
|
55
|
+
end
|
56
|
+
|
57
|
+
def defined_methods(klass)
|
58
|
+
methods = klass.methods(false).map { |m| klass.method(m) }
|
59
|
+
methods += klass.instance_methods(false)
|
60
|
+
.map { |m| klass.instance_method(m) }
|
61
|
+
source_locations = methods.map(&:source_location).compact
|
62
|
+
source_locations.map { |(file, line)| build_location_hash(file, line) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def build_location_hash(file, line)
|
66
|
+
{ file: file, line: line, path: [file, line].compact.join(':') }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/where_is/version.rb
CHANGED
data/where_is.gemspec
CHANGED
@@ -24,5 +24,5 @@ Gem::Specification.new do |spec|
|
|
24
24
|
spec.add_development_dependency 'bundler', '~> 1.13'
|
25
25
|
spec.add_development_dependency 'rake', '~> 10.0'
|
26
26
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
27
|
-
spec.add_development_dependency 'rubocop', '~> 0.
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.57.1'
|
28
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: where_is
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Allie
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 0.
|
61
|
+
version: 0.57.1
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 0.
|
68
|
+
version: 0.57.1
|
69
69
|
description: A little Ruby gem for finding the source location where class and methods
|
70
70
|
are defined.
|
71
71
|
email:
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- LICENSE.txt
|
80
80
|
- README.md
|
81
81
|
- lib/where_is.rb
|
82
|
+
- lib/where_is/extractors.rb
|
82
83
|
- lib/where_is/version.rb
|
83
84
|
- where_is.gemspec
|
84
85
|
homepage: https://github.com/daveallie/where_is
|