<?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"
	>

<channel>
	<title>Derek DeVries</title>
	<atom:link href="http://derekdevries.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://derekdevries.com</link>
	<description></description>
	<pubDate>Mon, 11 Aug 2008 05:59:18 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5</generator>
	<language>en</language>
			<item>
		<title>Auditing User Experience with Documentation</title>
		<link>http://derekdevries.com/2008/05/07/auditing-user-experience-with-documentation/</link>
		<comments>http://derekdevries.com/2008/05/07/auditing-user-experience-with-documentation/#comments</comments>
		<pubDate>Thu, 08 May 2008 00:16:10 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ux]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=17</guid>
		<description><![CDATA[End-user documentation has always been a thorn for many developers. We'd much rather be writing working software than explaining how that software works. It is especially difficult working in an agile environment where things change so quickly that writing extensive end-user documentation just slows down application development. ]]></description>
			<content:encoded><![CDATA[<p>End-user documentation has always been a thorn for many developers. We&#8217;d much rather be writing working software than explaining how that software works. It is especially difficult working in an agile environment where things change so quickly that writing extensive end-user documentation just slows down application development. </p>
<p>One real benefit of end-user documentation is that it generally will help make your application better. When you&#8217;re explaining in depth how to use your application, it becomes immediately obvious where you&#8217;re application is deficient, confusing, or inconsistent. </p>
<p>Try writing a walk-through of your application. Write down what each element of your application does, and why. If you find areas that are confusing to write down, chances are you&#8217;ll be getting an e-mail from a client who is confused as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2008/05/07/auditing-user-experience-with-documentation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Configuring Sphinx on OS X Leopard</title>
		<link>http://derekdevries.com/2008/04/05/configuring-sphinx-on-os-x-leopard/</link>
		<comments>http://derekdevries.com/2008/04/05/configuring-sphinx-on-os-x-leopard/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 21:55:25 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[sphinx osx]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=14</guid>
		<description><![CDATA[I ran into a few small errors when installing <a href="http://www.sphinxsearch.com/">Sphinx</a> on my Mac Book Pro. I'll share my install instructions along with a couple troubleshooting tips for others that may have the same problems. 

]]></description>
			<content:encoded><![CDATA[<p>I ran into a few small errors when installing <a href="http://www.sphinxsearch.com/">Sphinx</a> on my Mac Book Pro. I&#8217;ll share my install instructions along with a couple troubleshooting tips for others that may have the same problems. </p>
<p>Let&#8217;s start with the instructions for installing the latest version to <code>/usr/local</code>. If you&#8217;re not familiar with what this means, check out Dan&#8217;s article on <a href="http://hivelogic.com/articles/using_usr_local/">Using /usr/local</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">curl -O http://www.sphinxsearch.com/downloads/sphinx<span style="color: #000000;">-0.9</span><span style="color: #000000;">.8</span>-rc2.<span style="color: #c20cb9; font-weight: bold;">tar</span>.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> -xzf sphinx<span style="color: #000000;">-0.9</span><span style="color: #000000;">.8</span>-rc2.<span style="color: #c20cb9; font-weight: bold;">tar</span>.gz
<span style="color: #7a0874; font-weight: bold;">cd</span> sphinx<span style="color: #000000;">-0.9</span><span style="color: #000000;">.8</span>-rc2
./configure --<span style="color: #007800;">prefix=</span>/usr/<span style="color: #7a0874; font-weight: bold;">local</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ../
<span style="color: #c20cb9; font-weight: bold;">rm</span> -fr sphinx<span style="color: #000000;">-0.9</span><span style="color: #000000;">.8</span>-rc2*</pre></div></div>

<h3>Troubleshooting</h3>
<p>If you&#8217;re installing Sphinx on Leopard on the 64 bit architecture you need to run</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #007800;">LDFLAGS=</span><span style="color: #ff0000;">&quot;-arch x86_64&quot;</span> ./configure</pre></div></div>

<p>If <code>make</code> is throwing you an error that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">...
<span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>sphinx.o<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">1</span>
<span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>all<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">2</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>all-recursive<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">1</span></pre></div></div>

<p>You may need to specify the path to mysql using the <inlinecode>&#8211;with-mysql</inlinecode> option to <inlinecode>./configure</inlinecode>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">./configure --with-<span style="color: #007800;">mysql=</span>/usr/<span style="color: #7a0874; font-weight: bold;">local</span>/mysql</pre></div></div>

<p>If <code>make</code> is throwing you an error that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">sphinx.<span style="color: #c20cb9; font-weight: bold;">cpp</span>:<span style="color: #000000;">17343</span>: error: ‘XML_STATUS_ERROR’ was not declared <span style="color: #000000; font-weight: bold;">in</span> this scope
sphinx.<span style="color: #c20cb9; font-weight: bold;">cpp</span>:<span style="color: #000000;">17367</span>: error: ‘XML_STATUS_OK’ was not declared <span style="color: #000000; font-weight: bold;">in</span> this scope
sphinx.<span style="color: #c20cb9; font-weight: bold;">cpp</span>: In member <span style="color: #000000; font-weight: bold;">function</span> ‘bool CSphSource_XMLPipe2::ParseNextChunk<span style="color: #7a0874; font-weight: bold;">&#40;</span>int, CSphString&amp;<span style="color: #7a0874; font-weight: bold;">&#41;</span>’:
sphinx.<span style="color: #c20cb9; font-weight: bold;">cpp</span>:<span style="color: #000000;">17775</span>: error: ‘XML_STATUS_OK’ was not declared <span style="color: #000000; font-weight: bold;">in</span> this scope
<span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">2</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>sphinx.o<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">1</span>
<span style="color: #c20cb9; font-weight: bold;">make</span><span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #000000;">1</span><span style="color: #7a0874; font-weight: bold;">&#93;</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>all<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">2</span>
<span style="color: #c20cb9; font-weight: bold;">make</span>: *** <span style="color: #7a0874; font-weight: bold;">&#91;</span>all-recursive<span style="color: #7a0874; font-weight: bold;">&#93;</span> Error <span style="color: #000000;">1</span></pre></div></div>

<p>This is from having an incompatible version of <code>expat</code> installed. Here are instructions for installing the latest version of expat.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">curl -O -L http://downloads.sourceforge.net/expat/expat<span style="color: #000000;">-2.0</span><span style="color: #000000;">.1</span>.<span style="color: #c20cb9; font-weight: bold;">tar</span>.gz
<span style="color: #c20cb9; font-weight: bold;">tar</span> -xzf expat<span style="color: #000000;">-2.0</span><span style="color: #000000;">.1</span>.<span style="color: #c20cb9; font-weight: bold;">tar</span>.gz 
<span style="color: #7a0874; font-weight: bold;">cd</span> expat<span style="color: #000000;">-2.0</span><span style="color: #000000;">.1</span>
./configure --<span style="color: #007800;">prefix=</span>/usr/<span style="color: #7a0874; font-weight: bold;">local</span>
<span style="color: #c20cb9; font-weight: bold;">make</span> 
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ../
<span style="color: #c20cb9; font-weight: bold;">rm</span> -fr expat<span style="color: #000000;">-2.0</span><span style="color: #000000;">.1</span>*</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2008/04/05/configuring-sphinx-on-os-x-leopard/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Syncing to Time Capsule from Terminal</title>
		<link>http://derekdevries.com/2008/04/05/syncing-to-time-capsule-from-terminal/</link>
		<comments>http://derekdevries.com/2008/04/05/syncing-to-time-capsule-from-terminal/#comments</comments>
		<pubDate>Sat, 05 Apr 2008 21:29:36 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[osx terminal]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=13</guid>
		<description><![CDATA[I got really excited about the <a href="http://www.apple.com/timecapsule/">Apple Time Capsule</a> when it was released. I had two 500GB duplicate external hard drives chained off of my Mac Book Pro to backup my rather large collection of media. I typically use <a href="http://en.wikipedia.org/wiki/Rsync">rsync</a> to copy files to the drives and keep them in sync. ]]></description>
			<content:encoded><![CDATA[<p>I got really excited about the <a href="http://www.apple.com/timecapsule/">Apple Time Capsule</a> when it was released. I had two 500GB duplicate external hard drives chained off of my Mac Book Pro to backup my rather large collection of media. I typically use <a href="http://en.wikipedia.org/wiki/Rsync">rsync</a> to copy files to the drives and keep them in sync. </p>
<p>The only problem with my setup was that it required me to connect these drives to my laptop and eject them as I move my computer around the house. Time Capsule solves this by making my setup wireless. </p>
<p>You can view the hard drive volumes mounted on your Mac from the terminal:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$&gt; <span style="color: #c20cb9; font-weight: bold;">ls</span> /Volumes
./                    Macintosh HD@
../                   Derek<span style="color: #ff0000;">'s Time Capsule/</span></pre></div></div>

<p>When using a Time Capsule drive, it may not appear in <code>/Volumes</code> if it isn&#8217;t mounted. You can mount your drive with a simple bash script that uses <code>mount_afp</code>.</p>
<p>Save as mount_capsule.sh</p>

<div class="wp_syntax"><div class="code"><pre class="bash"><span style="color: #808080; font-style: italic;">#!/bin/bash</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># configure - enter your capsule name, ip, and username:password</span>
<span style="color: #007800;">CAP_NAME=</span><span style="color: #ff0000;">&quot;Derek's Time Capsule&quot;</span>
<span style="color: #007800;">CAP_IP=</span><span style="color: #ff0000;">&quot;192.168.1.103&quot;</span>
<span style="color: #007800;">USER=</span><span style="color: #ff0000;">&quot;username:password&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> ! <span style="color: #7a0874; font-weight: bold;">&#91;</span> -d <span style="color: #ff0000;">&quot;/Volumes/$CAP_NAME&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[info] Mounting $CAP_NAME...&quot;</span>
&nbsp;
  <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #ff0000;">&quot;/Volumes/$CAP_NAME&quot;</span>
  mount_afp <span style="color: #ff0000;">&quot;afp://$USER@$CAP_IP/$CAP_NAME&quot;</span> <span style="color: #ff0000;">&quot;/Volumes/$CAP_NAME&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;[info] $CAP_NAME mounted&quot;</span></pre></div></div>

<p>And now let&#8217;s make the script executable and run it.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$&gt; <span style="color: #c20cb9; font-weight: bold;">chmod</span> <span style="color: #000000;">744</span> mount_capsule.<span style="color: #c20cb9; font-weight: bold;">sh</span>
$&gt; ./mount_capsule.<span style="color: #c20cb9; font-weight: bold;">sh</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span>info<span style="color: #7a0874; font-weight: bold;">&#93;</span> Mounting Derek<span style="color: #ff0000;">'s Time Capsule...
[info] Derek'</span>s Time Capsule mounted</pre></div></div>

<p>Once we&#8217;ve mounted the drive, we can proceed to copy/move/rsync data to it.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">rsync -avz --delete /Users/derek/work /Volumes/Derek\<span style="color: #ff0000;">'s<span style="color: #000099; font-weight: bold;">\ </span>Time<span style="color: #000099; font-weight: bold;">\ </span>Capsule/backup</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2008/04/05/syncing-to-time-capsule-from-terminal/feed/</wfw:commentRss>
		</item>
		<item>
		<title>The Upper Class</title>
		<link>http://derekdevries.com/2008/03/26/the-upper-class/</link>
		<comments>http://derekdevries.com/2008/03/26/the-upper-class/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 23:27:33 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=12</guid>
		<description><![CDATA[Today I did a presentation on Ruby object inheritance for the <a href="http://www.techvalleyrb.org/">Tech Valley Ruby Brigade</a>. Here are <a href="http://assets.derekdevries.com/presentations/2008/the_upper_class.pdf">the slides</a>. ]]></description>
			<content:encoded><![CDATA[<p>Today I did a presentation on Ruby object inheritance for the <a href="http://www.techvalleyrb.org/">Tech Valley Ruby Brigade</a>. Here are <a href="http://assets.derekdevries.com/presentations/2008/the_upper_class.pdf">the slides</a>. Some of the presentation is based off material in the first chapter of <a href="http://www.amazon.com/gp/redirect.html%3FASIN=0596510322%26tag=sportspyder-20%26lcode=xm2%26cID=2025%26ccmID=165953%26location=/o/ASIN/0596510322%253FSubscriptionId=1RVMD8N7EVCFF8RR1CG2">Advanced Rails</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2008/03/26/the-upper-class/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails for PHP Reference</title>
		<link>http://derekdevries.com/2008/03/21/rails-for-php-reference/</link>
		<comments>http://derekdevries.com/2008/03/21/rails-for-php-reference/#comments</comments>
		<pubDate>Fri, 21 Mar 2008 22:49:16 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[php]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=11</guid>
		<description><![CDATA[<a href="http://mikenaberezny.com">Mike</a> and I have been diligently converting PHP methods to Ruby for our freshly released <a href="http://railsforphp.com/reference">Rails for PHP Reference</a>. This section is an extension of the reference found in our book -- <a href="http://www.pragprog.com/titles/ndphpr/">Rails for PHP Developers</a>. ]]></description>
			<content:encoded><![CDATA[<p><a href="http://mikenaberezny.com">Mike</a> and I have been diligently converting PHP methods to Ruby for our freshly released <a href="http://railsforphp.com/reference">Rails for PHP Reference</a>. This section is an extension of the reference found in our book &#8212; <a href="http://www.pragprog.com/titles/ndphpr/">Rails for PHP Developers</a>. </p>
<p>The book covers the language reference, while this reference aims to translate as many of the PHP methods as possible. We&#8217;ve even put together an easy way of searching for your favorite PHP functions by typing them in the url like: <code>http://railsforphp.com/preg_match_all</code>. </p>
<p>Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2008/03/21/rails-for-php-reference/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails Asset Cache</title>
		<link>http://derekdevries.com/2007/12/13/rails-asset-cache/</link>
		<comments>http://derekdevries.com/2007/12/13/rails-asset-cache/#comments</comments>
		<pubDate>Thu, 13 Dec 2007 22:29:53 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[css]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=10</guid>
		<description><![CDATA[Rails 2.0 uses asset cache to combine multiple Javascript or CSS libraries to a single Javascript and single CSS file. This improves performance by reducing the number of HTTP requests.]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my article on Minifying Your Rails Javascript, let&#8217;s take a look at a new feature in Rails 2.0 that makes packaging your assets into a single file much easier.</p>
<h3>Enabling Cache</h3>
<p>We use asset helpers such as <code>javascript_include_tag</code> and <code>stylesheet_link_tag</code> in our layouts to include external Javascript and CSS files. One benefit of using these helpers is the ability to cache these resources. Asset cache is turned on when the <code>perform_caching</code> configuration option is set to <code>true</code>. </p>
<p>Rails applications default to having caching disabled during development, and enabled in production. We can change this in <code>config/environments/development.rb</code> to test out caching during development.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">config.<span style="color:#9900CC;">action_controller</span>.<span style="color:#9900CC;">perform_caching</span> = <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<h3>Caching Assets</h3>
<p>We enable asset caching in our applications using the <code>:cache</code> option in our helpers. We can cache Javascript files by doing the following:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">&lt;%= javascript_include_tag <span style="color:#996600;">&quot;prototype&quot;</span>, <span style="color:#996600;">&quot;application&quot;</span>, <span style="color:#996600;">&quot;effects&quot;</span>,
                           <span style="color:#ff3333; font-weight:bold;">:cache</span> =&gt; <span style="color:#996600;">&quot;cache/all&quot;</span> %&gt;</pre></div></div>

<p>When caching is disabled, Rails will generate the normal references to these resources.</p>

<div class="wp_syntax"><div class="code"><pre>&lt;script src=&quot;/javascripts/prototype.js?1197321216&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/javascripts/application.js?1197321216&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;/javascripts/effects.js?1197321216&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>However when caching is enabled, Rails will generate a single external Javascript file in the location we specified. It will then reference this file instead of the original source files.</p>

<div class="wp_syntax"><div class="code"><pre>&lt;script src=&quot;/javascripts/cache/all.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre></div></div>

<p>As you can see in the above example, we&#8217;re going to store our asset cache in subdirectories named <code>cache/</code>. This makes it easier to clear out the cache later on. Caching external CSS files works similar to the Javascript example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">&lt;%= stylesheet_link_tag <span style="color:#996600;">&quot;typography&quot;</span>, <span style="color:#996600;">&quot;layout&quot;</span>, <span style="color:#996600;">&quot;color&quot;</span>,
                        <span style="color:#ff3333; font-weight:bold;">:cache</span> =&gt; <span style="color:#996600;">&quot;cache/all&quot;</span> %&gt;</pre></div></div>

<h3>Clearing Cache</h3>
<p>Rails comes with some useful built-in tasks for clearing out temporary files under the <code>tmp:</code> rake namespace. Rails does not come with a built-in task for clearing out the asset cache. We&#8217;ll add this ourselves by adding a new rake task named rake <code>tmp:assets:clear</code>. Create a new file in your Rails application named at <code>lib/tasks/tmp.rake</code>, and add the following code.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">namespace <span style="color:#ff3333; font-weight:bold;">:tmp</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  namespace <span style="color:#ff3333; font-weight:bold;">:assets</span> <span style="color:#9966CC; font-weight:bold;">do</span> 
    desc <span style="color:#996600;">&quot;Clears javascripts/cache and stylesheets/cache&quot;</span>
    task <span style="color:#ff3333; font-weight:bold;">:clear</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>      
      <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">rm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public/javascripts/cache/[^.]*'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">rm</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public/stylesheets/cache/[^.]*'</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The custom <code>tmp:assets:clear</code> task we added removes all files from both the <code>javascripts/cache/</code> and <code>stylesheets/cache/</code> directories. Let&#8217;s give it a whirl:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$&gt; rake tmp:assets:<span style="color: #c20cb9; font-weight: bold;">clear</span></pre></div></div>

<h3>Wrap Up</h3>
<p>Using asset cache can significantly reduce the number of requests sent to the server for external CSS and Javascript files. Along with a good gzip strategy, this is an essential feature when working with large multi-file Javascript libraries. </p>
<p>For additional performance increases using asset helpers, check out Chad Fowler&#8217;s write-up of <a href="http://chadfowler.com/2007/2/18/edge-rails-goody-distributed-asset-hosts">Distributed Asset Hosts</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2007/12/13/rails-asset-cache/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Rails, IE, and Parallels</title>
		<link>http://derekdevries.com/2007/03/26/rails-ie-and-parallels/</link>
		<comments>http://derekdevries.com/2007/03/26/rails-ie-and-parallels/#comments</comments>
		<pubDate>Mon, 26 Mar 2007 22:28:46 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[ie]]></category>

		<category><![CDATA[parallels]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=9</guid>
		<description><![CDATA[Testing your Rails applications in Internet Explorer is still a necessity these days, and Parallels Desktop for the Mac does a great job at making this as painless as possible.]]></description>
			<content:encoded><![CDATA[<p>More and more web developers are using OS X as their development tool of choice these days. It&#8217;s hard not to with excellent tools like <a href="http://macromates.com">TextMate</a>, and the ability to run in a Unix style environment. As much as we&#8217;d like to give IE the boot, we know that the browser market share is still dominated by this browser. Web development professionals these days need to make sure their code at least works in Firefox, Safari, IE6, and IE7. This leaves us with two very important browsers which don&#8217;t natively run on the Mac. <a href="http://www.parallels.com/en/products/desktop/">Parallels Desktop for Mac</a> comes to the rescue here to make it just as easy to test the two versions of IE on a Mac as it is on a PC. </p>
<p>We&#8217;ll go over the basics needed to test your Rails applications in IE6/IE7 on Parallels in the most convenient way possible. </p>
<ul>
<li><a href="#getting-started">Getting Started</a></li>
<li><a href="#open-ports-on-the-network">Open Ports on the Network</a></li>
<li><a href="#browsing-in-ie">Browsing in IE</a></li>
<li><a href="#browse-by-name-with-bonjour">Browse By Name with Bonjour</a></li>
</ul>
<h3>Getting Started</h3>
<p>I&#8217;ll start out by saying that the first prerequisite for this setup is having an Intel powered Mac. This article is useless without one unless you&#8217;re just here to drool over neat tools. </p>
<p>Being able to use Parallels comes at a cost. You&#8217;ll need to shell out $79.99 for a copy of Parallels for Mac, and a couple hundred more for a copy of Windows. I believe this is a nominal business cost for what the software offers, and is much cheaper than having to buy and maintain a separate PC to run Windows. Both Parallels and Windows have a trial versions you can use to verify that this setup is worth your money. </p>
<p>Once you <a href="http://www.parallels.com/en/download/desktop/">download Parallels for Mac</a>, you can unpack and install the software as usual. Parallels has a great guide for getting started with the software. It will take you through Installing Parallels Desktop, Creating new Virtual Machines, and Installing Windows on the machine. I started out by installing a single Windows XP virtual machine and set it up with IE6 and all other default settings I use. I then just copied that setup to create a new VM on which I upgraded to IE7. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/vms.gif" />
</p>
<h3>Open Ports on the Network</h3>
<p>When developing Rails applications on the Mac, the typical setup is to develop everything on localhost, and serve the applications using mongrel.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ <span style="color: #7a0874; font-weight: bold;">cd</span> maintainable
$ mongrel_rails start -d -p <span style="color: #000000;">3000</span></pre></div></div>

<p>We can of course access this directly in our native Mac browsers by navigating to <code>http://localhost:3000</code>. To make this application accessible from IE on our Windows VM, we&#8217;ll have to accept network traffic through our firewall on port 3000. Open up your Apple System Preferences and go to the sharing window.</p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/sharing.gif" />
</p>
<p>Navigate to the <strong>Firewall</strong> Tab in the Sharing pane, and click on the <strong>New</strong> button so that we can add an additional  port(s). </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/new_port.gif" />
</p>
<p>Here is where we make our Rails ports available on the network. We&#8217;re going to open up ports 3000-3099 for any additional Rails applications we choose to bind to a port in that range. Enter</p>
<ul>
<li><strong>Port Name:</strong> Other</li>
<li><strong>TCP Port Number(s):</strong> 3000-3099</li>
<li><strong>Description:</strong> Rails Applications (30xx)</li>
</ul>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/add_port.gif" />
</p>
<h3>Browsing in IE</h3>
<p>Now we should be able to browse to this from IE in our Parallels machine by pointing to our internal IP Address. We can find out this internal address by taking a look at the Services tab on our Sharing pane. Near the bottom it will say something like &#8220;Other Macintosh Users can access your computer at afp://192.168.1.100&#8243;. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/internal_ip.gif" />
</p>
<p>This is the number we&#8217;re looking for, and we can alternately find it using this command from the terminal.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ ifconfig | <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #000000;">192</span> | <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{print $2}'</span></pre></div></div>

<p>Once we have our internal IP, let&#8217;s fire up our IE browser and check out our site. In IE, navigate to http://192.168.1.100:3000. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/rails_in_ie.gif" />
</p>
<h3>Browse By Name with Bonjour</h3>
<p>The problem with browsing by internal IP address is that it&#8217;s likely to change from day to day. A better approach is to install Bonjour for Windows so that we can simply refer to our computer by name instead of number. </p>
<p>We&#8217;ll first go back into our Services tab on the Sharing pane in Apple System Preferences. Here we&#8217;re going to shorten our name to be something easy to remember and type into the browser. Click on the <strong>Edit</strong> Button near the top. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/edit_name.gif" />
</p>
<p>I&#8217;m going to simply rename this to <strong>derek.local</strong>. Feel free to be more creative with your own. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/derek_local.gif" />
</p>
<p>To seamlessly network with our Windows VM without the hassle of dealing with network settings manually, we&#8217;re going to install <a href="http://developer.apple.com/networking/bonjour/download/">Bonjour for Windows</a>. Download <strong>BonjourSetup.exe</strong>, and choose the default settings for the installation. With Bonjour installed, we can now browse to our application at http://derek.local:3000. </p>
<p class="inline_figure">
  <img src="http://assets.derekdevries.com/images/articles/2007/03/rails_in_ie_by_name.gif" /></p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2007/03/26/rails-ie-and-parallels/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Minifying Your Rails Javascript</title>
		<link>http://derekdevries.com/2007/02/05/minifying-your-rails-javascript/</link>
		<comments>http://derekdevries.com/2007/02/05/minifying-your-rails-javascript/#comments</comments>
		<pubDate>Mon, 05 Feb 2007 22:27:43 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[prototype]]></category>

		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=8</guid>
		<description><![CDATA[The Javascript libraries that ship with Rails are extremely useful, but have a significant filesize footprint. Writing a simple Rake task to minify our Javascript files using JSMin provides a way cut down the size of your Javascript code before deploying your Rails application.]]></description>
			<content:encoded><![CDATA[<p>While Prototype and Scriptaculous prove to exceptionally useful, they both have a pretty substantial footprint. The current stable Prototype (v1.5) is 69K, and the Scriptaculous Effects library is 36K. This puts us at over 100K to include these Javascript libraries. </p>
<p>This number can be cut down a little bit with Douglas Crockford&#8217;s <a href="http://www.crockford.com/javascript/jsmin.html">JSMin</a> filter. This filter creates a smaller version of your Javascript source by stripping out extraneous code like whitespace and comments. The minified versions of Prototype is 52K, and Effects is 28K. This saves us around 25K off the bat. </p>
<p>Since we like to keep as much of our code as possible in the Ruby family, we use the Ruby version of the minifier. Copy The <a href="http://www.crockford.com/javascript/jsmin.rb">jsmin.rb</a> file to <span class="path">script/javscript/jsmin.rb</span> in your Rails application.</p>
<p>Now let&#8217;s add a handy Rake task to minify your javascript code using this script. The following task will combine <span class="path">prototype.js</span>, <span class="path">effects.js</span>, and <span class="path">application.js</span>. This will create our final minified file as <span class="path">all_min.js</span>. You can add additional javascript files to the <code>libs</code> array as needed, or adjust what you want the final file to be named. Then copy the code into a file named <span class="path">lib/tasks/js.rake</span>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">namespace <span style="color:#ff3333; font-weight:bold;">:js</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#996600;">&quot;Minify javascript src for production environment&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:min</span> =&gt; <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    <span style="color:#008000; font-style:italic;"># list of files to minify</span>
    libs = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'public/javascripts/prototype.js'</span>, 
            <span style="color:#996600;">'public/javascripts/effects.js'</span>, 
            <span style="color:#996600;">'public/javascripts/application.js'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># paths to jsmin script and final minified file</span>
    jsmin = <span style="color:#996600;">'script/javascript/jsmin.rb'</span>
    final = <span style="color:#996600;">'public/javascripts/all_min.js'</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># create single tmp js file</span>
    tmp = <span style="color:#CC00FF; font-weight:bold;">Tempfile</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'all'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    libs.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|lib| <span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>lib<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|f| tmp.<span style="color:#9900CC;">write</span><span style="color:#006600; font-weight:bold;">&#40;</span>f.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    tmp.<span style="color:#9900CC;">rewind</span>
&nbsp;
    <span style="color:#008000; font-style:italic;"># minify file</span>
    %x<span style="color:#006600; font-weight:bold;">&#91;</span>ruby <span style="color:#008000; font-style:italic;">#{jsmin} &lt; #{tmp.path} &gt; #{final}]</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>#{final}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Before we run the task, there is a single point in Prototype version 1.5 stable that breaks the jsmin filter. There is a <a href="http://dev.rubyonrails.org/ticket/7301">patch</a> for this in the Rails trac, but we will need to manually add the single missing semi-colon until it is applied. You may want to also run your application code through <a href="http://www.jslint.com/">JSLint Verifier</a> and clean up any issues that may jam the jsmin filter. </p>
<p>Now we can minify our javascript by running this new task. It will create a new javascript file with the combined source. From the root of your rails application run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ rake js:min
<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> /Users/derek/work/sportspyder/trunk<span style="color: #7a0874; font-weight: bold;">&#41;</span>
&nbsp;
created public/javascripts/all_min.js
&lt;pre&gt;
&nbsp;
In our development environment we don<span style="color: #ff0000;">'t want to have to re-minify our javascript after every change. We'</span>ll still use our separate and easy to <span style="color: #c20cb9; font-weight: bold;">read</span> <span style="color: #7a0874; font-weight: bold;">source</span> files. We can <span style="color: #000000; font-weight: bold;">do</span> this by putting a conditional <span style="color: #000000; font-weight: bold;">in</span> our layout to include the minified version <span style="color: #000000; font-weight: bold;">in</span> production mode, and the separate files during development.
&nbsp;
&nbsp;
&lt;pre <span style="color: #007800;">lang=</span><span style="color: #ff0000;">&quot;ruby&quot;</span>&gt;
&lt;% <span style="color: #000000; font-weight: bold;">if</span> RAILS_ENV == <span style="color: #ff0000;">'production'</span> %&amp;gt;
&lt;%= javascript_include_tag <span style="color: #ff0000;">&quot;all_min&quot;</span> %&amp;gt;
&lt;% <span style="color: #000000; font-weight: bold;">else</span> %&amp;gt;
&lt;%= javascript_include_tag <span style="color: #ff0000;">&quot;prototype&quot;</span>, <span style="color: #ff0000;">&quot;effects&quot;</span>,
                           <span style="color: #ff0000;">&quot;application&quot;</span> %&amp;gt;
&lt;% end %&gt;</pre></div></div>

<p>One of the added benefits of this approach is that you have a single Javascript source file for the client to download instead of multiple resources for the browser to pull. </p>
<p>You&#8217;ll have to make sure to run this rake task before you deploy, and may want to add it directly to your deployment script. </p>
<p>While minification provides a nice break in file size, it is secondary to sending your javascript compressed. Compression can cut your Javascript to nearly 1/4 of the original size, and is most commonly done using using  Apache&#8217;s mod_deflate module. There is more information on Apache settings on <a href="http://mongrel.rubyforge.org/docs/apache.html">the Mongrel website</a>.</p>
<h4>Update - 03/01/2007</h4>
<p>Prototype doesn&#8217;t actively support a specific packing strategy for various reasons, and patches submitted for making the code pass something like JSLint are likely to be ignored. Reasons for this, and some other strategies on reducing filesize of the library are discussed in <a href="http://www.andrewdupont.net/2007/02/26/packing-prototype/"> Packing Prototype</a>. </p>
<h4>Update - 12/14/2007</h4>
<p>Rails 2.0 includes a feature that makes much of what is described here obsolete. Check out our write-up of Rails Asset Cache.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2007/02/05/minifying-your-rails-javascript/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Testing Javascript in Rails</title>
		<link>http://derekdevries.com/2007/01/08/testing-javascript-in-rails/</link>
		<comments>http://derekdevries.com/2007/01/08/testing-javascript-in-rails/#comments</comments>
		<pubDate>Mon, 08 Jan 2007 22:25:38 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[rails]]></category>

		<category><![CDATA[rake]]></category>

		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=7</guid>
		<description><![CDATA[While performing unit and functional tests are expected in any good Rails application, testing Javascript often falls to the wayside. If you have been looking for an elegant solution to testing your Javascript code, the Test.Unit component of Scriptaculous is your answer.]]></description>
			<content:encoded><![CDATA[<p>Testing is encouraged and expected in Rails, but most people fall short on good coverage of their Javascript code. We&#8217;ll go over how the <a href="http://wiki.script.aculo.us/scriptaculous/show/UnitTesting">Scriptaculous</a> Unit Testing library provides a nice solution to testing custom classes you have written using the Prototype library. </p>
<h3>Installation</h3>
<p>The Scriptaculous unit testing library can be installed in your Rails application using the <a href="http://dev.rubyonrails.org/svn/rails/plugins/javascript_test/">javascript_test</a> plugin located in the Rails subversion repository. To install the plugin, navigate to the base directory of your application and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ ./script/plugin <span style="color: #c20cb9; font-weight: bold;">install</span> \\
http://dev.rubyonrails.org/svn/rails/plugins/javascript_test</pre></div></div>

<p>Once the plugin is installed, you will have a new generator script to create tests for your Javascript files. To create a test skeleton file for a file such as <span class="path">public/javascripts/cookies.js</span>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ ./script/generate javascript_test cookies
  create  <span style="color: #7a0874; font-weight: bold;">test</span>/javascript
  create  <span style="color: #7a0874; font-weight: bold;">test</span>/javascript/cookies_test.html
<span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> %&gt;</pre></div></div>

<p>Now we have a special directory within <span class="path">test</span> to store our javascript test files. Each test suite will be an HTML file that includes the framework necessary for the tests to run. </p>
<h3>Writing Tests</h3>
<p>If you take a look at <span class="path">test/javascript/cookies_test.html</span>, you&#8217;ll see a familiar setup to Ruby&#8217;s <code>Test::Unit</code> library. There are the typical <code>setup</code> and <code>teardown</code> methods, along with a dummy test to get you started.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">new</span> Test.<span style="color: #006600;">Unit</span>.<span style="color: #006600;">Runner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
  <span style="color: #009900; font-style: italic;">// replace this with your real tests</span>
  setup: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>,
&nbsp;
  teardown: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>,
&nbsp;
  testTruth: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">with</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    assert<span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;testlog&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>For the <code>cookies.js</code> library I&#8217;m testing, I want to make sure that my code can set and access cookies with new methods that I&#8217;m adding to the <code>document</code> object. I&#8217;ll start out by setting the cookie, and then assert that when I retrieve it, it is the same value I set. I&#8217;ll also add a snippet to the <code>setup</code> method that makes sure the cookie is deleted before each test.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">new</span> Test.<span style="color: #006600;">Unit</span>.<span style="color: #006600;">Runner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
  setup: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// clear out cookie</span>
    <span style="color: #003366; font-weight: bold;">var</span> expires = <span style="color: #3366CC;">&quot;expires=Thu, 01-Jan-70 00:00:01 GMT&quot;</span>
    document.<span style="color: #006600;">cookie</span> = <span style="color: #3366CC;">'foo'</span> + <span style="color: #3366CC;">&quot;=; &quot;</span> + expires + <span style="color: #3366CC;">&quot;; path=/&quot;</span>;
  <span style="color: #66cc66;">&#125;</span>,
&nbsp;
  <span style="color: #009900; font-style: italic;">// test getting/setting cookies</span>
  testSettingAndAccessingCookies: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">with</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    assertEqual<span style="color: #66cc66;">&#40;</span>undefined, document.<span style="color: #006600;">getCookie</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    document.<span style="color: #006600;">setCookie</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span>, <span style="color: #3366CC;">'bar'</span><span style="color: #66cc66;">&#41;</span>;
    assertEqual<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bar'</span>, document.<span style="color: #006600;">getCookie</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'foo'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;testlog&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>This obviously isn&#8217;t the greatest test coverage,  but you get the idea. Adding new tests cases is as easy is adding additional methods that are prefixed with <code>test</code>. </p>
<p>There are many additional assertions beyond <code>assertEqual()</code>, including among others: </p>
<ul>
<li>assert</li>
<li>assertNull</li>
<li>assertMatch</li>
<li>assertVisible</li>
</ul>
<p>Check the source in <span class="path">plugins/javascript_test/assets/unittest.js</span><br />
for the full list. </p>
<h3>Running the Tests</h3>
<p>Running the tests is done in one of two ways. The first way is the very cool <code>test:javascripts</code> rake task that is bundled with the plugin. This task will launch an instance of the WEBrick server, and attempt to start up Safari, Firefox, IE, and Konquerer. It will run the javascript test in each of the available browsers on your system.</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ rake <span style="color: #7a0874; font-weight: bold;">test</span>:javascripts
  <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">in</span> /Users/derek/work/demo<span style="color: #7a0874; font-weight: bold;">&#41;</span>
  /<span style="color: #7a0874; font-weight: bold;">test</span>/javascript/cookies_test.html on Safari: SUCCESS
  /<span style="color: #7a0874; font-weight: bold;">test</span>/javascript/cookies_test.html on Firefox: SUCCESS
  Skipping Internet Explorer, not supported on this OS
  Skipping Konqueror, not supported on this OS</pre></div></div>

<p>The tests results will pop up in a nice red=fail, green=pass HTML interface. There is no backtrace information given, so it&#8217;s smart to make small test cases so that you can easily find any specific problems. </p>
<p class="center">
  <img src="http://assets.derekdevries.com/images/articles/2007/01/test_result.gif" alt="Test Results" />
</p>
<p>While this is fine and dandy, it is rather slow during active development. The other testing method is to open the HTML test files directly in your browser and just hit refresh to rerun the tests. </p>
<p>There is one small setup you need to do for this to work. You need to make a symbolic link to the unit testing javascript library for your tests to use. From your Rails application base directory:</p>

<div class="wp_syntax"><div class="code"><pre class="bash">$ <span style="color: #c20cb9; font-weight: bold;">ln</span> -s ../../vendor/plugins/javascript_test/assets/ \\
<span style="color: #7a0874; font-weight: bold;">test</span>/javascript/assets</pre></div></div>

<h3>Manipulating DOM Elements</h3>
<p>When you&#8217;re writing Javascript, rarely does the code work in a vacuum. Most of the time you are manipulating some type of element in the DOM tree. To test this type of interaction, you will obviously need some mock HTML source for the test to operate on. The convention in these type of tests is to just add this HTML right to the test file itself above your tests.</p>

<div class="wp_syntax"><div class="code"><pre>&lt;!-- our test DOM structure --&gt;
&lt;div id=&quot;widget_1&quot;&gt;
  &lt;h2&gt;States&lt;/h2&gt;
  &lt;ul style=&quot;display: none&quot;&gt;
    &lt;li&gt;Arizona&lt;/li&gt;
    &lt;li&gt;California&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;
&nbsp;
&lt;!-- our javascript test code --&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
// &lt;![CDATA[
...
// ]]&gt;
&lt;/script&gt;</pre></div></div>

<p>You will notice this text showing up on the HTML page as your tests run. Don&#8217;t sweat it because this is normal. Just watch for red. </p>
<h3>Writing Timed Based Tests</h3>
<p>When using javascript animation effects, a lot of features happen over a period of time. When testing the state of elements in javascript (such as whether they are visible/hidden), we will often need to wait for the effect to take place before we make our assertion. The library includes a helpful way of doing this using the <code>wait()</code> method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">new</span> Test.<span style="color: #006600;">Unit</span>.<span style="color: #006600;">Runner</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span>
  setup: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #66cc66;">&#125;</span>,
&nbsp;
  <span style="color: #009900; font-style: italic;">// get/set cookies</span>
  testShowWidgetBody: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">with</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> widget = <span style="color: #003366; font-weight: bold;">new</span> Widget<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'widget_1'</span><span style="color: #66cc66;">&#41;</span>;
    assertHidden<span style="color: #66cc66;">&#40;</span>widget.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #009900; font-style: italic;">// widget.toggle takes 500ms seconds to complete</span>
    widget.<span style="color: #006600;">toggle</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
    <span style="color: #009900; font-style: italic;">// wait 550ms before asserting that it worked</span>
    wait<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">550</span>, <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span> <span style="color: #000066; font-weight: bold;">with</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      assertNotHidden<span style="color: #66cc66;">&#40;</span>widget.<span style="color: #006600;">body</span><span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span>, <span style="color: #3366CC;">&quot;testlog&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>There is much more to this library, but that&#8217;s all for now folks. Hopefully this gives you a good start so that you&#8217;re well on your way to testing your Javascript code as diligently as you test your Ruby code. </p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2007/01/08/testing-javascript-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating Classes with Prototype</title>
		<link>http://derekdevries.com/2006/12/21/creating-classes-with-prototype/</link>
		<comments>http://derekdevries.com/2006/12/21/creating-classes-with-prototype/#comments</comments>
		<pubDate>Thu, 21 Dec 2006 22:24:23 +0000</pubDate>
		<dc:creator>derek</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[javascript]]></category>

		<category><![CDATA[prototype]]></category>

		<guid isPermaLink="false">http://derekdevries.com/?p=6</guid>
		<description><![CDATA[The Prototype library helps improve the readability of Javascript code by adding a syntactic sugar for creating objects. This syntax organizes Javascript's unfamiliar prototype-based inheritance scheme in a pattern that is more common to programmers coming from Ruby, PHP, or Java.]]></description>
			<content:encoded><![CDATA[<p>Note: Examples make use of Joe Hewitt&#8217;s <a href="https://addons.mozilla.org/firefox/1843/">Firebug</a> extension for Firefox. If you do not have this software, feel free to follow along by replacing <code>console.log(msg)</code> with a much less elegant option such as <code>alert(msg)</code>.</p>
<p>Now that Ajax has reared its ugly head, developers are awakening to the powerful behavior that Javascript can add to their applications. Javascript&#8217;s very unfamiliar object syntax is one of the reasons it had previously been avoided like the plague. Since Javascript has a prototype-based inheritance, creating objects is quite different than the class-based inheritance scheme we are used to. </p>
<h3>Simple Javascript Objects</h3>
<p>Creating an object in Javascript is actually quite simple using the <code>new Object</code> syntax, and we can add properties on the fly using the <code>.</code> operator.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> d = <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
d.<span style="color: #000066;">name</span> = <span style="color: #3366CC;">'Tripod'</span>;
d.<span style="color: #006600;">legs</span> = <span style="color: #CC0000;">3</span>;
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;My Dog &quot;</span>+d.<span style="color: #000066;">name</span>+<span style="color: #3366CC;">&quot; has &quot;</span>+d.<span style="color: #006600;">legs</span>+<span style="color: #3366CC;">&quot; legs&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Methods are added to an object using the same syntax as properties. In fact methods are simply functions assigned as properties. Keep in mind that functions in Javascript are actually objects, and can be passed around and assigned just like any other data type. In this case, we will attach a function to the <code>run</code> property of our object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">d.<span style="color: #006600;">run</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'hobble... hobble'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #66cc66;">&#125;</span>
d.<span style="color: #006600;">run</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Another option for creating objects is using Javascript&#8217;s object literal syntax. This uses a key/value pair definition within curly braces to define the object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> d = <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066;">name</span>: <span style="color: #3366CC;">'Tripod'</span>,
  legs: <span style="color: #CC0000;">3</span>,
  run: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'hobble... hobble'</span><span style="color: #66cc66;">&#41;</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h3>Object Constructors</h3>
<p>Although simple, these ways of creating objects becomes repetitive when we want to create another object. What we really want to do is create a template for an object that stores all of this information together so that we can reuse it. This is done in javascript by creating a constructor function. This function lets you pass in variables to define the state of your object similar to how you would in a class. A constructor looks just like any other Javascript function except for a few constructor commandments:</p>
<ul>
<li>The function name shall be capitalized (not required but highly recommended).</li>
<li>The function shall be called through use of the <code>new</code> construct.</li>
<li>The function shall make use of the <code>this</code> keyword.</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> Party<span style="color: #66cc66;">&#40;</span>people, beers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> = people;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>  = beers;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We can now create a Party object using the <code>new</code> construct. This will create a reference to a new Javascript object, and pass that object into the function to be referenced with the <code>this</code> keyword.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> p1 = <span style="color: #003366; font-weight: bold;">new</span> Party<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">3</span>, <span style="color: #CC0000;">9</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #003366; font-weight: bold;">var</span> p2 = <span style="color: #003366; font-weight: bold;">new</span> Party<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">11</span>, <span style="color: #CC0000;">60</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 1 has: &quot;</span> + p1.<span style="color: #006600;">people</span> + <span style="color: #3366CC;">&quot; peeps&quot;</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 2 has: &quot;</span> + p2.<span style="color: #006600;">people</span> + <span style="color: #3366CC;">&quot; peeps&quot;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>We can now create lots of Parties in a more efficient method than before. This is great, but these objects are pretty useless without any behavior. Let&#8217;s add a method to our parties to see if any of them are kickin.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Party.<span style="color: #006600;">prototype</span>.<span style="color: #000066;">status</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>/<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> &gt; <span style="color: #CC0000;">5</span> ? <span style="color: #3366CC;">'kickin'</span> : <span style="color: #3366CC;">'lame'</span>;
<span style="color: #66cc66;">&#125;</span>
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 1 is: &quot;</span> + p1.<span style="color: #000066;">status</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 2 is: &quot;</span> + p2.<span style="color: #000066;">status</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Earlier we created a method by assigning a function directly to the property of an object, but this time we have assigned it to <code>Party</code>&#8217;s <code>prototype</code> property. This is where Javascript gets a bit confusing. The Party constructor has an extra property that is always defined by default: the <code>prototype</code> object. This is like a parent object of all Party objects that gets shared between each instance. Since methods will remain the same for all <code>Party</code> instances, we don&#8217;t want to waste memory by creating copies for each instance. The <code>prototype</code> object gives us a space to share those properties between all instances. We can do a similar thing with any property that will remain constant in all instances.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Party.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">music</span> = <span style="color: #003366; font-weight: bold;">true</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 1 tunes: &quot;</span> + p1.<span style="color: #006600;">music</span><span style="color: #66cc66;">&#41;</span>;
console.<span style="color: #006600;">log</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;party 2 tunes: &quot;</span> + p2.<span style="color: #006600;">music</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Because it&#8217;s no party without tunes. Now we have a template to create a party that always has music, and we can check if it&#8217;s a party worth attending by using the <code>status()</code> method. The entire definition of our object template like so:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> Party<span style="color: #66cc66;">&#40;</span>people, beers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> = people;
  <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>  = beers;
<span style="color: #66cc66;">&#125;</span>
Party.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">music</span>  = <span style="color: #003366; font-weight: bold;">true</span>;
Party.<span style="color: #006600;">prototype</span>.<span style="color: #000066;">status</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>/<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> &gt; <span style="color: #CC0000;">5</span> ? <span style="color: #3366CC;">'kickin'</span> : <span style="color: #3366CC;">'lame'</span>;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>It&#8217;s definitely not going to win a beauty pageant. </p>
<h3>The Prototype Way</h3>
<p>This is where the <a href="http://prototype.conio.net/">Prototype</a> library steps in to help clean up a little of the mess. One of base features of Prototype is making the creation of an object template nicer using the <code>Class.create()</code> function. This function takes the mess above, and structures it in a way that is more familiar to us class-based programmers.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript">Party = <span style="color: #003366; font-weight: bold;">Class</span>.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
Party.<span style="color: #006600;">prototype</span> = <span style="color: #66cc66;">&#123;</span>
  music: <span style="color: #003366; font-weight: bold;">true</span>,
&nbsp;
  initialize: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>people, beers<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> = people;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>  = beers;
  <span style="color: #66cc66;">&#125;</span>,
  <span style="color: #000066;">status</span>: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">beers</span>/<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">people</span> &gt; <span style="color: #CC0000;">5</span> ? <span style="color: #3366CC;">'kickin'</span> : <span style="color: #3366CC;">'lame'</span>;
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>This takes all of this logic and puts it into a nice construct that keeps it all together with a unified constructor function named <code>initialize</code>. Prototype does a little magic behind the scenes so that you can initialize your objects in the same way you previously did with the <code>new</code> operator.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> p1 = <span style="color: #003366; font-weight: bold;">new</span> Party<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">3</span>, <span style="color: #CC0000;">9</span><span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>Let&#8217;s take a look at what Prototype is doing here by sneaking a peak at the source.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> <span style="color: #003366; font-weight: bold;">Class</span> = <span style="color: #66cc66;">&#123;</span>
  create: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #006600;">initialize</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>, arguments<span style="color: #66cc66;">&#41;</span>;
    <span style="color: #66cc66;">&#125;</span>
  <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>We see here that the <code>Class</code> object consists of a single<br />
method, <code>create()</code>. When we call this method it returns us<br />
a function object.</p>
<p>Remember earlier when we created the <code>Party</code> constructor function using Javascript&#8217;s native syntax? It was essentially a function used to set the properties of new objects. </p>
<p><img src="http://assets.derekdevries.com/images/articles/2006/12/construct.gif" alt="Native Construction" /></p>
<p>This function returned by <code>create()</code> is actually this same constructor function. The difference is that it delegates setting the object properties to the <code>initialize</code> method.</p>
<p><img src="/images/articles/creating_classes_with_prototype/construct_proto.gif" alt="Prototype Construction" /></p>
<ol>
<li>
    You create a <code>Party</code> object with the <code>new</code><br />
    construct, and pass in the the properties we want to set. This executes<br />
    the constructor function that was created when we called<br />
    <code>Class.create()</code>.
  </li>
<li>
    The constructor function calls Javascript&#8217;s <code>apply</code><br />
    function on initialize. The <code>apply</code> function lets you<br />
    execute a method in the context of a different object. In this<br />
    case we need to execute the <code>initialize</code> method, but we<br />
    want it to use our newly constructed object from our constructor method<br />
    as <code>this</code> when it executes.
  </li>
</ol>
<p>While a little more complex behind the scenes, this code provides a nice abstraction that allows us to code in a style we are more familiar with. We welcome anything that makes our code a little easier to read or maintain.</p>
]]></content:encoded>
			<wfw:commentRss>http://derekdevries.com/2006/12/21/creating-classes-with-prototype/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
