method_locator 0.0.3 → 0.0.4
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.
- data/README.md +8 -3
- data/lib/method_locator.rb +6 -6
- data/lib/method_locator/version.rb +1 -1
- data/method_locator.gemspec +2 -2
- data/spec/method_locator/method_locator_spec.rb +8 -0
- metadata +6 -6
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# method_locator
|
2
2
|
|
3
|
-
|
3
|
+
method_locator is a Ruby gem that allows you to easily determine the method lookup path of a particular
|
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
|
@@ -8,15 +8,20 @@ singleton classes when you invoke Module#ancestors. The new Object#method_lookup
|
|
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
|
|
11
|
+
This library will make more sense if you understand Ruby's object model and method lookup path.
|
12
|
+
A great explanation of this can be found in the following article by Andrea Singh: [Ruby's Eigenclasses Demystified](http://blog.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html)
|
13
|
+
|
11
14
|
## Installation
|
12
15
|
|
13
|
-
|
16
|
+
method_locator is available as a Ruby gem:
|
14
17
|
|
15
18
|
gem install method_locator
|
16
19
|
|
17
20
|
## Examples
|
18
21
|
|
19
22
|
```ruby
|
23
|
+
require 'method_locator'
|
24
|
+
|
20
25
|
module M1
|
21
26
|
def foo
|
22
27
|
puts "foo from M1"
|
data/lib/method_locator.rb
CHANGED
@@ -2,9 +2,7 @@ require "method_locator/version"
|
|
2
2
|
|
3
3
|
# MethodLocator finds all method definitions in an object instance, class, or
|
4
4
|
# module ancestor chain. This code will make more sense if you understand Ruby's
|
5
|
-
# object model and method lookup path.
|
6
|
-
# "Ruby's Eigenclasses Demystified" - http://blog.madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html
|
7
|
-
|
5
|
+
# object model and method lookup path. See README for more information.
|
8
6
|
module MethodLocator
|
9
7
|
def methods_for(meth)
|
10
8
|
method_lookup_path.each_with_object([]) do |clazz, matches|
|
@@ -17,7 +15,7 @@ module MethodLocator
|
|
17
15
|
insert_modules_into(lookup_path)
|
18
16
|
end
|
19
17
|
|
20
|
-
def
|
18
|
+
def only_class_ancestors
|
21
19
|
ancestors.grep(Class)
|
22
20
|
end
|
23
21
|
|
@@ -28,11 +26,13 @@ module MethodLocator
|
|
28
26
|
end
|
29
27
|
|
30
28
|
def nonclass_lookup_path
|
31
|
-
|
29
|
+
# not only non-classes have singleton classes, for example integers
|
30
|
+
sclass = singleton_class rescue nil
|
31
|
+
self.class.only_class_ancestors.unshift(sclass).compact
|
32
32
|
end
|
33
33
|
|
34
34
|
def class_lookup_path
|
35
|
-
|
35
|
+
only_class_ancestors.map(&:singleton_class) + Class.only_class_ancestors
|
36
36
|
end
|
37
37
|
|
38
38
|
def insert_modules_into(lookup_path)
|
data/method_locator.gemspec
CHANGED
@@ -9,8 +9,8 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = ["Ryan LeCompte"]
|
10
10
|
s.email = ["lecompte@gmail.com"]
|
11
11
|
s.homepage = "http://github.com/ryanlecompte/method_locator"
|
12
|
-
s.summary = %q{
|
13
|
-
s.description = %q{
|
12
|
+
s.summary = %q{method_locator provides a way to traverse an object's method lookup path to find all places where a method may be defined.}
|
13
|
+
s.description = %q{method_locator provides a way to traverse an object's method lookup path to find all places where a method may be defined.}
|
14
14
|
|
15
15
|
s.rubyforge_project = "method_locator"
|
16
16
|
|
@@ -26,6 +26,10 @@ describe MethodLocator do
|
|
26
26
|
it "returns proper methods for modules" do
|
27
27
|
M4.methods_for(:hello).map(&:owner).should == [M4.singleton_class]
|
28
28
|
end
|
29
|
+
|
30
|
+
it "returns proper methods for objects without a singleton class" do
|
31
|
+
5.methods_for(:+).should == [Fixnum.instance_method(:+)]
|
32
|
+
end
|
29
33
|
end
|
30
34
|
|
31
35
|
describe "#method_lookup_path" do
|
@@ -67,6 +71,10 @@ describe MethodLocator do
|
|
67
71
|
Kernel,
|
68
72
|
BasicObject]
|
69
73
|
end
|
74
|
+
|
75
|
+
it "returns proper path for objects without a singleton class" do
|
76
|
+
5.method_lookup_path.should == 5.class.ancestors
|
77
|
+
end
|
70
78
|
end
|
71
79
|
|
72
80
|
def method_lookup_path_for(obj)
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
12
|
+
date: 2011-10-13 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70150932623800 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,8 +21,8 @@ dependencies:
|
|
21
21
|
version: 2.5.0
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
25
|
-
description:
|
24
|
+
version_requirements: *70150932623800
|
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:
|
28
28
|
- lecompte@gmail.com
|
@@ -63,7 +63,7 @@ rubyforge_project: method_locator
|
|
63
63
|
rubygems_version: 1.8.10
|
64
64
|
signing_key:
|
65
65
|
specification_version: 3
|
66
|
-
summary:
|
66
|
+
summary: method_locator provides a way to traverse an object's method lookup path
|
67
67
|
to find all places where a method may be defined.
|
68
68
|
test_files:
|
69
69
|
- spec/method_locator/method_locator_spec.rb
|