Ruby Fu

Secrets of a Ruby Ninja for your edification and amusement.

Tuesday, May 15, 2007

 

Transparent Proxies, Part 1

This is an old one, but I tightened up the code a little bit. Take a look at the following:
module Enumerable
def all
MapProxy.new self
end
end
class Enumerable::MapProxy
instance_methods.each do |m|
undef_method m unless m.to_s=~/^__(id|send)__$/
end
private
def initialize proxied
@proxied=proxied
end
def method_missing m,*a,&b
@proxied.map{|o|o.__send__ m, *a, &b} # thanks, NiKA
end
end

Basically it lets you write [1,2,3].all.to_s and get back ["1","2","3"]. It does so by creating an instance of the Enumerable::MapProxy class that dispatches any method you send it right on down to each element of the Enumerable object (an Array in the case of [1,2,3]). It's called MapProxy because it dispatches the method to each element by way of the #map method.

Its pretty convenient for some kinds of operations, but there's a downside to using Transparent Proxies-- I call them "Transparent" because as far as you can tell when you interact with it, it doesn't actually identify itself in the traditional way. If you say [1,2,3].all.class you don't get back a class, you get an Array of classes, which are the classes of the elements in the collection.

The downside therefore is that you get this object with extremely interesting behaviour, but don't really know what's going on unless you can figure out where the object came from.

If you use Rails, you already use Transparent Proxies, since the AssociationProxy class in the ActiveRecord::Associations module is a well-known example of one. In many ways the effect is seamless and great, but it can be the cause of some pretty bizarre behaviour, especially in light of the way it chooses to direct method_missing calls back down to the class the association represents.

For example, did you know that if you have a belongs_to :customer association on your Order class, that @order.belongs_to actually returns an instance of the AssociationProxy class?

If you are ever stuck with an object that you "think" might be an instance of a specific Transparent Proxy class, here is a handy way to find out:
class Class
def one_of_me? object
ObjectSpace.each_object(self) do |o|
return true if o.__id__==object.__id__
end
return false
end
end

# to demonstrate...

Enumerable::MapProxy.
one_of_me?([1,2,3].all) #<= true

ActiveRecord::Associations::AssociationProxy.
one_of_me?(@order.customer) #<= true

Labels: , , ,


posted by Brendan Baldwin  # 3:32 PM  
Comments:
Cool stuff, ruby ninja!

You might consider using '__send__', the bulletproof version of 'send', when monkeypatching like that.

This can cope with a member of the Enumerable that happens to implement a 'send' method; it can happen (many a rails model with 'send' column has wrought havoc) Can't be too safe.

Keep all this great stuff coming!
 
Thats a really good point actually. I also wish that Ruby had a built-in method for doing #send that would emulate/obey the public/protected/private status declarations so that the Proxy would not be exposing the private methods.

I'll update the post to use __send__ though; I like it.
 
Post a Comment



Links to this post:

Create a Link



<< Home

Archives

May 2007   June 2007  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]