Reflecting on an Association Extension

Hi all.

How can I reflect on a method which had been extended into an
association.

If i try model.association.method(:method_name) I get an undefined
error yet I can invoke it via model.association.method_name. I’ve
tried several different approaches to get at the method but can’t seem
to be able to get hold of it.

Thanks
Garrett

On 22 Oct 2007, at 15:37, garrett wrote:

class Post
has_many :comments do
def some_method
end
end
end

The basic issues is that post.comments is not an array, it’s an
instance of a proxy class that will (when needed) load the collection
and forward methods.
so when you call post.comments.method(:some_method) that’s being
forwarded on to the array itself.

You can use post.comments.proxy_respond_to?(:foo) to find if the
proxy itself response to a method, but the ‘method’ method has been
undef’d on association proxies.

Fred.

Thanks Fred

Is there no way then for me to check that two method names actually
have the same implementation.

I had been intending the test that

class Post
has_many :comments do
def foo
end
alias_method :bar, :foo
end
end

post.comments.method(:foo) == post.comments.method(:bar)

On Oct 22, 4:30 pm, Frederick C. [email protected]

garrett wrote:

Thanks Fred

Is there no way then for me to check that two method names actually
have the same implementation.

I had been intending the test that

class Post
has_many :comments do
def foo
end
alias_method :bar, :foo
end
end

post.comments.method(:foo) == post.comments.method(:bar)

The #method() method in Ruby isn’t very helpful for comparing
implementations:

“foo”.method(:length) == “foo”.method(:length)
=> false

If you need to check to see if an extension has been included in an
association, you could grope through the ancestor modules. Or it may be
easier to define a duck-type predicate in the extension module, like
#is_my_fancy_type?, that you can use to check if the association has
that type.


Josh S.
http://blog.hasmanythrough.com

Josh S. wrote:

garrett wrote:

Thanks Fred

Is there no way then for me to check that two method names actually
have the same implementation.

I had been intending the test that

class Post
has_many :comments do
def foo
end
alias_method :bar, :foo
end
end

post.comments.method(:foo) == post.comments.method(:bar)

The #method() method in Ruby isn’t very helpful for comparing
implementations:

“foo”.method(:length) == “foo”.method(:length)
=> false

If you need to check to see if an extension has been included in an
association, you could grope through the ancestor modules. Or it may be
easier to define a duck-type predicate in the extension module, like
#is_my_fancy_type?, that you can use to check if the association has
that type.

Ah, oops. Ruby’s per-object methods get me again. Method identity is
unique per instance. The two “foo” strings in my other example are of
course different objects. Sigh. Symbols are the same instance though,
therefore:

:foo.method(:to_s) == :foo.method(:to_s)
=> true

So if you have the exact same association instance, you could actually
compare implementations of messages to see if they were the same method.
I still don’t get the use case though.


Josh S.
http://blog.hasmanythrough.com