<?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-06-22T19:26:45Z</updated>
  <link rel="self" href="http://feeds.feedburner.com/TheThoughtsOfRobots-Main" type="application/atom+xml" /><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-02-17T07:07:06Z</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="Keyword"&gt;do&lt;/span&gt;
  &lt;span class="Keyword"&gt;include&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;Base&lt;/span&gt;
&lt;span class="Keyword"&gt;end&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>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2008-12-16:21</id>
    <published>2008-12-16T14:09:00Z</published>
    <updated>2009-01-15T23:06:40Z</updated>
    <category term="CSS" />
    <link href="http://autonomousmachine.com/2008/12/16/gif-the-png-fallback" rel="alternate" type="text/html" />
    <title>GIF, the PNG fallback</title>
<content type="html">
            &lt;p&gt;About a year ago I was working on the front end of one of those new fangled Web 2.0 websites, with pages full of curvy drop shadowed gradients. I stumbled into a fairly obvious technique for dealing with IE6’s lack of proper PNG support- replace said PNGS with the closest possible GIF in my IE6-specific stylesheet. Yes, this means that the edges of some page elements aren’t going to look as nice, but most of the time, it’s close enough for such an old browser.&lt;/p&gt;

&lt;p&gt;My typical CSS skeleton construction process is now:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Semantic markup&lt;/li&gt;
&lt;li&gt;CSS and optimal background images (PNG if needed)&lt;/li&gt;
&lt;li&gt;IE Compatibility Check for layout&lt;/li&gt;
&lt;li&gt;IE Compatibility Check for PNG/GIF substitution&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Step 4 is where I go through and replace any trouble PNGs with GIFs. I no longer use any of the various filter/IE specific PNG hacks; all of the ones I’ve tried don’t work correctly with tiled background images, which something I end up needing frequently.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2008-11-24:20</id>
    <published>2008-11-24T23:37:00Z</published>
    <updated>2008-11-25T15:59:57Z</updated>
    <category term="JavaScript" />
    <category term="Prototype" />
    <link href="http://autonomousmachine.com/2008/11/24/emulating-hover-with-prototype-and-lowpro" rel="alternate" type="text/html" />
    <title>Emulating :hover in IE6 with Prototype and Low Pro</title>
<content type="html">
            &lt;p&gt;A common technique for building UI items that respond to a hover, such as drop downs, is to write CSS rules that make a child UL appear when an enclosing element is moused over:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
         &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;/item1&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Item 1&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
         &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
                &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;subitem1&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Subitem1&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
                &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;subitem2&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Subitem2&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
                &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;subitem3&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Subitem3&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
         &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;/item2&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Item 2&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt; &lt;span class="MarkupTagAttribute"&gt;href&lt;/span&gt;=&lt;span class="String"&gt;&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;/item3&lt;span class="String"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;Item 3&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;a&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class="MarkupTag"&gt;&lt;span class="MarkupTag"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt;&lt;span class="MarkupTag"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt; &lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt; &lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt; {
  &lt;span class="LibraryObject"&gt;display&lt;/span&gt;: &lt;span class="LibraryConstant"&gt;none&lt;/span&gt;;
}

&lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt; &lt;span class="MarkupNameOfTag"&gt;li&lt;/span&gt;&lt;span class="MarkupTagAttribute"&gt;&lt;span class="MarkupTagAttribute"&gt;:&lt;/span&gt;hover&lt;/span&gt; &lt;span class="MarkupNameOfTag"&gt;ul&lt;/span&gt; {
  &lt;span class="LibraryObject"&gt;display&lt;/span&gt;: &lt;span class="LibraryConstant"&gt;block&lt;/span&gt;;
}
&lt;/pre&gt;

&lt;p&gt;Which works everywhere except under IE6. I’ve seen some posts on the web that suggest adding event listeners to the elements that need to detect hovering, and then having an event handler add a CSS class to the elements. Unfortunately, most of these posts recommend attaching listeners to the the &lt;em&gt;mouseover&lt;/em&gt; and &lt;em&gt;mouseout&lt;/em&gt; events, which is problematic if the elements have any children. When the child elements fire their own events, the result will be an annoying flicker.&lt;/p&gt;

&lt;p&gt;One option is to add conditionals the event handler functions to make sure only the desired events are being responded to. But a simpler solution in this case is to use IE’s non-standard &lt;em&gt;mouseenter&lt;/em&gt; and &lt;em&gt;mouseleave&lt;/em&gt; events. Here’s an example in LowPro:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&lt;span class="Keyword"&gt;var&lt;/span&gt; ElementWithManualHover &lt;span class="JsOperator"&gt;=&lt;/span&gt; Behavior.create({
    &lt;span class="FunctionName"&gt;onmouseenter&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
        &lt;span class="Variable"&gt;this&lt;/span&gt;.element.addClassName(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;hover&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
    },
    &lt;span class="FunctionName"&gt;onmouseleave&lt;/span&gt;: &lt;span class="Keyword"&gt;function&lt;/span&gt;() {
        &lt;span class="Variable"&gt;this&lt;/span&gt;.element.removeClassName(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;hover&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
    }
});

&lt;span class="Keyword"&gt;if&lt;/span&gt; (Prototype.Browser.IE6) {
    Event.addBehavior({
       &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;ul li&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;: ElementWithManualHover 
    });
}
&lt;/pre&gt;

&lt;p&gt;I’m using some &lt;a href="http://github.com/jim/gizmos/tree/master/prototype/browser_detection.js"&gt;extended browser detection&lt;/a&gt; to apply these behaviors only to IE6. You could do the same thing on your own in a few more lines of code.&lt;/p&gt;

&lt;p&gt;This, of course, is only a good technique if you’re already using Prototype. If you aren’t, take a look at jQuery’s &lt;a href="http://docs.jquery.com/Events/hover#overout"&gt;hover&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://autonomousmachine.com/">
    <author>
      <name>jim</name>
    </author>
    <id>tag:autonomousmachine.com,2008-11-21:18</id>
    <published>2008-11-21T21:49:00Z</published>
    <updated>2009-06-19T20:38:27Z</updated>
    <category term="PHP" />
    <link href="http://autonomousmachine.com/2008/11/21/fitzgerald-a-sinatra-clone-in-php" rel="alternate" type="text/html" />
    <title>Fitzgerald: a Sinatra clone in PHP</title>
<content type="html">
            &lt;p&gt;Two weeks ago I had a tiny client website that suddenly needed a login section to provide some downloads to approved users. Initially I reached for &lt;a href="http://sinatra.rubyforge.org/"&gt;Sinatra&lt;/a&gt;, which I have grown to love in recent months, but sadly the client’s server had no opportunity to run Ruby. A sad day for sure, but the world moves on and I decided to revisit my old friend PHP and see what could be done.&lt;/p&gt;

&lt;p&gt;I spent about ten minutes looking through various PHP frameworks, but  quickly decided that it would be faster to write the code the tiny site would need than learn the ins and outs of a full framework. I wanted something tiny anyway (preferably a single file), and most of what I found online were various fully fledged MVC Rails clones. Not going to work.&lt;/p&gt;

&lt;p&gt;I knew I wanted a solution that would get me as close to possible to Sinatra’s syntax, while making the best of the confines of PHP. Here’s the final syntax I came up with:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&amp;lt;?php
    &lt;span class="PreprocessorDirective"&gt;include&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;lib/fitzgerald.php&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);

    &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;Application&lt;/span&gt; &lt;span class="Keyword"&gt;extends&lt;/span&gt; &lt;span class="InheritedClassName"&gt;Fitzgerald&lt;/span&gt; {

        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; Basic get request&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_index&lt;/span&gt;() {
            &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;render(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;index&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;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;Application&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;get(&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="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;get_index&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;app&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;run();
?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;The basic concept is to subclass Fitzgerald with controller methods, and then map them to urls using get() and post() calls on an instantiated application object.&lt;/p&gt;

&lt;p&gt;I added some Merb-style argument passing to the mix using PHP5’s &lt;a href="http://us.php.net/oop5.reflection"&gt;Reflection API&lt;/a&gt;:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&amp;lt;?php
    &lt;span class="PreprocessorDirective"&gt;include&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;lib/fitzgerald.php&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);

    &lt;span class="Keyword"&gt;class&lt;/span&gt; &lt;span class="TypeName"&gt;Application&lt;/span&gt; &lt;span class="Keyword"&gt;extends&lt;/span&gt; &lt;span class="InheritedClassName"&gt;Fitzgerald&lt;/span&gt; {

        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; $page will be one of 'about', 'contact', or 'faq' thanks to our URL mapping below&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_page&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;page&lt;/span&gt;) {
            &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;render(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;page&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;Application&lt;/span&gt;();

    &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; :page is a placeholder- it will match anything by default, or a regex to match can&lt;/span&gt;
    &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; be passed in using an optional third argument to get() or post()&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;get(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;/:page&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;get_page&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="LibraryFunction"&gt;array&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;page&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="Keyword"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;about|contact|faq&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;app&lt;/span&gt;&lt;span class="Keyword"&gt;-&amp;gt;&lt;/span&gt;run();
?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;Like Sinatra, the return value of a method is what is returned to the browser. Fitzgerald has built in methods to render a template or redirect the browser:&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;Application&lt;/span&gt; &lt;span class="Keyword"&gt;extends&lt;/span&gt; &lt;span class="InheritedClassName"&gt;Fitzgerald&lt;/span&gt; {

        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; Renders a php file in '../views/', making variables provided to compact&lt;/span&gt;
        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; available as local variables in the template&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_page&lt;/span&gt;(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;page&lt;/span&gt;) {
            &lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;var1&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;Value to use in template&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;var2&lt;/span&gt; &lt;span class="Keyword"&gt;=&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;Another value to use in template&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;;
            &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;render(&lt;span class="Variable"&gt;&lt;span class="Variable"&gt;$&lt;/span&gt;page&lt;/span&gt;, &lt;span class="LibraryFunction"&gt;compact&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;var1&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;var2&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;));
        }

        &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; You can also send redirect headers&lt;/span&gt;
        &lt;span class="Keyword"&gt;public&lt;/span&gt; &lt;span class="Keyword"&gt;function&lt;/span&gt; &lt;span class="UserDefinedConstant"&gt;get_redirect&lt;/span&gt; {
            &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;/url/to/redirect/to&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;);
        }
    }
?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;You can also specify a layout with an options array passed to the application initialization:&lt;/p&gt;

&lt;pre class="mac_classic"&gt;
&amp;lt;?php
  &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; This will capture the output of any render calls into a local variable $content,&lt;/span&gt;
  &lt;span class="Comment"&gt;&lt;span class="Comment"&gt;//&lt;/span&gt; and then render the layout provided here&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;ApplicationWithLogin&lt;/span&gt;(&lt;span class="LibraryFunction"&gt;array&lt;/span&gt;(&lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;layout&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="Keyword"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="String"&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;span class="String"&gt;shell&lt;/span&gt;&lt;span class="String"&gt;'&lt;/span&gt;&lt;/span&gt;));
?&amp;gt;
&lt;/pre&gt;

&lt;p&gt;And that’s &lt;a href="http://github.com/jim/fitzgerald/tree/master"&gt;Fitzgerald&lt;/a&gt;. There is a more advanced example that shows how to build a simple authentication system in the &lt;a href="http://github.com/jim/fitzgerald/tree/master"&gt;repo on Github&lt;/a&gt;- look at example.php.&lt;/p&gt;
          </content>  </entry>
</feed>
