<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Binary Logic &#187; proxy</title>
	<atom:link href="http://www.binarylogic.com/tag/proxy/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.binarylogic.com</link>
	<description>Ben Johnson's thoughts and programming techniques</description>
	<lastBuildDate>Sat, 23 Jan 2010 21:19:58 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to create a proxy class in ruby</title>
		<link>http://www.binarylogic.com/2009/08/07/how-to-create-a-proxy-class-in-ruby/</link>
		<comments>http://www.binarylogic.com/2009/08/07/how-to-create-a-proxy-class-in-ruby/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 23:53:31 +0000</pubDate>
		<dc:creator>benjohnson</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[proxy]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.binarylogic.com/?p=775</guid>
		<description><![CDATA[Creating a proxy class in ruby can be a nice tool to have in your nerdy tool belt, and its actually really simple to do. Have you ever wondered how ActiveRecord is able to pull off its nifty tricks with associations? Ex:

user = User.first
user.orders.build
# => #Order object

user.orders.first
# => #Order ojbect

user.orders.class
# => Array

Wait, &#8216;build&#8217; isn&#8217;t a [...]]]></description>
			<content:encoded><![CDATA[<p>Creating a proxy class in ruby can be a nice tool to have in your nerdy tool belt, and its actually really simple to do. Have you ever wondered how ActiveRecord is able to pull off its nifty tricks with associations? Ex:</p>
<pre class="ruby">
user = User.first
user.orders.build
# => #Order object

user.orders.first
# => #Order ojbect

user.orders.class
# => Array
</pre>
<p>Wait, &#8216;build&#8217; isn&#8217;t a method available for arrays. How did they do that?<span id="more-775"></span></p>
<p>One way is to add a &#8216;build&#8217; method into the Array class, but this is really intrusive. You don&#8217;t necessarily need a build method for every Array object, more importantly build needs to know what kind of object to build. How does it get this information?</p>
<p>The answer to the mystery is a proxy class that delegates method calls to the underlying array. It&#8217;s actually really simple to do. Check it out:</p>
<pre>
class Proxy
    instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ }

    def my_awesome_method
       "you just called an awesome method!"
    end

    protected
        def method_missing(name, *args, &#038;block)
          target.send(name, *args, &#038;block)
        end

        def target
          @target ||= []
        end
end
</pre>
<p>Notice that the first thing we do is undefine just about every method in the class. This makes sure that calls to those methods hit method_missing and ultimately get called on the underlying array. Afterwards we define any special methods, such as &#8216;my_awesome_method&#8217;, and then we handle any calls to undefined methods. That&#8217;s it! It&#8217;s actually really simple and clever. Here&#8217;s how it works:</p>
<pre>
proxy = Proxy.new
proxy.my_awesome_method
# => "you just called an awesome method!"

proxy.class
# => Array
</pre>
<h3>Why would you want to do this?</h3>
<p>Well sometimes you want to add a little extra functionality to a basic object, such as an Array, Hash, etc. This allows you to do that without having to modify the underlying class, which is good. You basically get to play middle man and intercept any calls to that object. Then you have the decision to pass them through or do your own thing. This also promotes much more &#8220;ruby like&#8221; syntax. Without this, calling associations in AR would look something like:</p>
<pre>
user.orders.build
user.orders.create
user.orders.all.first
user.orders.all.size
</pre>
<p>Anything related to the underlying Array must be called off of the &#8220;all&#8221; method. That just seems dirty. This is so much cleaner:</p>
<pre>
user.orders.build
user.orders.create
user.orders.first
user.orders.size
</pre>
<p>Pretty cool huh?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.binarylogic.com/2009/08/07/how-to-create-a-proxy-class-in-ruby/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

