<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Brainspl.at: Merb gets cool Routes and file uploads</title>
    <link>http://brainspl.at/articles/2006/10/15/merb-gets-cool-routes-and-file-uploads</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Brainspl.at</description>
    <item>
      <title>Merb gets cool Routes and file uploads</title>
      <description>I have done more work on Merb and it is getting close to being ready for production use. I implemented a cool route generator that I thought I would detail here a bit as I found it very fun to make.&lt;br /&gt;&lt;br /&gt;
So here is a simple Merb route definition:&lt;br /&gt;
&lt;pre&gt;
  Merb::RouteMatcher.prepare do |r|
    r.add '/foo/:bar/baz/:id', :class =&amp;gt; 'Test', :method =&amp;gt; 'foo'
    r.add '/these/:routes/are/:sweet', :class =&amp;gt; 'Upload', :method =&amp;gt; 'start'
    r.add '/:class/:method/:id', {}
  end
&lt;/pre&gt;
This set of routes will be compiled into a route matching lambda on startup of Merb. So the above routes will generate and define a method for matching incoming requests with this lambda as the body of that method:&lt;br /&gt;
&lt;pre&gt;
lambda{|path| 
  if Regexp.new('/foo/(.+)/baz/(.+)') =~ path
    @sections[:bar] = $1
    @sections[:id] = $2
    return {:class=&amp;gt;"Test", :method=&amp;gt;"foo"}.merge(@sections)
  end

  if Regexp.new('/these/(.+)/are/(.+)') =~ path
    @sections[:routes] = $1
    @sections[:sweet] = $2
    return {:class=&amp;gt;"Upload", :method=&amp;gt;"start"}.merge(@sections)
  end

  if Regexp.new('/(.+)/(.+)/(.+)') =~ path
    @sections[:class] = $1
    @sections[:method] = $2
    @sections[:id] = $3
    return {}.merge(@sections)
  end

  return {:class=&amp;gt;'NoRouteFound', :method=&amp;gt;'noroute'}
}
&lt;/pre&gt;

So now lets look at what happens when an incoming request comes in and matches against our router lambda:&lt;br /&gt;
&lt;pre&gt;
  routes = Merb::RouteMatcher.new
  p routes.route_request( "/foo/234/baz/dsdsd")
  routes = Merb::RouteMatcher.new
  p routes.route_request( "/these/234/are/yup")
  routes = Merb::RouteMatcher.new
  p routes.route_request( "/upload/test/12")
  routes = Merb::RouteMatcher.new
  p routes.route_request( '/hdsfvsdfsdfdsf')
&lt;/pre&gt;
Those four route matches will output these four hashes which are then used to instantiate and run your controller classes with the right params. You can see when a route doesn&amp;#8217;t match it routes to the Noroutefound#noroute  controller/action so you can put a custom error handler in place there.&lt;br /&gt;

&lt;pre&gt;
{:class=&amp;gt;"Test", :bar=&amp;gt;"234", :id=&amp;gt;"dsdsd", :method=&amp;gt;"foo"}
{:class=&amp;gt;"Upload", :sweet=&amp;gt;"yup", :routes=&amp;gt;"234", :method=&amp;gt;"start"}
{:class=&amp;gt;"upload", :id=&amp;gt;"12", :method=&amp;gt;"test"}
{:class=&amp;gt;"Norouteround", :method=&amp;gt;"noroute"}
&lt;/pre&gt;
&lt;br /&gt;
I am having a great time working on merb in little spurts. I think it will be very usefull as an additional weapon for your rails app arsenal. Pull out the Merb when you have a page that rails cannot serve fast enough or when you need to handle a ton of file uploads at once without blocking rails for each upload. So please now is the time for feature requests if anyone has any. &lt;br /&gt;&lt;br /&gt;
Trac is here: &lt;a href='http://merb.devjavu.com/'&gt;http://merb.devjavu.com/&lt;/a&gt; and you can get the latest  from svn here: &lt;a href='http://svn.devjavu.com/merb'&gt;http://svn.devjavu.com/merb&lt;/a&gt; and here is the &lt;a href='http://svn.devjavu.com/merb/README'&gt;&lt;span class="caps"&gt;README&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
I have created the infrastructure to make a merb gem easy to build and just got a home for it on rubyforge so by rubyconf you will be able to gem install merb and then use the &amp;#8216;merb&amp;#8217; command line util to start your merb apps.&lt;br /&gt;&lt;br /&gt;
Next feature is easy integration with ActiveRecord or Og ;)</description>
      <pubDate>Sun, 15 Oct 2006 18:47:00 +0000</pubDate>
      <guid>urn:uuid:80767745-0dd2-409c-8556-d839fb3f7573</guid>
      <author>ezmobius</author>
      <link>http://brainspl.at/articles/2006/10/15/merb-gets-cool-routes-and-file-uploads</link>
      <category>mongrel</category>
      <category>merb</category>
      <category>metaprogramming</category>
      <trackback:ping>http://brainspl.at/articles/trackback/3720</trackback:ping>
    </item>
  </channel>
</rss>

