03 Dec, 2008

Published at 04:42PM

Tagged with programming, proxy, and ruby

This post has 0 comments

A proxy through Roxy

Roxy is a pretty sweet library. It allows you to easily define and use proxies for any Ruby class.

Although, I do have one minor gripe. I think yielding the proxy_owner and proxy_target variables to the block (rather than them just being automatically available) would be better. It’d leave more room for readability, in my opinion.

For example, instead of:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Person
  include Roxy::Moxie

  attr_accessor :parents

  proxy :parents do
    def divorced?
      # the 'proxy_owner' variable refers to the person 
      # instance and the 'proxy_target' variable refers 
      # to the parents
    end
  end
end

You’d be able to do:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Person
  include Roxy::Moxie

  attr_accessor :parents

  proxy :parents do |person, parents|
    def divorced?
      # now it becomes immediately obvious that the
      # 'person' variable refers to the person instance,
      # and the 'parents' variable refers to the parents
    end
  end
end

I know, I know. “Just fork it and make the change, stupid”. And you’re absolutely right. Unfortunately, I just don’t have an immediate need for this or I might.

I can’t wait until I do need this, though. What a great way to make good code even better.

And here’s the repo on github if you didn’t already pick up on that.

Comments

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch