Thursday, June 25, 2009

More on Proxy in Ruby

Last week I was impressed how easy it is to create a proxy in Ruby. After some thinking I realized that it is not as good as it looks (at least in some situations).
First of all we don't know what methods are available because reflection does not work as we expect. The quick and dirty solution would be to simulate expected behaviour by redefining SimpleProxy's /((private|public|protected)_)?methods/ and 'respond_to?' methods so it exposes the same interface as the class we delegate calls to.
But does it? What about SimpleProxy.instance_methods? Before we instantiate this class we don't know anything about methods we should be delegating. Obviously one could have created specific class for each class one wants to proxy but this way we are loosing flexibility of our "transparent proxy" object.
There is another disadvantage of using such a proxy - tools won't recognize methods as belonging to the proxy class so you have to have documentation for an object you are hiding behind it on hand.

No comments:

Post a Comment