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

<channel>
	<title>Fiat Developmentum &#187; Code</title>
	<atom:link href="http://fiatdev.com/category/code/feed" rel="self" type="application/rss+xml" />
	<link>http://fiatdev.com</link>
	<description>Let There Be Software!</description>
	<lastBuildDate>Sun, 27 Sep 2009 01:05:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Patron 0.4</title>
		<link>http://fiatdev.com/2009/07/22/patron-0-4</link>
		<comments>http://fiatdev.com/2009/07/22/patron-0-4#comments</comments>
		<pubDate>Wed, 22 Jul 2009 17:35:32 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=255</guid>
		<description><![CDATA[The 0.4 version of Patron has been released. This version addresses feedback from the initial public release and fixes a few minor bugs:


Fixed a bug in the Patron.version method
Added HTTP proxy support
Enabled SSL and Win32 support
Added ability to download data to a file with GET
Fixed a bug where header values were prefixed with a space
Made [...]]]></description>
			<content:encoded><![CDATA[<p>The 0.4 version of Patron has been released. This version addresses feedback from the initial public release and fixes a few minor bugs:</p>

<ul>
<li>Fixed a bug in the Patron.version method</li>
<li>Added HTTP proxy support</li>
<li>Enabled SSL and Win32 support</li>
<li>Added ability to download data to a file with GET</li>
<li>Fixed a bug where header values were prefixed with a space</li>
<li>Made the Session#request method public</li>
<li>Added support for the WebDAV COPY method</li>
<li>Added support for uploading files with POST and PUT</li>
</ul>

<p>In addition to all of this, Patron has a new home page at <a href="http://toland.github.com/patron/">http://toland.github.com/patron/</a>.</p>

<p>So, what&#8217;s next for Patron? Well, I would like to put out a 0.5 release in the next few weeks with some refactoring and API tuning. Also, I would like to implement the rest of the WebDAV methods. I am toying with the idea of creating a net/http compatible interface so that Patron can be used as a drop-in replacement. Not sure if that is going to go anywhere, though.</p>

<p>Once I am happy with the API and documentation I will release 1.0. Other than that, I try to respond to bug reports and feature requests in a timely manner. I originally wrote Patron to scratch my own itch, and that itch has been scratched. At this point I am looking to make Patron as broadly useful as I can, and your feedback helps with that.</p>

<p>Patches and contributed documentation are always welcome. In particular, if anyone feels like trying to get this working on Windows, I will be happy to integrate the resulting patches and publish a binary gem.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2009/07/22/patron-0-4/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Using DTL templates with Nitrogen</title>
		<link>http://fiatdev.com/2009/07/13/using-dtl-templates-with-nitrogen</link>
		<comments>http://fiatdev.com/2009/07/13/using-dtl-templates-with-nitrogen#comments</comments>
		<pubDate>Mon, 13 Jul 2009 20:24:35 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[dtl]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[erlydtl]]></category>
		<category><![CDATA[nitrogen]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=249</guid>
		<description><![CDATA[One thing that I did not like about working with the Nitrogen web framework is the way that view rendering is handled. Nitrogen calls the function main/0 in your web modules which, by convention, returns an instance of the template record. This record points to a template file that has HTML code with a little [...]]]></description>
			<content:encoded><![CDATA[<p>One thing that I did not like about working with the <a href="http://nitrogenproject.com/">Nitrogen</a> web framework is the way that view rendering is handled. Nitrogen calls the function <code>main/0</code> in your web modules which, by convention, returns an instance of the <code>template</code> record. This record points to a template file that has HTML code with a little Erlang mixed in. Essentially, you can call functions in your web module and whatever content those functions return is interpolated into your template.</p>

<p>This all makes it sound a little more complicated than it really is. Here is a little &#8220;hello world&#8221; sample to illustrate how it works. First, the <code>web_page</code> Erlang module defines the <code>main/0</code> function and a couple of callouts:</p>

<pre><code>-module(web_page).
-include("wf.inc").
-compile(export_all).

main() -&gt; #template { file="./wwwroot/template.html" }.
title() -&gt; "Hello, World!".
content() -&gt; #h1 { text="Hello, World!" }.
</code></pre>

<p>This will cause Nitrogen to render the template.html file.</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;[[[page:title()]]]&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
[[[page:content()]]]
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>The callouts in the template will call the respective functions in the <code>web_page</code> module and we end up with a page that displays a bold &#8220;Hello, World!&#8221;. The template file is really analogous to a Rails layout and the content that Rails puts into a view template is returned from the callouts using a markup DSL based on the Erlang record syntax.</p>

<p>This template mechanism is simple, but it becomes very limiting when working on a non-trivial application. Nitrogen mixes controller functionality with presentation in a way that is reminiscent of PHP or JSP. Also, you cannot compose templates and the markup DSL doesn&#8217;t cover all of the HTML elements and attributes.</p>

<p>I ran into this problem while working on a Nitrogen powered site and decided that I just couldn&#8217;t live with the default template system. After a little digging around I discovered <a href="http://code.google.com/p/erlydtl/">ErlyDTL</a> which implements a substantial subset of the <a href="http://www.djangoproject.com/documentation/templates/">Django Template Language</a> in Erlang. It turns out that using ErlyDTL in a Nitrogen site is quite simple.</p>

<p>The first thing I needed was a utility function to render ErlyDTL templates. Nitrogen includes implicit elements for the notification flash and Javascript code for all of that AJAX goodnes which I needed to replicate in my render function.</p>

<pre><code>-module(my_util).
-compile(export_all).

template_path(Template) -&gt;
    filename:join([code:priv_dir(my_app), templates, Template]).

render_template(Name, Variables) -&gt;
    Template = template_path(Name ++ ".html"),
    Mod = list_to_atom(Name ++ "_template"),
    ok = erlydtl:compile(Template, Mod),
    {ok, Body} = Mod:render([{flash, element_flash:render()},
                             {script, wf_script:get_script()}
                             | Variables]),
    Body.
</code></pre>

<p>Now, I can replace the simple module from earlier with this new one:</p>

<pre><code>-module(web_page).
-include("wf.inc").
-compile(export_all).

main() -&gt;
    my_util:render_template("template", [
          {title, "Hello, World!"},
          {content, content()}]).

content() -&gt; "Hello, World!".       
</code></pre>

<p>My template file is now equally simple:</p>

<pre><code>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;{{ title }}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;{{ content }}&lt;/h1&gt;    
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>All of the markup is now in the template file, and the module is concerned only with getting the content from somewhere and making it available to the view. This also allows us to use the full power of ErlyDTL: we can compose templates and perform simple operations on the content to be displayed. Here is an example from the ErlyDTL home page:</p>

<pre><code>Welcome back, {{ name }}!

You have {{ friends|length }} friends: {{ friends|join:", " }}

Have some primes:
{# this is exciting #}
{% for i in primes %}
    {{ i }}
{% endfor %}
</code></pre>

<p>There is one drawback to this approach. The Nitrogen DSL includes helpers for generating links that work with its event system. When using ErlyDTL we have to create those links manually, but it isn&#8217;t hard. The key thing to remember is that the link id is the event name that will be passed to your module. Here are a couple of examples:</p>

<pre><code>&lt;a id="do_it" href="javascript:"&gt;Do It!&lt;/a&gt;
&lt;button id="do_it" class="button submit"&gt;Do It!&lt;/button&gt;
</code></pre>

<p>The extra flexibility of DTL is certainly worth the small amount of extra work.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2009/07/13/using-dtl-templates-with-nitrogen/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Announcing Patron: a Ruby HTTP client library</title>
		<link>http://fiatdev.com/2009/07/03/announcing-patron-a-ruby-http-client-library</link>
		<comments>http://fiatdev.com/2009/07/03/announcing-patron-a-ruby-http-client-library#comments</comments>
		<pubDate>Fri, 03 Jul 2009 20:02:50 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[client]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=244</guid>
		<description><![CDATA[I was recently given the opportunity to release several of the projects I was working on at The Hive as open source. These projects are libraries that we built to address specific problems we ran into but were not core to our business. Most of these were always destined to be released, it just took [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently given the opportunity to release several of the projects I was working on at The Hive as open source. These projects are libraries that we built to address specific problems we ran into but were not core to our business. Most of these were always destined to be released, it just took a while before we got around to it. Everything is currently on my <a href="http://github.com/toland">GitHub page</a>, but I wanted to do a little cleanup on each project and announce them individually.</p>

<p>Today I am announcing the first of these projects: Patron. Patron is an HTTP client library for Ruby that features a usable API and good performance. As for the name Patron, I searched through the thesaurus looking for synonyms for the word &#8220;client&#8221;.</p>

<p>The performance problems with the built-in Ruby HTTP client are well documented <a href="http://apocryph.org/2008/10/04/analysis_ruby_18x_http_client_performance/">elsewhere</a>, so I won&#8217;t go into that here. The other alternative is the Curb gem which is based on <a href="http://curl.haxx.se/libcurl/">libcurl</a> and offers great performance. Unfortunately, curb is unfinished and doesn&#8217;t appear to have an official maintainer. Also, the API leaves much to be desired. These issues are compounded by the fact that curb is primarily implemented in C and is difficult to modify.</p>

<p>I tried to patch curb to get all of the features we wanted, but subtle bugs kept creeping in. Eventually I gave in and wrote Patron. The goal was to have a Ruby HTTP client built on libcurl with a reasonable API. By implementing as much as possible in Ruby I think that Patron is much more maintainable than curb.</p>

<p>I have not made an attempt to support every feature of libcurl. Libcurl is huge and trying to support every feature would lead to bloat and make it harder to maintain. Instead, I built a small API that did what we needed it to do. It has been in use at The Hive for some time now and seems pretty stable.</p>

<p>The documentation for Patron is <a href="http://patron.rubyforge.org/">on Rubyforge</a> and the source code is available <a href="http://github.com/toland/patron/tree/master">on GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2009/07/03/announcing-patron-a-ruby-http-client-library/feed</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>QLMarkdown 1.1</title>
		<link>http://fiatdev.com/2009/02/11/qlmarkdown-11</link>
		<comments>http://fiatdev.com/2009/02/11/qlmarkdown-11#comments</comments>
		<pubDate>Thu, 12 Feb 2009 03:19:31 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[.mac]]></category>
		<category><![CDATA[markdown]]></category>
		<category><![CDATA[quicklook]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=136</guid>
		<description><![CDATA[There is a new version of the  generator available. I didn&#8217;t have anything to do with the new release other than zipping up the final product. Michael Dominic K. added some nice new features. You can download the goodies here or grab the source from GitHub.
]]></description>
			<content:encoded><![CDATA[<p>There is a new version of the <a href="#">QLMarkdown</a> generator available. I didn&#8217;t have anything to do with the new release other than zipping up the final product. Michael Dominic K. added some <a href="http://www.mdk.org.pl/2009/2/10/quicklook-for-markdown">nice new features</a>. You can download the goodies <a href="http://fiatdev.com/downloads/QLMarkdown-1.1.zip">here</a> or grab <a href="http://github.com/toland/qlmarkdown">the source</a> from GitHub.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2009/02/11/qlmarkdown-11/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Nitrogen</title>
		<link>http://fiatdev.com/2008/12/06/nitrogen</link>
		<comments>http://fiatdev.com/2008/12/06/nitrogen#comments</comments>
		<pubDate>Sat, 06 Dec 2008 17:10:35 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=123</guid>
		<description><![CDATA[I have been using Erlang extensively for the last 6 months or so and it is a very nice language. While I have enjoyed working with the language, I wasn&#8217;t really impressed with the existing Erlang web frameworks. Mochiweb is nice enough, and I use it to power an application with an HTTP based ReSTful [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using Erlang extensively for the last 6 months or so and it is a very nice language. While I have enjoyed working with the language, I wasn&#8217;t really impressed with the existing Erlang web frameworks. <a href="http://code.google.com/p/mochiweb/">Mochiweb</a> is nice enough, and I use it to power an application with an HTTP based ReSTful API, but it is too bare bones to use for building a full-featured site.</p>

<p>Then I ran across <a href="http://nitrogen-erlang.tumblr.com/">Nitrogen</a>. The first thing that struck me about Nitrogen is how it (ab)uses the Erlang record syntax to create a passable DSL for HTML generation. Check out this snippet from the included sample application:</p>

<pre><code>main() -&gt;
  Title = "Postbacks",
  Body = #template { file=onecolumn, title=Title,
                     headline=Title, section1=[

    #button { text="Press Me", postback=button_pressed },
    #p{},

    #link { text="Click Me", postback=link_clicked },
    #p{},

    #label { text="Press enter in the textbox." },
    #textbox { text="This is a message...", 
               postback=textbox_enterkey },
    #p{},

    #checkbox { text="Toggle Me", postback=checkbox_clicked },
    #p{},

    #dropdown { postback=dropdown_changed, options=[
      #option { text="Option 1" },
      #option { text="Option 2" },
      #option { text="Option 3" }
    ]},
    #p{},

    #span { text="Mouse Over Me", 
            actions=#event { type=mouseover, 
                             postback=span_mousedover } },
    #p{}

  ]},
  wf:render(Body).
</code></pre>

<p>The second thing that caught my attention is the event model. The <code>postback</code> attribute on the elements in the above snippet cause the Erlang term that is assigned to it (in this case symbols) to be posted back to the server using AJAX. This example simply shows an alert box with the posted symbol. The code is  very concise:</p>

<pre><code>event(EventInfo) -&gt;
  wf:wire(#alert { text=wf:f("~p", [EventInfo]) }),
  ok.
</code></pre>

<p>The nice thing about this model is that you can use Erlang&#8217;s pattern matching to catch events from different elements. If you have elements that look like this:</p>

<pre><code>#button { text="Next", postback=next },
#button { text="Previous", postback=previous },
</code></pre>

<p>then you can handle the events using pattern matching:</p>

<pre><code>event(next) -&gt;
  %% Go to the next item...
  ok;
event(previous) -&gt;
  %% Go to the previous item...
  ok.
</code></pre>

<p>This is clean and easy to understand.</p>

<p>I have not spent as much time as I would like with Nitrogen, but it certainly has my attention. It currently addresses two of the gripes I have had with other frameworks, but there are other issues that have not yet been addressed. For example, how easy is it to deploy or update a site built with Nitrogen? What does the performance and efficiency look like? Using Erlang gives Nitrogen a head start in both of these areas, so I am optimistic.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2008/12/06/nitrogen/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>app_version Updated</title>
		<link>http://fiatdev.com/2008/08/10/app_version-updated</link>
		<comments>http://fiatdev.com/2008/08/10/app_version-updated#comments</comments>
		<pubDate>Sun, 10 Aug 2008 23:47:23 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=109</guid>
		<description><![CDATA[I have updated my app_version Rails plugin to support getting build numbers from Git. This is of necessity a little kludgy. The plugin allows you to specify either &#8216;git-revcount&#8217; or &#8216;git-hash&#8217; as the build. &#8216;git-revcount&#8217; will set the build number to the number of commits on the current Git branch and &#8216;git-hash&#8217; will set the [...]]]></description>
			<content:encoded><![CDATA[<p>I have updated my app_version Rails plugin to support getting build numbers from Git. This is of necessity a little kludgy. The plugin allows you to specify either &#8216;git-revcount&#8217; or &#8216;git-hash&#8217; as the build. &#8216;git-revcount&#8217; will set the build number to the number of commits on the current Git branch and &#8216;git-hash&#8217; will set the build number to the first six digits of the Git HEAD hash. Neither method is really equivalent to the Subversion revision number, but either might be useful in certain contexts.</p>

<p>The source code for the plugin is available at <a href="http://github.com/toland/app_version">http://github.com/toland/app_version</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2008/08/10/app_version-updated/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>app_version plugin is now hosted on GitHub</title>
		<link>http://fiatdev.com/2008/04/10/app_version-plugin-is-now-hosted-on-github</link>
		<comments>http://fiatdev.com/2008/04/10/app_version-plugin-is-now-hosted-on-github#comments</comments>
		<pubDate>Fri, 11 Apr 2008 02:25:27 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=96</guid>
		<description><![CDATA[I recently changed hosting providers and I am still in the process of moving everything over. One headache that I don&#8217;t need is recreating all of the Subversion repositories that I had hosted on the old service. So, I have decided to move the code for my  plugin to GitHub.

The plugin&#8217;s code is now [...]]]></description>
			<content:encoded><![CDATA[<p>I recently changed hosting providers and I am still in the process of moving everything over. One headache that I don&#8217;t need is recreating all of the Subversion repositories that I had hosted on the old service. So, I have decided to move the code for my <a href="#">app_version</a> plugin to <a href="http://github.com/">GitHub</a>.</p>

<p>The plugin&#8217;s code is now located at <a href="http://github.com/toland/app_version">http://github.com/toland/app_version</a>. You can clone the repository using the following command:</p>

<pre><code>git clone git://github.com/toland/app_version.git
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2008/04/10/app_version-plugin-is-now-hosted-on-github/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OOP is a tool</title>
		<link>http://fiatdev.com/2008/04/07/oop-is-a-tool</link>
		<comments>http://fiatdev.com/2008/04/07/oop-is-a-tool#comments</comments>
		<pubDate>Tue, 08 Apr 2008 00:49:34 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[theory]]></category>

		<guid isPermaLink="false">http://fiatdev.com/?p=91</guid>
		<description><![CDATA[I spotted this comment to a lengthy article about the proper use of inheritance in Object Oriented programming:


  On one of the &#8216;communities,&#8217; someone had a sarcastic response to the 
  Final == Good article: To boil the article down: OOP is hard.
  Let&#8217;s make it as non-object-oriented as possible so that [...]]]></description>
			<content:encoded><![CDATA[<p>I spotted <a href="http://weblog.raganwald.com/2008/04/is-strictly-equivalent-to.html?showComment=1207590060000#c3689604514633552629">this comment</a> to a <a href="http://weblog.raganwald.com/2008/04/is-strictly-equivalent-to.html">lengthy article</a> about the proper use of inheritance in Object Oriented programming:</p>

<blockquote>
  <p>On one of the &#8216;communities,&#8217; someone had a sarcastic response to the 
  <a href="http://cafe.elharo.com/blogroll/final-good/">Final == Good</a> article: <em>To boil the article down: OOP is hard.
  Let&#8217;s make it as non-object-oriented as possible so that things don&#8217;t
  get too complicated!</em></p>
  
  <p>I think that’s actually correct in a way. OOP actually is hard to get right.
  Encapsulation is not hard, it is a simplifying principle. But inheritance is
  seductively easy in the trivial case but hard to get just right in the wild.
  Thus, the admonition to favour composition.</p>
  
  <p>That does’t mean we never use it of course. But I am not embarrassed to say
  that I think inheritance is hard to get right.</p>
</blockquote>

<p>This strikes me as odd; the whole point of OOP is to make it easier for programmers to conceptualize and build complicated software systems. If inheritance is complicating the issue rather than simplifying it, maybe OOP needs a rethink.</p>

<p>The only &#8220;right&#8221; way to do OOP is the way that helps the programmer deliver high quality software in a reasonable amount of time. This isn&#8217;t something that strikes me as capable of being objectively right or wrong. I think that there is a danger in overthinking issues like this or in assigning too much value to being &#8220;object oriented.&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2008/04/07/oop-is-a-tool/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating a &#8216;returning&#8217; macro in Lisp</title>
		<link>http://fiatdev.com/2007/08/25/creating-a-returning-macro-in-lisp</link>
		<comments>http://fiatdev.com/2007/08/25/creating-a-returning-macro-in-lisp#comments</comments>
		<pubDate>Sat, 25 Aug 2007 21:11:01 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[macros]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://fiatdev.com/2007/08/25/creating-a-returning-macro-in-lisp</guid>
		<description><![CDATA[I have been reading Practical Common Lisp and learning quite a lot in the process. However, I began to see a pattern emerging in the code that deals with objects that is also very common in Ruby. The pattern emerges where you have a class and you want to perform some initialization beyond what the [...]]]></description>
			<content:encoded><![CDATA[<p>I have been reading <a href="http://www.gigamonkeys.com/book/">Practical Common Lisp</a> and learning quite a lot in the process. However, I began to see a pattern emerging in the code that deals with objects that is also very common in Ruby. The pattern emerges where you have a class and you want to perform some initialization beyond what the normal initializers provide you. I will refer to the following class in this example:</p>

<pre><code>;;; Lisp User class
(defclass user ()
 (login
  password
  first-name
  last-name))
</code></pre>

<p>The recurring pattern involves code that looks like this:</p>

<pre><code>(let ((u make-instance user))
  (with-slots (first-name last-name) u
    (setf first-name "Foo")
    (setf last-name "Bar"))
  u)
</code></pre>

<p>In other words, we want to create an instance of a class within a <code>let</code> form, manipulate that instance somehow and then return it from the form. This example is trivial and could have been accomplished using initializers, but I think it still illustrates the pattern.</p>

<p>Rails has a function that makes this pattern a little cleaner, at least to my eyes. The function is called <code>returning</code>, is part of the ActiveSupport library and is defined as:</p>

<pre><code>def returning(value)
  yield(value)
  value
end
</code></pre>

<p>This is very simple and it allows you to write the Ruby equivalent of our user initialization code like this:</p>

<pre><code>def create_user
  returning User.new do |u|
    u.first_name = "Foo"
    u.last_name = "Bar"
  end
end
</code></pre>

<p>Translating this into Lisp should be a fun and reasonable simple exercise. First, lets try a naive transliteration of the <code>returning</code> function from Ruby to Lisp. That might leave us with the following code:</p>

<pre><code>(defun returning (value fn)
  (let ((var value))
    (funcall fn var)
    var))
</code></pre>

<p>This is a literal and faithful translation of the code from Ruby to Lisp and it allows us to write our user initialization code like so:</p>

<pre><code>(returning (make-instance 'user)
           (lambda (u)
             (with-slots (first-name last-name) u
               (setf first-name "Foo")
               (setf last-name "Bar"))))
</code></pre>

<p>This code has a couple of problems. First, it is more verbose than I would like. The final term is no longer hanging off the bottom but we have added a lambda form that wasn&#8217;t there before. Second, this really isn&#8217;t very idiomatic Lisp code (at least as far as I can tell with my still limited knowledge of Lisp).</p>

<p>The problem is that Lisp&#8217;s <code>lambda</code> forms are not really the same thing as Ruby&#8217;s blocks. In fact, the closest thing Ruby has to a Lisp <code>lambda</code> is a <code>Proc</code> object which you can create, oddly enough, with the <code>lambda</code> keyword. The <code>returning</code> function in Ruby is taking advantage of blocks to implement the <a href="http://en.wikipedia.org/wiki/Template_method">Template Method</a> pattern. While you can use <code>lambda</code> in Lisp to create a template method, there is a more elegant solution: macros.</p>

<p>So, lets take a stab at creating a &#8216;returning&#8217; macro in Lisp. We will need to take the name of the variable to use and its initial value as parameters to the macro. We will also need the body of the form where all of the work gets done. This gives us the following signature:</p>

<pre><code>(defmacro returning (var value &amp;body body) ...)
</code></pre>

<p>The <code>&amp;body</code> works like <code>&amp;rest</code> and collects the rest of the forms passed to the macro into the <code>body</code> parameter. The <code>let</code> form way back at the beginning of this post provides a template for what we want the body of the macro to look like. If we take the original <code>let</code> form, quote it and substitute <code>var</code>, <code>value</code> and <code>body</code> in the appropriate places we get this:</p>

<pre><code>(defmacro returning (var value &amp;body body)
  `(let ((,var ,value))
     ,@body
     ,var))
</code></pre>

<p>Since <code>body</code> is a list we have to splice it into the current form using the <code>@</code> operator. This does almost what we want. We can use it to rewrite our original initialization code using <code>returning</code> in place of the original <code>let</code>:</p>

<pre><code>(returning u (make-instance 'user)
  (with-slots (first-name last-name) u
    (setf first-name "Foo")
    (setf last-name "Bar")))
</code></pre>

<p>There is a tiny problem with this code as well. The variable name and initialization form are hanging out there at the end of the line and there is no indication that they are related. We would really like to put a pair of parenthesis around both the <code>u</code> and the <code>make-instance</code> form so that it is clear they are related. Fortunately, we can &#8220;destructure&#8221; lists that are passed to macros as parameters. If we change the signature of the returning macro to include parenthesis around <code>var</code> and <code>value</code> we can write the code like this:</p>

<pre><code>(returning (u (make-instance 'user))
  (with-slots (first-name last-name) u
    (setf first-name "Foo")
    (setf last-name "Bar")))
</code></pre>

<p>Finally, what if we want to leave off the initialization form? Perhaps we want to initialize the variable &#8216;u&#8217; in the body. This is easily accomplished by adding <code>&amp;optional</code> to the macro parameter list before the <code>value</code> parameter:</p>

<pre><code>(defmacro returning ((var &amp;optional value) &amp;body body)
  `(let ((,var ,value))
     ,@body
     ,var))
</code></pre>

<p>This allows us to initialize <code>u</code> in the body of the form:</p>

<pre><code>(returning (u)
  (setf u (make-instance 'user))
  (with-slots (first-name last-name) u
    (setf first-name "Foo")
    (setf last-name "Bar")))
</code></pre>

<p>It can be helpful to see how a macro expands. The <code>macroexpand-1</code> function does exactly what we want. Here are the expansions of our returning macro both with and without the initialization form (the output has been cleaned up a bit):</p>

<pre><code>CL-USER&gt; (macroexpand-1 '(returning (u (make-instance 'user))
              (with-slots (first-name last-name) u
                (setf first-name "Foo")
                (setf last-name "Bar"))))
(LET ((U (MAKE-INSTANCE 'USER))) 
  (WITH-SLOTS (FIRST-NAME LAST-NAME) U 
    (SETF FIRST-NAME "Foo") 
    (SETF LAST-NAME "Bar")) 
  U)
T
CL-USER&gt; (macroexpand-1 '(returning (u)
              (setf u (make-instance 'user))
              (with-slots (first-name last-name) u
                (setf first-name "Foo")
                (setf last-name "Bar"))))
(LET ((U NIL)) 
  (SETF U (MAKE-INSTANCE 'USER)) 
  (WITH-SLOTS (FIRST-NAME LAST-NAME) U 
    (SETF FIRST-NAME "Foo") 
    (SETF LAST-NAME "Bar")) 
  U)
T
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2007/08/25/creating-a-returning-macro-in-lisp/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Making ActionWebService work with Edge Rails</title>
		<link>http://fiatdev.com/2007/05/31/making-actionwebservice-work-with-edge-rails</link>
		<comments>http://fiatdev.com/2007/05/31/making-actionwebservice-work-with-edge-rails#comments</comments>
		<pubDate>Thu, 31 May 2007 21:04:47 +0000</pubDate>
		<dc:creator>phil</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[actionwebservice]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[soap]]></category>

		<guid isPermaLink="false">http://fiatdev.com/2007/05/31/making-actionwebservice-work-with-edge-rails</guid>
		<description><![CDATA[I have a Rails application with a small SOAP service that is used for integration. Today I updated the site to Edge Rails and noticed a ton of strange errors related to ActionWebService. The first error looked like this:

Expected app/controllers/my_controller.rb to define MyController 


I remembered hearing that ActionWebService was going to be moved out of [...]]]></description>
			<content:encoded><![CDATA[<p>I have a Rails application with a small SOAP service that is used for integration. Today I updated the site to Edge Rails and noticed a ton of strange errors related to ActionWebService. The first error looked like this:</p>

<pre><code>Expected app/controllers/my_controller.rb to define MyController 
</code></pre>

<p>I remembered hearing that ActionWebService was going to be moved out of Rails core, and I eventually found <a href="http://dev.rubyonrails.org/changeset/6550">changeset 6550</a> from about 1 month ago where AWS was removed from the default load path. The ChangeLog suggests adding <code>vendor/rails/actionwebservice/lib</code> to <code>config.load_paths</code>. It turns out this is not enough. The <code>app/apis</code> directory also needs to be added to the load path and &#8216;action_web_service&#8217; needs to be required.</p>

<p>Here is an excerpt from my environment.rb after making these changes:</p>

<pre><code>Rails::Initializer.run do |config|
  # Add additional load paths for your own custom dirs
  config.load_paths += %W(
    #{RAILS_ROOT}/app/apis
    #{RAILS_ROOT}/vendor/rails/actionwebservice/lib
  )

  # ...more stuff...
end

require 'action_web_service'
</code></pre>

<p>This will get AWS working again, but functional tests may fail with the following error:</p>

<pre><code>NoMethodError: undefined method 'invoke' for #&lt;MyApiTest:0x3180f08&gt;
</code></pre>

<p>There is one more change that is necessary: add <code>require 'action_web_service/test_invoke'</code> to the top of <code>test/test_helper.rb</code>.</p>

<pre><code>ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'test_help'
require 'action_web_service/test_invoke'
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://fiatdev.com/2007/05/31/making-actionwebservice-work-with-edge-rails/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
