method_locator 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/method_locator/version.rb +1 -1
- data/lib/method_locator.rb +16 -8
- data/spec/method_locator/method_locator_spec.rb +11 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Method Locator is a Ruby gem that allows you to easily determine the method look
|
|
4
4
|
object / class / module, as well as find all places (represented as UnboundMethod instances) where
|
5
5
|
a callable method is defined on an object. This is very useful in an environment such as Rails where
|
6
6
|
you are unsure where a method may be defined or overridden. Note that by default, Ruby tends to hide
|
7
|
-
singleton classes when you invoke
|
7
|
+
singleton classes when you invoke Module#ancestors. The new Object#method_lookup_path does not hide
|
8
8
|
these singleton classes and will return them, so that you get a true representation of how Ruby
|
9
9
|
performs lookups for methods.
|
10
10
|
|
data/lib/method_locator.rb
CHANGED
@@ -13,27 +13,35 @@ module MethodLocator
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def method_lookup_path
|
16
|
-
is_a?(Class) ? class_lookup_path : nonclass_lookup_path
|
16
|
+
lookup_path = is_a?(Class) ? class_lookup_path : nonclass_lookup_path
|
17
|
+
insert_modules_into(lookup_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
def class_only_ancestors
|
21
|
+
ancestors.grep(Class)
|
17
22
|
end
|
18
23
|
|
19
24
|
private
|
20
25
|
|
26
|
+
def instance_methods_for(clazz)
|
27
|
+
clazz.instance_methods(false) + clazz.private_instance_methods(false)
|
28
|
+
end
|
29
|
+
|
21
30
|
def nonclass_lookup_path
|
22
|
-
self.class.
|
31
|
+
self.class.class_only_ancestors.unshift(singleton_class)
|
23
32
|
end
|
24
33
|
|
25
34
|
def class_lookup_path
|
26
|
-
|
35
|
+
class_only_ancestors.map(&:singleton_class) + Class.class_only_ancestors
|
36
|
+
end
|
37
|
+
|
38
|
+
def insert_modules_into(lookup_path)
|
27
39
|
# reverse is used here since included_modules contains all modules defined in
|
28
40
|
# the current class as well as in its ancestors.
|
29
41
|
lookup_path.reverse.map do |clazz|
|
30
|
-
[clazz.included_modules, clazz]
|
42
|
+
[clazz.included_modules.reverse, clazz]
|
31
43
|
end.flatten.uniq.reverse
|
32
44
|
end
|
33
|
-
|
34
|
-
def instance_methods_for(clazz)
|
35
|
-
clazz.instance_methods(false) + clazz.private_instance_methods(false)
|
36
|
-
end
|
37
45
|
end
|
38
46
|
|
39
47
|
Object.send(:include, MethodLocator)
|
@@ -6,11 +6,13 @@ describe MethodLocator do
|
|
6
6
|
describe "#methods_for" do
|
7
7
|
it "returns proper methods for regular object instances" do
|
8
8
|
instance = B.new
|
9
|
+
instance.extend(M5)
|
9
10
|
|
10
11
|
def instance.foo
|
11
12
|
end
|
12
13
|
|
13
14
|
instance.methods_for(:foo).map(&:owner).should == [instance.singleton_class,
|
15
|
+
M5,
|
14
16
|
B,
|
15
17
|
M2,
|
16
18
|
M1,
|
@@ -29,7 +31,10 @@ describe MethodLocator do
|
|
29
31
|
describe "#method_lookup_path" do
|
30
32
|
it "returns proper path for regular object instances" do
|
31
33
|
instance = B.new
|
34
|
+
instance.extend(M5)
|
35
|
+
|
32
36
|
method_lookup_path_for(instance).should == [instance.singleton_class,
|
37
|
+
M5,
|
33
38
|
B,
|
34
39
|
M2,
|
35
40
|
M1,
|
@@ -65,7 +70,7 @@ describe MethodLocator do
|
|
65
70
|
end
|
66
71
|
|
67
72
|
def method_lookup_path_for(obj)
|
68
|
-
allowed_items = [obj, M1, M2, M3, M4, A, B, Class, Module, Object, MethodLocator, Kernel, BasicObject]
|
73
|
+
allowed_items = [obj, M1, M2, M3, M4, M5, A, B, Class, Module, Object, MethodLocator, Kernel, BasicObject]
|
69
74
|
allowed_items = allowed_items + allowed_items.map(&:singleton_class)
|
70
75
|
obj.method_lookup_path.keep_if { |c| allowed_items.include?(c) }
|
71
76
|
end
|
@@ -92,6 +97,11 @@ module M4
|
|
92
97
|
end
|
93
98
|
end
|
94
99
|
|
100
|
+
module M5
|
101
|
+
def foo
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
95
105
|
class A
|
96
106
|
def foo
|
97
107
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_locator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2011-09-21 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70185155876540 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: 2.5.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70185155876540
|
25
25
|
description: Method Locator provides a way to traverse an object's method lookup path
|
26
26
|
to find all places where a method may be defined.
|
27
27
|
email:
|