<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-US">
  <title>The Thoughts of Robots - Home</title>
  <id>tag:autonomousmachine.com,2009:mephisto/</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  
  <link href="http://autonomousmachine.com/" rel="alternate" type="text/html" />
  <updated>2009-10-28T22:19:41Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/TheThoughtsOfRobots-Main" type="application/atom+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-10-28:206</id>
    <published>2009-10-28T22:18:00Z</published>
    <updated>2009-10-28T22:19:41Z</updated>
    <category term="Code" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/10/28/add-a-scope-for-easier-eager-loading" rel="alternate" type="text/html" />
    <title>Add an ActiveRecord scope for easier eager loading</title>
<content type="html">
            &lt;p&gt;In the optimization phase of a project, one of the first things I do is look for pages that could benefit from the use of ActiveRecord’s eager loading. Since &lt;code&gt;named_scopes&lt;/code&gt; were introduced in Rails 2.1, most of my model loading has involved a chain of scopes, and it is always frustrating to have to change&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;blogs&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;Blog&lt;/span&gt;.&lt;span class="FunctionName"&gt;published&lt;/span&gt;.&lt;span class="FunctionName"&gt;active&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;to&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;blogs&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;Blog&lt;/span&gt;.&lt;span class="FunctionName"&gt;published&lt;/span&gt;.&lt;span class="FunctionName"&gt;active&lt;/span&gt;.&lt;span class="FunctionName"&gt;scoped&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;include&lt;/span&gt; =&amp;gt; [&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;slugs&lt;/span&gt;, &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;topic&lt;/span&gt; =&amp;gt; &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;slugs&lt;/span&gt;])
&lt;/pre&gt;

&lt;p&gt;It’s a small difference, but one I’ve never really liked. It seems like an obvious choice would be to add a &lt;code&gt;named_scope&lt;/code&gt; to all subclasses of ActiveRecord::Base and have that handle the &lt;code&gt;:include&lt;/code&gt; parameter. I finally wrote the 11 lines of code required to do this today:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;module&lt;/span&gt; &lt;span class="TypeName"&gt;ActiveRecord&lt;/span&gt;
  &lt;span class="Keyword"&gt;module&lt;/span&gt; &lt;span class="TypeName"&gt;EagerLoadScope&lt;/span&gt;
    &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;self.included&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;base&lt;/span&gt;)
      base.&lt;span class="FunctionName"&gt;named_scope&lt;/span&gt; &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;eager_load&lt;/span&gt;, lambda { |*&lt;span class="Variable"&gt;inclusion&lt;/span&gt;|
        {&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;include&lt;/span&gt; =&amp;gt; inclusion}
      }
    &lt;span class="Keyword"&gt;end&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

&lt;span class="LibraryObject"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="FunctionName"&gt;Base&lt;/span&gt;.&lt;span class="FunctionName"&gt;class_eval&lt;/span&gt; { &lt;span class="Keyword"&gt;include&lt;/span&gt; &lt;span class="LibraryObject"&gt;ActiveRecord&lt;/span&gt;::&lt;span class="FunctionName"&gt;EagerLoadScope&lt;/span&gt; }
&lt;/pre&gt;

&lt;p&gt;And now that line looks like this:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;blogs&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;Blog&lt;/span&gt;.&lt;span class="FunctionName"&gt;published&lt;/span&gt;.&lt;span class="FunctionName"&gt;active&lt;/span&gt;.&lt;span class="FunctionName"&gt;eager_load&lt;/span&gt;(&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;slugs&lt;/span&gt;, &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;topic&lt;/span&gt; =&amp;gt; &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;slugs&lt;/span&gt;)
&lt;/pre&gt;

&lt;p&gt;Again, it’s a small improvement, but one I consider worthwhile.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-09-11:128</id>
    <published>2009-09-11T16:30:00Z</published>
    <updated>2009-09-11T16:31:47Z</updated>
    <category term="CSS" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/9/11/hiding-parts-of-a-view-while-in-development" rel="alternate" type="text/html" />
    <title>Hiding parts of a view while in development</title>
<content type="html">
            &lt;p&gt;Most of my recent projects have involved working with a front end developer, who would provide HTML/CSS templates with dummy content to be integrated into a Rails backend. During the process of wiring up the views, I often wish to hide a section of a template or partial until the models and controllers are setup to support them.&lt;/p&gt;

&lt;p&gt;I’ve started using this little view helper to hide code in a view, while providing a console message to remind me to go back and address the issue when the time is right:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;hide&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;&lt;span class="Keyword"&gt;&amp;amp;&lt;/span&gt;block&lt;/span&gt;)
  &lt;span class="Keyword"&gt;if&lt;/span&gt; &lt;span class="LibraryObject"&gt;Rails&lt;/span&gt;.&lt;span class="FunctionName"&gt;env&lt;/span&gt; &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;development&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
    match, file, line &lt;span class="Keyword"&gt;=&lt;/span&gt; caller.&lt;span class="FunctionName"&gt;first&lt;/span&gt;.&lt;span class="FunctionName"&gt;match&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;[&lt;/span&gt;^:&lt;span class="String"&gt;]&lt;/span&gt;&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;:&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;.+&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;).&lt;span class="FunctionName"&gt;to_a&lt;/span&gt;
    &lt;span class="LibraryObject"&gt;Rails&lt;/span&gt;.&lt;span class="FunctionName"&gt;logger&lt;/span&gt;.&lt;span class="FunctionName"&gt;info&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;** Hiding content on line &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;line&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt; of &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;file&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;It’s simple to use, especially in HAML:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;-&lt;/span&gt; hide &lt;span class="Keyword"&gt;do&lt;/span&gt;
  &lt;span class="Keyword"&gt;=&lt;/span&gt; some_view_code_that_will_not_execute
&lt;/pre&gt;

&lt;p&gt;The helper will print something like this to your console:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
&lt;span class="Keyword"&gt;**&lt;/span&gt; &lt;span class="Variable"&gt;Hiding&lt;/span&gt; content on line &lt;span class="Constant"&gt;14&lt;/span&gt; of &lt;span class="Keyword"&gt;/&lt;/span&gt;&lt;span class="Variable"&gt;Users&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;jimb&lt;span class="Keyword"&gt;/&lt;/span&gt;&lt;span class="Variable"&gt;Projects&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;demo&lt;span class="Keyword"&gt;/&lt;/span&gt;app&lt;span class="Keyword"&gt;/&lt;/span&gt;views&lt;span class="Keyword"&gt;/&lt;/span&gt;objects&lt;span class="Keyword"&gt;/&lt;/span&gt;show.&lt;span class="Entity"&gt;html&lt;/span&gt;.&lt;span class="Entity"&gt;haml&lt;/span&gt;
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-09-04:123</id>
    <published>2009-09-04T06:20:00Z</published>
    <updated>2009-09-04T06:31:56Z</updated>
    <category term="Code" />
    <category term="JavaScript" />
    <link href="http://autonomousmachine.com/2009/9/4/dollar-spec-is-a-tiny-javascript-spec-library" rel="alternate" type="text/html" />
    <title>Dollar Spec is a tiny JavaScript spec library</title>
<content type="html">
            &lt;p&gt;Dollar spec is a tiny JavaScript spec framework that gets out of the way and lets you get things done. It supports nested describes and before/after blocks, but that is about it. The standard set of expectations is small right now, but they are easy to add.&lt;/p&gt;

&lt;h3&gt;Why another JavaScript spec framework?&lt;/h3&gt;

&lt;p&gt;I wanted a small, pure-JS framework that supported a syntax I didn’t have to think too much to use. I wanted to only add as much syntax as was neccessary, and leave the rest to JavaScript. I wanted to run JavaScript tests from the console. I wanted a really extensible way to add new expectations. And I wanted minimal namespace usage. DollarSpec creates a single global entity, &lt;code&gt;$spec&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, it seemed like a good way to stretch my brain. I’ve been doing a lot of JavaScript development recently, and it was fun to apply some of the new techniques I’ve picked up.&lt;/p&gt;

&lt;h3&gt;OK Let’s See What You’ve Got&lt;/h3&gt;

&lt;p&gt;Here’s a sample spec for an awesome addition function:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.describe(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;my awesome addition function&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(spec) {

    &lt;span class="Keyword"&gt;var&lt;/span&gt; &lt;span class="FunctionName"&gt;add&lt;/span&gt; = &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
       &lt;span class="Keyword"&gt;var&lt;/span&gt; total &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="Number"&gt;0&lt;/span&gt;;
       &lt;span class="Keyword"&gt;for&lt;/span&gt; (&lt;span class="Keyword"&gt;var&lt;/span&gt; i &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="Number"&gt;0&lt;/span&gt;, l &lt;span class="JsOperator"&gt;=&lt;/span&gt; arguments.&lt;span class="LibraryConstant"&gt;length&lt;/span&gt;; i &lt;span class="JsOperator"&gt;&amp;lt;&lt;/span&gt; l; i&lt;span class="JsOperator"&gt;++&lt;/span&gt;) {
           total &lt;span class="JsOperator"&gt;+&lt;/span&gt;&lt;span class="JsOperator"&gt;=&lt;/span&gt; arguments[i];
       }
       &lt;span class="Keyword"&gt;return&lt;/span&gt; total;
    };

    spec.before(&lt;span class="Keyword"&gt;function&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; code in this block is run before each spec&lt;/span&gt;
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; and yes, describe blocks can be nested&lt;/span&gt;
    });

    spec.it(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;adds two numbers&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(expect) {
        expect(&lt;span class="LibraryFunction"&gt;add&lt;/span&gt;(&lt;span class="Number"&gt;2&lt;/span&gt;,&lt;span class="Number"&gt;2&lt;/span&gt;)).to.equal(&lt;span class="Number"&gt;4&lt;/span&gt;);
    });

    spec.it(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;adds three numbers&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(expect) {
        expect(&lt;span class="LibraryFunction"&gt;add&lt;/span&gt;(&lt;span class="Number"&gt;1&lt;/span&gt;,&lt;span class="Number"&gt;1&lt;/span&gt;,&lt;span class="Number"&gt;2&lt;/span&gt;)).to.equal(&lt;span class="Number"&gt;4&lt;/span&gt;);
    });

    &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; to show a failing spec&lt;/span&gt;
    spec.it(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;adds two numbers&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(expect) {
        expect(&lt;span class="LibraryFunction"&gt;add&lt;/span&gt;(&lt;span class="Number"&gt;1&lt;/span&gt;,&lt;span class="Number"&gt;1&lt;/span&gt;,&lt;span class="Number"&gt;2&lt;/span&gt;)).to.equal(&lt;span class="Number"&gt;5&lt;/span&gt;);
    });

    &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; to show a pending spec&lt;/span&gt;
    spec.it(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;casts and adds strings&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
});

&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.run();
&lt;/pre&gt;

&lt;p&gt;If you run this inside Firefox, you will get some messages in the Firebug console:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://autonomousmachine.com/assets/2009/9/4/Picture_29.png" alt="Filter results" /&gt;&lt;/p&gt;

&lt;p&gt;These message can be adjusted to your liking:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; To disable console output (defaults to true)&lt;/span&gt;
&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.opts.&lt;span class="TypeName"&gt;console&lt;/span&gt; &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="BuiltInConstant"&gt;false&lt;/span&gt;;

&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; Print a line to the console for each test (defaults to false)&lt;/span&gt;
&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.opts.verbose &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="BuiltInConstant"&gt;true&lt;/span&gt;;
&lt;/pre&gt;

&lt;p&gt;If you don’t want to use the console reporting, the results of all specs are collected and returned by &lt;code&gt;$spec.stats()&lt;/code&gt;. This makes it easy to do whatever you wish with the results.&lt;/p&gt;

&lt;h3&gt;Supported expectations&lt;/h3&gt;

&lt;p&gt;Keep in mind that any expectation can be negated using &lt;code&gt;not()&lt;/code&gt;. For example:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
expect(&lt;span class="Number"&gt;3&lt;/span&gt;).to.not().equal(&lt;span class="Number"&gt;8&lt;/span&gt;);
&lt;/pre&gt;

&lt;h4&gt;expect(actual).to.be(object)&lt;/h4&gt;

&lt;p&gt;Compares the expected and actual values using &lt;code&gt;===&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;expect(actual).to.beA(Object)&lt;/h4&gt;

&lt;p&gt;Verifies that &lt;code&gt;actual&lt;/code&gt; is an instance of &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;expect(actual).to.equal(expected)&lt;/h4&gt;

&lt;p&gt;Compares &lt;code&gt;actual&lt;/code&gt; and &lt;code&gt;expected&lt;/code&gt; using &lt;code&gt;==&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;expect(affectorFunction).to.change(affectedFunction)&lt;/h4&gt;

&lt;p&gt;Compares the value returned from calling &lt;code&gt;affectedFunction&lt;/code&gt; before and after calling &lt;code&gt;affectorFunction&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;expect(affectorFunction).to.change(affectedFunction).by(amount)&lt;/h4&gt;

&lt;p&gt;Compares the difference between the value returned from calling &lt;code&gt;affectedFunction&lt;/code&gt; before and after calling &lt;code&gt;affectorFunction&lt;/code&gt; to &lt;code&gt;amount&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;expect(callbackFunction).to.raiseError([verifyFunction])&lt;/h4&gt;

&lt;p&gt;Verifies that &lt;code&gt;callbackFunction&lt;/code&gt; raises an exception when called.&lt;/p&gt;

&lt;p&gt;Optionally will call &lt;code&gt;verifyFunction&lt;/code&gt; and pass in a raised exception for further inspection. &lt;code&gt;verifyFunction&lt;/code&gt; must return &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;Custom expectations&lt;/h3&gt;

&lt;p&gt;DollarSpec’s expectations are built from two pieces- verbs and matchers.&lt;/p&gt;

&lt;h4&gt;Verbs&lt;/h4&gt;

&lt;p&gt;A verb is a function that can be used in an expectation to define something about the parameters of what is expected. The most basic verbs simply define which matcher to use. Verbs must always return &lt;code&gt;this&lt;/code&gt; in order to allow chaining to work. Here is the verb definition for &lt;code&gt;equal&lt;/code&gt;:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.verb(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;equal&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(expected) {
    &lt;span class="Variable"&gt;this&lt;/span&gt;.set(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;matcher&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;equal&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
    &lt;span class="Variable"&gt;this&lt;/span&gt;.set(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;expected&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, expected);
    &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="Variable"&gt;this&lt;/span&gt;;
});
&lt;/pre&gt;

&lt;p&gt;This verb will cause the expectation to use the &lt;code&gt;equal&lt;/code&gt; matcher, and stores the value of &lt;code&gt;expected&lt;/code&gt; so it can be examined in the matcher later.&lt;/p&gt;

&lt;h4&gt;Matchers&lt;/h4&gt;

&lt;p&gt;Matchers are functions that determine if an expectation passes or fails. They can also define messages to be displayed to the user when a test fails. Here’s the &lt;code&gt;beA&lt;/code&gt; matcher:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="JsOperator"&gt;$&lt;/span&gt;spec.matcher(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;beA&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Keyword"&gt;function&lt;/span&gt;(result) {
  result.failure &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Expected an instance of &lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class="JsOperator"&gt;+&lt;/span&gt; &lt;span class="Variable"&gt;this&lt;/span&gt;.klass.&lt;span class="LibraryFunction"&gt;toString&lt;/span&gt;() &lt;span class="JsOperator"&gt;+&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;, but it was &lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class="JsOperator"&gt;+&lt;/span&gt; &lt;span class="JsOperator"&gt;typeof&lt;/span&gt;(&lt;span class="Variable"&gt;this&lt;/span&gt;.actual);
  result.negatedFailure &lt;span class="JsOperator"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;Expected instance of a class other than &lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class="JsOperator"&gt;+&lt;/span&gt; &lt;span class="Variable"&gt;this&lt;/span&gt;.klass.&lt;span class="LibraryFunction"&gt;toString&lt;/span&gt;() &lt;span class="JsOperator"&gt;+&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;, but it was one&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;

  &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="Variable"&gt;this&lt;/span&gt;.actual &lt;span class="JsOperator"&gt;instanceof&lt;/span&gt;(&lt;span class="Variable"&gt;this&lt;/span&gt;.klass);
});

&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;result.negatedFailure&lt;/code&gt; is the message to be displayed when the tests passes, but was used in a negated expectation using &lt;code&gt;not()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can see how the built-in &lt;a href="http://github.com/jim/dollar_spec/blob/master/src/dollar_spec/verbs.js"&gt;verbs&lt;/a&gt; and &lt;a href="http://github.com/jim/dollar_spec/blob/master/src/dollar_spec/matchers.js"&gt;matchers&lt;/a&gt; are implemented in the Github repository.&lt;/p&gt;

&lt;h3&gt;Dogfood for Dinner&lt;/h3&gt;

&lt;p&gt;Dollar Spec has a &lt;a href="http://github.com/jim/dollar_spec/tree/master/spec"&gt;suite of specs&lt;/a&gt;, written using Dollar Spec and powered by &lt;a href="http://github.com/jim/diligence/tree/master"&gt;diligence&lt;/a&gt;, a tiny remote JavaScript test runner library. It requires &lt;a href="http://tinyclouds.org/node/"&gt;Node.js&lt;/a&gt;, which is awesome. Go check it out if you haven’t yet.&lt;/p&gt;

&lt;p&gt;To run the specs:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
cd spec
node suite.&lt;span class="Entity"&gt;js&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Open a browser and go to &lt;code&gt;localhost:5678&lt;/code&gt;. You should see something like the following:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://autonomousmachine.com/assets/2009/9/4/Picture_31.png" alt="Dollar Spec Results" /&gt;&lt;/p&gt;

&lt;p&gt;Diligence is still under development, but its working well so far on a few JavaScript projects I’m working on. I’ll post more about it as it matures.&lt;/p&gt;

&lt;h2&gt;More information&lt;/h2&gt;

&lt;p&gt;There is a bit more info in the &lt;a href="http://github.com/jim/diligence/tree/master"&gt;README on Github&lt;/a&gt;. Feedback is always welcome!&lt;/p&gt;

&lt;h2&gt;Alternatives&lt;/h2&gt;

&lt;p&gt;You should probably take a look at &lt;a href="http://github.com/nathansobo/screw-unit/tree/master"&gt;ScrewUnit&lt;/a&gt; if you’re looking for a more complete/mature BDD framework.&lt;/p&gt;

&lt;h4&gt;Now go write some specs.&lt;/h4&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-06-19:99</id>
    <published>2009-06-19T20:11:00Z</published>
    <updated>2009-06-22T19:26:45Z</updated>
    <category term="Code" />
    <category term="Installation" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/6/19/install-passenger-and-nginx-from-source-on-ubuntu" rel="alternate" type="text/html" />
    <title>Install Passenger and nginx from source on Ubuntu</title>
<content type="html">
            &lt;p&gt;Passenger 2.2.3 came out this week, and while up until now I was using the &lt;a href="http://www.modrails.com/install.html"&gt;Ubuntu packages&lt;/a&gt; from &lt;a href="http://wiki.brightbox.co.uk/docs:phusion-passenger"&gt;Brightbox&lt;/a&gt; (great blokes, really), I was having some problems with serving caches files and I didn’t want to wait for the package to be updated. The package itself can be found &lt;a href="http://github.com/brightbox/nginx-brightbox/tree/master"&gt;on Github&lt;/a&gt; if you’d like to see it in more detail.&lt;/p&gt;

&lt;p&gt;I used a combination of two guides to get everything working: &lt;a href="http://articles.slicehost.com/2007/10/16/ubuntu-lts-installing-nginx"&gt;one at Slicehost’s wiki&lt;/a&gt; and another &lt;a href="http://www.johnnypez.com/web-design-and-development/compiling-nginx-with-phusion-passenger-on-osx/"&gt;blog post&lt;/a&gt; by someone whose name may or may not be Johnny Pez.&lt;/p&gt;

&lt;p&gt;It can be a little confusing to jump back and forth between the two, so I’ve posted a compiled set of steps here. After working through this article, you can continue on with the &lt;a href="http://articles.slicehost.com/2007/10/17/ubuntu-lts-adding-an-nginx-init-script"&gt;second Slicehost article&lt;/a&gt; for setting up the rest of your environment.&lt;/p&gt;

&lt;h2&gt;Passenger&lt;/h2&gt;

&lt;p&gt;I’m going to assume you have Ruby and RubyGems installed. If not, &lt;a href="http://articles.slicehost.com/2009/1/6/ubuntu-intrepid-ruby-on-rails"&gt;do that first&lt;/a&gt; before continuing.&lt;/p&gt;

&lt;p&gt;Install the Passenger gem:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
sudo gem install passenger
&lt;/pre&gt;

&lt;p&gt;Then you need to build the nginx extension, which will be needed when we compile it.&lt;/p&gt;

&lt;pre class="blackboard"&gt;
cd &lt;span class="String"&gt;&lt;span class="String"&gt;`&lt;/span&gt;passenger-config --root&lt;span class="String"&gt;`&lt;/span&gt;&lt;/span&gt;
sudo rake nginx
&lt;/pre&gt;

&lt;h2&gt;nginx&lt;/h2&gt;

&lt;p&gt;You’ll need to install nginx’s prerequisites:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
sudo apt&lt;span class="Keyword"&gt;-&lt;/span&gt;get install libpcre3 libpcre3&lt;span class="Keyword"&gt;-&lt;/span&gt;dev libpcrecpp0 libssl&lt;span class="Keyword"&gt;-&lt;/span&gt;dev
&lt;/pre&gt;

&lt;p&gt;Then, download the nginx source and unpack it. Everyone seems to be using the legacy stable version of nginx, and I haven’t tried any of the newer ones yet.&lt;/p&gt;

&lt;pre class="blackboard"&gt;
wget http&lt;span class="Constant"&gt;&lt;span class="Constant"&gt;:&lt;/span&gt;/&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;sysoev.&lt;span class="Entity"&gt;ru&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;nginx&lt;span class="Keyword"&gt;/&lt;/span&gt;nginx&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Constant"&gt;0.6&lt;/span&gt;.&lt;span class="Constant"&gt;37&lt;/span&gt;.&lt;span class="Entity"&gt;tar&lt;/span&gt;.&lt;span class="Entity"&gt;gz&lt;/span&gt;
tar xzvf nginx&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Constant"&gt;0.6&lt;/span&gt;.&lt;span class="Constant"&gt;37&lt;/span&gt;.&lt;span class="Entity"&gt;tar&lt;/span&gt;.&lt;span class="Entity"&gt;gz&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Now we need to configure nginx.&lt;/p&gt;

&lt;pre class="blackboard"&gt;
cd nginx&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Constant"&gt;0.6&lt;/span&gt;.&lt;span class="Constant"&gt;37&lt;/span&gt;
.&lt;span class="Keyword"&gt;/&lt;/span&gt;configure &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;sbin&lt;span class="Keyword"&gt;-&lt;/span&gt;path&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;usr&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;local&lt;span class="Keyword"&gt;/&lt;/span&gt;sbin &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;with&lt;span class="Keyword"&gt;-&lt;/span&gt;http_ssl_module  \
&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;add&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;module&lt;/span&gt;&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;`&lt;/span&gt;passenger-config --root&lt;span class="String"&gt;`&lt;/span&gt;&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;ext&lt;span class="Keyword"&gt;/&lt;/span&gt;nginx
&lt;/pre&gt;

&lt;p&gt;You can change the configure flags if you want (see &lt;a href="http://articles.slicehost.com/2007/10/16/ubuntu-lts-installing-nginx#comment-518"&gt;the comments&lt;/a&gt; on the Slicehost article), but I’ve left them alone so the rest of Slicehost’s articles can be followed without modification.&lt;/p&gt;

&lt;p&gt;Assuming the configure succeeded, all that’s left is to compile and install.&lt;/p&gt;

&lt;pre class="blackboard"&gt;
make
sudo make install
&lt;/pre&gt;

&lt;h2&gt;All Systems Go&lt;/h2&gt;

&lt;p&gt;To make sure everything is working, start nginx:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
sudo &lt;span class="Keyword"&gt;/&lt;/span&gt;usr&lt;span class="Keyword"&gt;/&lt;/span&gt;local&lt;span class="Keyword"&gt;/&lt;/span&gt;sbin&lt;span class="Keyword"&gt;/&lt;/span&gt;nginx
&lt;/pre&gt;

&lt;p&gt;Visit your server’s IP, and you should see the nginx welcome screen. This is good! Kill nginx:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
sudo kill &lt;span class="String"&gt;&lt;span class="String"&gt;`&lt;/span&gt;cat /usr/local/nginx/logs/nginx.pid&lt;span class="String"&gt;`&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Now &lt;a href="http://articles.slicehost.com/2007/10/17/ubuntu-lts-adding-an-nginx-init-script"&gt;continue on with the Slicehost series&lt;/a&gt; to complete your nginx configuration. You’ll also probably want to &lt;a href="http://articles.slicehost.com/2009/3/4/ubuntu-intrepid-nginx-from-source-layout"&gt;mirror the default configuration layout&lt;/a&gt; (these articles specify Intrepid, but the steps will be pretty close on most recent versions of Ubuntu).&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-06-05:97</id>
    <published>2009-06-05T05:51:00Z</published>
    <updated>2009-06-05T06:01:31Z</updated>
    <category term="Code" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/6/5/interrogation-creates-boolean-accessors-from-named_scope-declarations" rel="alternate" type="text/html" />
    <title>Interrogation creates boolean accessors from named_scope declarations</title>
<content type="html">
            &lt;p&gt;This is a little hack I came up with today when I got tired of duplicating logic in scopes and accessor methods. If I define a method in a model like this:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;Submarine&lt;span class="InheritedClassName"&gt; &lt;span class="InheritedClassName"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;
  named_scope &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;on_surface&lt;/span&gt;, &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;conditions&lt;/span&gt; =&amp;gt; {&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;location&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;surface&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;}
  named_scope &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;submerged&lt;/span&gt;, &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;conditions&lt;/span&gt; =&amp;gt; {&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;location&lt;/span&gt; =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;under water&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;}
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;I think I should be able to interrogate the model to see if it is in these scopes without needing to define Submarine#on_surface? or Submarine#submerged? This is what I want to be able to do:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
sub &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="LibraryObject"&gt;Submarine&lt;/span&gt;.&lt;span class="FunctionName"&gt;first&lt;/span&gt; =&amp;gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&amp;lt;Submarine id: 1, location: 'surface'&amp;gt;&lt;/span&gt;
sub.&lt;span class="FunctionName"&gt;on_surface?&lt;/span&gt; =&amp;gt; &lt;span class="BuiltInConstant"&gt;true&lt;/span&gt;
sub.&lt;span class="FunctionName"&gt;submerged?&lt;/span&gt; =&amp;gt; &lt;span class="BuiltInConstant"&gt;false&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Interrogation will allow for exactly this. The first version would fire a database query on each call, but I’ve since added in an attempt at parsing simple conditions against a model’s attributes, with a fallback to the database for complicated conditions.&lt;/p&gt;

&lt;h2&gt;One last thing&lt;/h2&gt;

&lt;p&gt;The largest problem with this approach is an object’s state is not always in sync with the database. I’ve thought about this a lot, and at this point I think it’s best to raise an exception when using these methods on dirty objects. I think handling dirty objects is beyond the scope of this little hack.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/jim/interrogation/tree/master"&gt;The code is on Github&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-05-30:95</id>
    <published>2009-05-30T17:07:00Z</published>
    <updated>2009-05-30T17:15:20Z</updated>
    <category term="Code" />
    <category term="Mac" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/5/30/duplicate-a-terminal-window-with-ruby-and-appscript" rel="alternate" type="text/html" />
    <title>Duplicate a Mac OS X Terminal window with Ruby and Appscript</title>
<content type="html">
            &lt;p&gt;Even though OS X’s Terminal now supports multiple tabs, I still find myself using lots of separate windows so I can see everything at once. A common pain point was opening a second Terminal window and changing to the current working directory (I do this a lot from the root of Rails apps: one window for running the server, and one for git and other work). I’ve since written a little Ruby script that essentially duplicates the current Terminal window. Save the following as &lt;code&gt;dup&lt;/code&gt; and put it somewhere on your path:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;!/usr/bin/env ruby&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt;&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; Duplicates a terminal window, optionally running the passed command&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; % dup (creates new window)&lt;/span&gt;
&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; % dup command arg1 arg 2 (new window, plus runs passed command)&lt;/span&gt;

&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;rubygems&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;appscript&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="Keyword"&gt;include&lt;/span&gt; &lt;span class="Variable"&gt;Appscript&lt;/span&gt;

&lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;quote&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;command&lt;/span&gt;)
  command.&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\$&lt;/span&gt;`&lt;span class="StringInterpolation"&gt;\\&lt;/span&gt;!&amp;quot;&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;){|&lt;span class="Variable"&gt;m&lt;/span&gt;|&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\\&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\\&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;m&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;}
&lt;span class="Keyword"&gt;end&lt;/span&gt;

path &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Variable"&gt;ENV&lt;/span&gt;[&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;PWD&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;]
terminal &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="FunctionName"&gt;app&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Terminal&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;)
coords &lt;span class="Keyword"&gt;=&lt;/span&gt; terminal.&lt;span class="FunctionName"&gt;windows&lt;/span&gt;.&lt;span class="FunctionName"&gt;first&lt;/span&gt;.&lt;span class="FunctionName"&gt;position&lt;/span&gt;.&lt;span class="FunctionName"&gt;get&lt;/span&gt;
dimensions &lt;span class="Keyword"&gt;=&lt;/span&gt; terminal.&lt;span class="FunctionName"&gt;windows&lt;/span&gt;.&lt;span class="FunctionName"&gt;first&lt;/span&gt;.&lt;span class="FunctionName"&gt;size&lt;/span&gt;.&lt;span class="FunctionName"&gt;get&lt;/span&gt;

command &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;cd &lt;span class="StringInterpolation"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="FunctionName"&gt;quote&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;path&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; clear&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
command &lt;span class="Keyword"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="FunctionName"&gt;quote&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;&lt;span class="Variable"&gt;ARGV&lt;/span&gt;&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;.&lt;/span&gt;&lt;span class="FunctionName"&gt;join&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;(&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt; &lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;)&lt;/span&gt;&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; &lt;span class="Keyword"&gt;unless&lt;/span&gt; &lt;span class="Variable"&gt;ARGV&lt;/span&gt;.&lt;span class="FunctionName"&gt;empty?&lt;/span&gt;

terminal.&lt;span class="FunctionName"&gt;do_script&lt;/span&gt;(command)
terminal.&lt;span class="FunctionName"&gt;windows&lt;/span&gt;.&lt;span class="FunctionName"&gt;first&lt;/span&gt;.&lt;span class="FunctionName"&gt;size&lt;/span&gt;.&lt;span class="FunctionName"&gt;set&lt;/span&gt;([dimensions[&lt;span class="Number"&gt;0&lt;/span&gt;], dimensions[&lt;span class="Number"&gt;1&lt;/span&gt;]])
terminal.&lt;span class="FunctionName"&gt;windows&lt;/span&gt;.&lt;span class="FunctionName"&gt;first&lt;/span&gt;.&lt;span class="FunctionName"&gt;position&lt;/span&gt;.&lt;span class="FunctionName"&gt;set&lt;/span&gt;([coords[&lt;span class="Number"&gt;0&lt;/span&gt;] &lt;span class="Keyword"&gt;+&lt;/span&gt; dimensions[&lt;span class="Number"&gt;0&lt;/span&gt;] &lt;span class="Keyword"&gt;+&lt;/span&gt; &lt;span class="Number"&gt;10&lt;/span&gt;, coords[&lt;span class="Number"&gt;1&lt;/span&gt;]])
&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://appscript.sourceforge.net/"&gt;Appscript&lt;/a&gt; is required, but it is only a &lt;code&gt;sudo gem install appscript&lt;/code&gt; away. The script will pass and arguments passed to it to the new terminal window. One common use for me is &lt;code&gt;dup script/server&lt;/code&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-04-23:85</id>
    <published>2009-04-23T18:54:00Z</published>
    <updated>2009-04-23T18:54:51Z</updated>
    <category term="Code" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/4/23/reloading-rails-plugins-in-development-mode" rel="alternate" type="text/html" />
    <title>Reloading Rails plugins in development mode</title>
<content type="html">
            &lt;p&gt;In Rails 2.3 you can cause your plugins to be reloaded with each request by adding this to your development.rb file:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
config.&lt;span class="FunctionName"&gt;reload_plugins&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="BuiltInConstant"&gt;true&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;This was possible before, but it required altering &lt;code&gt;Dependencies.load_once_paths&lt;/code&gt; and other black magic. I’ve always had issues with these approaches, and so it’s nice to see official support for such a useful feature.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-04-01:74</id>
    <published>2009-04-01T00:54:00Z</published>
    <updated>2009-04-01T00:55:21Z</updated>
    <category term="Code" />
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/4/1/carmen-a-rails-plugin-for-geographic-names-and-abbreviations" rel="alternate" type="text/html" />
    <title>Carmen: A Rails plugin for geographic names and abbreviations</title>
<content type="html">
            &lt;p&gt;On a recent project I became tired of &lt;a href="http://github.com/sprsquish/state_select/tree/master"&gt;state select&lt;/a&gt; and &lt;a href="http://github.com/rails/country_select/tree/master"&gt;country select&lt;/a&gt;, the ubiquitous Rails plugins for providing a list of state or country names in views. I didn’t like that their data was defined in code. Or that their data was defined in the view, so validating a model field against them was messy. &lt;/p&gt;

&lt;p&gt;I started to refactor country select by moving the list of countries into a Geography module. But then I needed a list of states, and their abbreviations, and it struck me as odd that I needed two plugins that worked differently in order to provide what seemed to be some very basic functionality to my app.&lt;/p&gt;

&lt;h2&gt;Well she sneaks around the world, from Kiev to Carolina…&lt;/h2&gt;

&lt;p&gt;&lt;a href="http://github.com/jim/carmen/tree/master"&gt;Carmen&lt;/a&gt; is the result of my frustration: a small unified library that handles country and state names and abbreviations. It stores its data lists in YAML, so it’s easy to edit and add new state lists.&lt;/p&gt;

&lt;p&gt;Carmen’s primary use is providing a list of countries (or states within country), and, optionally, their abbreviations. &lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;states&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;US&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; [[&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Alabama&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;AL&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;], [&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Arkansas&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;AR&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;], ... ]
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;state_names&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;US&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; [&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Alabama&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Arkansas&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, ... ]
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;state_codes&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;US&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; [&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;AL&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;AR&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, ... ]
&lt;/pre&gt;

&lt;p&gt;Similar methods are available for countries. It also has some convenience methods to assist in converting between names and abbreviations:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;country_name&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;US&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;United States&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;country_code&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Canada&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;CA&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;state_code&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Manitoba&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;CA&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;MB&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;state_name&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;AZ&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;US&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;) =&amp;gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Arizona&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Any of the state methods that require a country code will use a default country code if none is supplied. This defaults to ‘US’, but can be easily changed for those outside the US:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;.&lt;span class="FunctionName"&gt;default_country&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;CA&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;And of course, it’s simple to use Carmen in your model validations:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;Address&lt;span class="InheritedClassName"&gt; &lt;span class="InheritedClassName"&gt;&amp;lt;&lt;/span&gt; ActiveRecord::Base&lt;/span&gt;&lt;/span&gt;
  validates_inclusion_of &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;country&lt;/span&gt;, &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;in&lt;/span&gt; =&amp;gt; &lt;span class="LibraryObject"&gt;Carmen&lt;/span&gt;::&lt;span class="FunctionName"&gt;country_codes&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Carmen also supplies view helpers that work the same as those in country select or state select, so it should be a drop in replacement in most cases. I’ve &lt;a href="http://jim.github.com/carmen/"&gt;posted the docs&lt;/a&gt;, and the code is &lt;a href="http://github.com/jim/carmen/tree/master"&gt;on Github&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-02-17:51</id>
    <published>2009-02-17T06:52:00Z</published>
    <updated>2009-07-24T03:09:27Z</updated>
    <category term="Code" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/2/17/using-will_paginate-with-datamapper-and-sinatra" rel="alternate" type="text/html" />
    <title>Using will_paginate with DataMapper and Sinatra</title>
<content type="html">
            &lt;p&gt;Yes, it can be done. But not using prebuilt gems, since we need to use the &lt;a href="http://github.com/mislav/will_paginate/tree/agnostic"&gt;agnostic branch of the will paginate repository&lt;/a&gt; for DataMapper support. Here’s what I did to install the necessary version of will_paginate:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
git clone git&lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;/&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;github.&lt;span class="FunctionName"&gt;com&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;mislav&lt;span class="Keyword"&gt;/&lt;/span&gt;will_paginate.&lt;span class="FunctionName"&gt;git&lt;/span&gt;
cd will_paginate
git checkout &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;track &lt;span class="Keyword"&gt;-&lt;/span&gt;b agnostic origin&lt;span class="Keyword"&gt;/&lt;/span&gt;agnostic &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; check out agnostic branch&lt;/span&gt;
gem build will_paginate.&lt;span class="FunctionName"&gt;gemspec&lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; build the gem&lt;/span&gt;
sudo gem install will_paginate&lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Number"&gt;3.0&lt;/span&gt;.&lt;span class="Number"&gt;0&lt;/span&gt;.&lt;span class="FunctionName"&gt;gem&lt;/span&gt; &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;#&lt;/span&gt; install the gem&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;You could also create a fork, merge the code from agnostic into the master branch, push to GitHub, and have your own username-will_paginate gem to install. I decided this wasn’t worth the effort, at least for the project I was working on.&lt;/p&gt;

&lt;h2&gt;Models&lt;/h2&gt;

&lt;p&gt;When will_paginate is required, it does some checks to see if it is running under Rails or Merb, and if so, mixes the class methods and view helpers into the base classes for you. If you’re working with another framework (such as Sinatra), you’ll need to require the finders and view helpers on your own:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;will_paginate&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;will_paginate/finders/data_mapper&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;At this point, &lt;code&gt;YourModelName#paginate&lt;/code&gt; will return paginated collections. But we need to be able to render pagination links as well…&lt;/p&gt;

&lt;h2&gt;Views&lt;/h2&gt;

&lt;p&gt;will_paginate has abstracted out the view logic into a few modules, and all it depends on is the implementation of the &lt;code&gt;WillPaginate::ViewHelpers::LinkRenderer#url&lt;/code&gt; method. To get the view helper working, I added the following to the bottom of my Sinatra app’s file (based on the supplied &lt;a href="http://github.com/mislav/will_paginate/blob/df23a9e0a2cef0b66a0f525c45adaa8afe1249fb/lib/will_paginate/view_helpers/merb.rb"&gt;the merb adapter&lt;/a&gt;):&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;will_paginate/view_helpers/base&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="Keyword"&gt;require&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;will_paginate/view_helpers/link_renderer&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;

&lt;span class="LibraryObject"&gt;WillPaginate&lt;/span&gt;::&lt;span class="FunctionName"&gt;ViewHelpers&lt;/span&gt;::&lt;span class="FunctionName"&gt;LinkRenderer&lt;/span&gt;.&lt;span class="FunctionName"&gt;class_eval&lt;/span&gt; &lt;span class="Keyword"&gt;do&lt;/span&gt;
  &lt;span class="Keyword"&gt;protected&lt;/span&gt;
  &lt;span class="Keyword"&gt;def&lt;/span&gt; &lt;span class="FunctionName"&gt;url&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;page&lt;/span&gt;)
    url &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;@&lt;/span&gt;template&lt;/span&gt;.&lt;span class="FunctionName"&gt;request&lt;/span&gt;.&lt;span class="FunctionName"&gt;url&lt;/span&gt;
    &lt;span class="Keyword"&gt;if&lt;/span&gt; page &lt;span class="Keyword"&gt;==&lt;/span&gt; &lt;span class="Number"&gt;1&lt;/span&gt;
&lt;span class="Comment"&gt;      &lt;span class="Comment"&gt;#&lt;/span&gt; strip out page param and trailing ? if it exists&lt;/span&gt;
      url.&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;page=&lt;span class="String"&gt;&lt;span class="String"&gt;[&lt;/span&gt;0-9&lt;span class="String"&gt;]&lt;/span&gt;&lt;/span&gt;+&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;).&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="StringInterpolation"&gt;\?&lt;/span&gt;$&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;)
    &lt;span class="Keyword"&gt;else&lt;/span&gt;
      &lt;span class="Keyword"&gt;if&lt;/span&gt; url &lt;span class="Keyword"&gt;=~&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;page=&lt;span class="String"&gt;&lt;span class="String"&gt;[&lt;/span&gt;0-9&lt;span class="String"&gt;]&lt;/span&gt;&lt;/span&gt;+&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;
        url.&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;page=&lt;span class="String"&gt;&lt;span class="String"&gt;[&lt;/span&gt;0-9&lt;span class="String"&gt;]&lt;/span&gt;&lt;/span&gt;+&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;page=&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;page&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
      &lt;span class="Keyword"&gt;else&lt;/span&gt;
        url &lt;span class="Keyword"&gt;+&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;?page=&lt;span class="StringInterpolation"&gt;&lt;span class="StringInterpolation"&gt;#{&lt;/span&gt;page&lt;span class="StringInterpolation"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
      &lt;span class="Keyword"&gt;end&lt;/span&gt;      
    &lt;span class="Keyword"&gt;end&lt;/span&gt;
  &lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;span class="Keyword"&gt;end&lt;/span&gt;

helpers &lt;span class="LibraryObject"&gt;WillPaginate&lt;/span&gt;::&lt;span class="FunctionName"&gt;ViewHelpers&lt;/span&gt;::&lt;span class="FunctionName"&gt;Base&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;The code assumes the page number will be in the query string; it could easily be modified to work in other situations.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-02-07:42</id>
    <published>2009-02-07T15:34:00Z</published>
    <updated>2009-02-07T15:35:19Z</updated>
    <category term="Code" />
    <category term="Rails" />
    <link href="http://autonomousmachine.com/2009/2/7/git-error-fatal-reference-is-not-a-tree-error" rel="alternate" type="text/html" />
    <title>Git Error: fatal: reference is not a tree error</title>
<content type="html">
            &lt;p&gt;If you do what I do and keep your Rails plugins in separate git repos &lt;a href="http://edspencer.net/2008/04/git-clone-vs-git-submodule.html"&gt;using git submodules&lt;/a&gt;, you will encounter a git error if you’ve made changes to the local repositories for a plugin and try to deploy without pushing the changes up to the server:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fatal: reference is not a tree: 17f1db7f33c986f0d4a9dc0c5846322095cda96b&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To resolve this, &lt;code&gt;cd&lt;/code&gt; into the relevant directory (vendor/plugins/whatever/) and push to the repo. It seems obvious, but I’ve made this mistake once or twice.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-02-03:38</id>
    <published>2009-02-03T03:27:00Z</published>
    <updated>2009-02-03T03:46:45Z</updated>
    <category term="PHP" />
    <link href="http://autonomousmachine.com/2009/2/3/fitzgerald-update-before-filters-and-senddownload" rel="alternate" type="text/html" />
    <title>Fitzgerald update: before filters and sendDownload</title>
<content type="html">
            &lt;p&gt;Fitzgerald has been running at least two websites for a little while now, but I’ve had no time to do much with it for the last month. There were a few feature additions as a result of a client site I was working one, and now’s as good a time as any to mention them. Especially seeing as the official docs are a couple blogs posts for now.&lt;/p&gt;

&lt;h2&gt;Before Filters&lt;/h2&gt;

&lt;p&gt;One feature of Fitzgerald I added to the repo a while ago but failed to document is the ability to add before filters to actions. Anyone that’s used an MVC web framework will probably be familiar with this concept; for those who aren’t, the idea is to assign one or more methods to be called before a given action. The most common use case for this is probably ensuring that a use is logged in before viewing a given page.&lt;/p&gt;

&lt;p&gt;It should be noted that adding after filter would be trivial; however, I’m not sure there really is a need for them in a framework of this size. So I’ve left them out for now.&lt;/p&gt;

&lt;p&gt;Fitzgerald’s implementation of filters is a little different than most in order allow a filter to return text to the browser or redirect. Any time a filter returns anything, the filter chain is halted and the result is sent to the browser.&lt;/p&gt;

&lt;p&gt;Filters are defined using a similar syntax to binding actions to routes, and are executed in the order they are defined:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&amp;lt;?php
&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;ApplicationWithAFilter&lt;/span&gt; &lt;span class="Keyword"&gt;extends&lt;/span&gt; &lt;span class="InheritedClassName"&gt;Fitzgerald&lt;/span&gt; {

    &lt;span class="Keyword"&gt;protected &lt;/span&gt;&lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="FunctionName"&gt;verify_user&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; redirect unauthorized users to the home page&lt;/span&gt;
        &lt;span class="Keyword"&gt;if&lt;/span&gt;(&lt;span class="Keyword"&gt;!&lt;/span&gt;&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;isAllowed()) {
            &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;redirect(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
        }
    }

    &lt;span class="Keyword"&gt;protected &lt;/span&gt;&lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="FunctionName"&gt;get_protected&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; display some secrets&lt;/span&gt;
    }

    &lt;span class="Keyword"&gt;protected &lt;/span&gt;&lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="FunctionName"&gt;post_protected&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; make some secrets&lt;/span&gt;
    }

}

&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;app&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="Keyword"&gt;new&lt;/span&gt; &lt;span class="LibraryObject"&gt;ApplicationWithAFilter&lt;/span&gt;();

&lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; Define a before filter to be executed before one or more actions&lt;/span&gt;
&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;app&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;before(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;get_protected|post_protected&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;verify_user&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);

?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Just add a method with the correct name to your &lt;code&gt;Application&lt;/code&gt; definition, and you are all set. I’ve updated &lt;a href="http://github.com/jim/fitzgerald/blob/cb06435a740c66bfdb5813c00858f33503daa231/example.php"&gt;example.php&lt;/a&gt; in the repo to include usage of a before filter.&lt;/p&gt;

&lt;h2&gt;sendDownload&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sendDownload&lt;/code&gt; is really just a brute force version of &lt;code&gt;sendFile&lt;/code&gt; that does everything it can to persuade a browser to download a file instead of opening it inline. There are no guarantees this technique will work all of the time, but in my testing, &lt;code&gt;sendDownload&lt;/code&gt; contained the correct black magic to force Internet Explorer to behave itself.&lt;/p&gt;

&lt;p&gt;Since I haven’t documented either of these methods yet, here’s how to use &lt;code&gt;sendFile&lt;/code&gt; and &lt;code&gt;sendDownload&lt;/code&gt;:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&amp;lt;?php
&lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;ApplicationWithDownloads&lt;/span&gt; &lt;span class="Keyword"&gt;extends&lt;/span&gt; &lt;span class="InheritedClassName"&gt;Fitzgerald&lt;/span&gt; {
    &lt;span class="Keyword"&gt;public &lt;/span&gt;&lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="FunctionName"&gt;get_pdf&lt;/span&gt;() {
        &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;sendFile(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;filename_to_send_as.pdf&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;application/pdf&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;path(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;pdf/awesome.pdf&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;));
    }
    &lt;span class="Keyword"&gt;public &lt;/span&gt;&lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="FunctionName"&gt;get_pdf_for_dowload&lt;/span&gt;() {
        &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;sendDownload(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;filename_to_send_as.pdf&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;this&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;path(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;pdf/awesome.pdf&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;));
    }
}
?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;sendFile&lt;/code&gt; takes three arguments: the filename to send the file as, the content type to set in the headers, and the path to the local file to send. Note the call to &lt;code&gt;path&lt;/code&gt;, which simply appends its argument to the app root (which is defined as one level up from &lt;code&gt;fitzgerald.php&lt;/code&gt;). Usage of &lt;code&gt;sendDownload&lt;/code&gt; is the same, except it doesn’t take a content type argument.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-01-24:35</id>
    <published>2009-01-24T20:08:00Z</published>
    <updated>2009-06-22T19:27:00Z</updated>
    <category term="Installation" />
    <category term="Mac" />
    <link href="http://autonomousmachine.com/2009/1/24/building-sphinx-0-9-9rc1-with-postgresql-8-3-support-on-mac-os-10-5" rel="alternate" type="text/html" />
    <title>Building Sphinx 0.9.9RC1 with PostgreSQL 8.3 support via MacPorts on Mac OS 10.5</title>
<content type="html">
            &lt;p&gt;Up until today I was using the version of &lt;a href="http://www.sphinxsearch.com"&gt;Sphinx&lt;/a&gt; provided by &lt;a href="http://www.macports.org"&gt;MacPorts&lt;/a&gt;, 0.9.8, as a lot of developers probably are. It installed painlessly, but I’m in the process of auditioning several different search engine options for a project and I wanted to make sure I was using the latest in my comparisons. I’m also using a PostgreSQL 8.3 database, which requires specifying a few additional options to configure.&lt;/p&gt;

&lt;p&gt;Here are the commands I used to install Sphinx 0.9.9RC1 (run inside the Sphinx source directory after you &lt;a href="http://www.sphinxsearch.com/downloads.html"&gt;download it&lt;/a&gt;):&lt;/p&gt;

&lt;pre class="blackboard"&gt;
export &lt;span class="Variable"&gt;LDFLAGS&lt;/span&gt;&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;-L/usr/lib&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
.&lt;span class="Keyword"&gt;/&lt;/span&gt;configure &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;with&lt;span class="Keyword"&gt;-&lt;/span&gt;pgsql &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;with&lt;span class="Keyword"&gt;-&lt;/span&gt;pgsql&lt;span class="Keyword"&gt;-&lt;/span&gt;includes&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;opt&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;local&lt;span class="Keyword"&gt;/&lt;/span&gt;&lt;span class="Keyword"&gt;include&lt;/span&gt;&lt;span class="Keyword"&gt;/&lt;/span&gt;postgresql83&lt;span class="Keyword"&gt;/&lt;/span&gt; \
    &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;with&lt;span class="Keyword"&gt;-&lt;/span&gt;pgsql&lt;span class="Keyword"&gt;-&lt;/span&gt;libs&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;opt&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;local&lt;span class="Keyword"&gt;/&lt;/span&gt;lib&lt;span class="Keyword"&gt;/&lt;/span&gt;postgresql83&lt;span class="Keyword"&gt;/&lt;/span&gt;
make
sudo make install
&lt;/pre&gt;

&lt;p&gt;The first line ensures that make can &lt;a href="http://maximkulkin.blogspot.com/2008/05/installing-sphinx-on-mac-os-leopard.html"&gt;find the system iconv libraries&lt;/a&gt;. Otherwise, it’s a pretty straight forward process.&lt;/p&gt;

&lt;h2&gt;Update&lt;/h2&gt;

&lt;p&gt;A simpler solution is to make sure that the &lt;code&gt;pg_config&lt;/code&gt; executable can be found on your path. Add &lt;code&gt;/opt/local/lib/postgresql83/bin/&lt;/code&gt; to your path, and then you can simply use:&lt;/p&gt;

&lt;pre class="blackboard"&gt;
export &lt;span class="Variable"&gt;LDFLAGS&lt;/span&gt;&lt;span class="Keyword"&gt;=&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;-L/usr/lib&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
.&lt;span class="Keyword"&gt;/&lt;/span&gt;configure &lt;span class="Keyword"&gt;-&lt;/span&gt;&lt;span class="Keyword"&gt;-&lt;/span&gt;with&lt;span class="Keyword"&gt;-&lt;/span&gt;pgsql
make
sudo make install
&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-01-20:34</id>
    <published>2009-01-20T07:11:00Z</published>
    <updated>2009-01-20T07:29:09Z</updated>
    <category term="Code" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/1/20/obvious-tip-1-short-circuiting-logic-during-debugging" rel="alternate" type="text/html" />
    <title>Obvious Tip 1: Short Circuiting Logic While Debugging</title>
<content type="html">
            &lt;p&gt;Who hasn’t forced a conditional statement one way or the other to force part of a complicated system to always trigger during a bout of (potentially late night, potentially ill-advised) hackery? I’ve learned the hard way to always take the twenty seconds to add a warning so I don’t forget to remove the hack. Of course, specs are going to fail if I forget to, but it’s easy to spend time looking in the wrong places for a fix to something as sturdy as if statements.&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="FunctionName"&gt;puts&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;! OVERRIDING SOME LOGIC !&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;)
&lt;span class="Keyword"&gt;if&lt;/span&gt; &lt;span class="BuiltInConstant"&gt;true&lt;/span&gt; &lt;span class="Keyword"&gt;||&lt;/span&gt; some_method_that_may_or_may_not_return_true?
  ...
&lt;span class="Keyword"&gt;end&lt;/span&gt;
&lt;/pre&gt;

&lt;p&gt;Make sure to put the reminder &lt;em&gt;outside&lt;/em&gt; the conditional. Depending on what you’re working on, you can use a framework logger if you have one. I used puts above because the code I’m working on (which sparked this entry) is standalone.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2009-01-15:33</id>
    <published>2009-01-15T23:51:00Z</published>
    <updated>2009-01-15T23:55:33Z</updated>
    <category term="Rails" />
    <category term="Ruby" />
    <link href="http://autonomousmachine.com/2009/1/15/using-blocks-in-rails-custom-date-formats" rel="alternate" type="text/html" />
    <title>Using blocks in Rails custom date formats</title>
<content type="html">
            &lt;p&gt;A not-so-well known feature of ActiveSupport is its ability to handle blocks as definitions for custom date and time formats. Here’s one I just added to a project to give nice short dates, stripping out any leading zeros:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;ActiveSupport&lt;/span&gt;::&lt;span class="FunctionName"&gt;CoreExtensions&lt;/span&gt;::&lt;span class="FunctionName"&gt;Date&lt;/span&gt;::&lt;span class="FunctionName"&gt;Conversions&lt;/span&gt;::&lt;span class="FunctionName"&gt;DATE_FORMATS&lt;/span&gt;.&lt;span class="FunctionName"&gt;merge!&lt;/span&gt;(
  &lt;span class="UserDefinedConstant"&gt;&lt;span class="UserDefinedConstant"&gt;:&lt;/span&gt;shorty&lt;/span&gt; =&amp;gt; lambda {|&lt;span class="Variable"&gt;date&lt;/span&gt;| date.&lt;span class="FunctionName"&gt;strftime&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;%m/%d/%y&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).&lt;span class="FunctionName"&gt;gsub&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;0?&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\/&lt;/span&gt;0?&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\/&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;(&lt;/span&gt;&lt;span class="StringInterpolation"&gt;\d&lt;/span&gt;+&lt;span class="String"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="String"&gt;&lt;span class="String"&gt;/&lt;/span&gt;&lt;/span&gt;, &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;\1/\2/\3&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;)}
)
&lt;/pre&gt;

&lt;p&gt;You can drop that in your environment.rb; a better option is to place it in a file inside config/initializers.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2008-12-30:8</id>
    <published>2008-12-30T07:06:00Z</published>
    <updated>2009-01-15T23:06:30Z</updated>
    <category term="JavaScript" />
    <category term="Prototype" />
    <link href="http://autonomousmachine.com/2008/12/30/list-ordering-with-a-javascript-state-machine" rel="alternate" type="text/html" />
    <title>List Ordering with a JavaScript State Machine</title>
<content type="html">
            &lt;p&gt;I’ve been thinking a lot about state machines recently- specifically, if the use of an event-driven state machine could be a useful pattern when creating JavaScript interfaces. I did a little poking around online, and the best resource I could find was a &lt;a href="http://www.ibm.com/developerworks/views/web/libraryview.jsp?search_by=Finite+state+machines+in+JavaScript"&gt;series of articles&lt;/a&gt; on IBM’s developerWorks. The articles are actually quite good, and I’d recommend reading them. They include some nice background information on the hows and whys of state machine usage.&lt;/p&gt;

&lt;p&gt;The one valid criticism I have of the articles is they spend a lot of time developing special code to deal with browser idiosyncrasies and event handling although the articles date from early 2007, well after the rise of JavaScript frameworks. I decided to see what I could to do build a lightweight state machine model using mostly framework code, in this case Prototype and Low Pro.&lt;/p&gt;

&lt;h2&gt;A Somewhat Contrived Example&lt;/h2&gt;

&lt;p&gt;Since I want to mostly show the syntax I’m using, here is a basic example for a simple widget: a web radio. Here are the states and transitions this example will be using:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://autonomousmachine.com/assets/2008/9/17/radio_state_diagram.png?1221657621" alt="State machine diagram for radio widget" /&gt;&lt;/p&gt;

&lt;h2&gt;States&lt;/h2&gt;

&lt;p&gt;Here are the states shown in that diagram, converted to JavaScript (I’ve stripped most of the functionality out for brevity, a link to the full code is below). I’ve stolen the idea of using functions as event handlers like this from the developerWorks articles. I like the flexibility it offers, and also that in order to transition to a new state, one of these functions simply needs to return the name of the new state. I’ve also added a shortcut- if a event handling function is only going to return a state name, that string can just be used in the definition in place of the function.&lt;/p&gt;

&lt;p&gt;The state ‘start’ and its event ‘init’ are simply there so we can define a place for the machine to start.&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.definition &lt;span class="JsOperator"&gt;=&lt;/span&gt; {
    start: {
        init: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Stopped&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
    },

    Stopped: {
        play: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Playing&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
    },

    Paused: {
        play: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Playing&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;,
        &lt;span class="FunctionName"&gt;stop&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;event&lt;/span&gt;) {
            &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Stopped&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;;
        }
    },

    Playing: {
        &lt;span class="FunctionName"&gt;enter&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
            &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; called when we enter this state&lt;/span&gt;
        },
        pause: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Paused&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;,
        stop: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;Stopped&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;,
        &lt;span class="FunctionName"&gt;exit&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
            &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; called when we leave this state&lt;/span&gt;
        }
    }
};
&lt;/pre&gt;

&lt;h2&gt;Events&lt;/h2&gt;

&lt;p&gt;The next thing to consider is how to map DOM events to the events defined above. I decided to define them for each state using a simple CSS selector to event name mapping. Just as with the state definitions, each CSS selector is either mapped to a string (this time an event name) or a function that will return an event name.&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.events &lt;span class="JsOperator"&gt;=&lt;/span&gt; {
    Stopped: {
        &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;button.play:click&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;play&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
    },
    Playing: {
        &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;button.stop:click&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;stop&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;,
        &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="FunctionName"&gt;button.pause:click&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="FunctionName"&gt;function&lt;/span&gt;(&lt;span class="FunctionParameter"&gt;event&lt;/span&gt;) {
            &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; ...&lt;/span&gt;
            &lt;span class="Keyword"&gt;return&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;pause&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;;
        }
    },
    Paused: {
        &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;button.stop:click&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;stop&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;,
        &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;button.play:click&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;play&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;
    }
};
&lt;/pre&gt;

&lt;h2&gt;All Together Now&lt;/h2&gt;

&lt;p&gt;The state and event mapping definitions are brought together in a fairly standard Low Pro behavior, inheriting from another behavior containing the state machine logic. Any functions that are used as a part of the state and event definitions above are evaluated in the context of a behavior instance, so this is a convenient place for utility and helper  functions.&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.machine &lt;span class="JsOperator"&gt;=&lt;/span&gt; Behavior.create(State.behavior, {
    definition: &lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.definition,
    events: &lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.events,
    &lt;span class="FunctionName"&gt;incrementCounter&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; ...&lt;/span&gt;
    },
    &lt;span class="FunctionName"&gt;updateCounter&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; ...&lt;/span&gt;
    }
});
&lt;/pre&gt;

&lt;p&gt;The complete behavior is applied using the standard Low Pro Event.addBehavior().&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
Event.addBehavior({
    &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;div.radio&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: &lt;span class="LibraryObject"&gt;Radio&lt;/span&gt;.machine
});
&lt;/pre&gt;

&lt;p&gt;Here’s &lt;a href="http://monsters.autonomousmachine.com/low_pro_state/radio.html"&gt;a working example of the radio&lt;/a&gt; using the &lt;a href="http://github.com/jim/gizmos/tree/master/lowpro/state/examples/radio.js"&gt;complete code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;Sort, Sort, Sort&lt;/h2&gt;

&lt;p&gt;Obviously, a state machine could be used to model a much more complicated interaction. A client application I’m working on had a need for some customizable sorting, and I decided to write the code from the ground up in order to have complete control over the sort lifecycle. After some thought, I decided to try to model the behavior of many desktop UI systems: a clone of the element being dragged would move with the mouse, and an insertion point would appear when and where a legal insertion could occur. I thought this was the best interface for most use cases as it allows the user to see both an object’s new and old positions throughout the drag process. I also like the clear indication of when a valid drop can occur.&lt;/p&gt;

&lt;p&gt;The states and events used by the ordering widget are shown in the following diagram:&lt;/p&gt;

&lt;p&gt;&lt;img src="http://autonomousmachine.com/assets/2008/9/17/sort_state_diagram.png" alt="State machine diagram for ordering widget" /&gt;&lt;/p&gt;

&lt;p&gt;I won’t paste all of the code to the sorter here, but I’ve posted it &lt;a href="http://github.com/jim/gizmos/tree/master/lowpro/state/examples/sorter.js"&gt;on GitHub&lt;/a&gt; as well as &lt;a href="http://monsters.autonomousmachine.com/low_pro_state/sort.html"&gt;a working example&lt;/a&gt;. It worked out rather well, and I found the use of a state machine helped in keeping the code well organized and extensible, despite the fairly complicated interaction being modeled.&lt;/p&gt;
          </content>  </entry>
</feed>
