Merb gets cool Routes and file uploads
Posted by ezmobius Sun, 15 Oct 2006 18:47:00 GMT
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.So here is a simple Merb route definition:
Merb::RouteMatcher.prepare do |r|
r.add '/foo/:bar/baz/:id', :class => 'Test', :method => 'foo'
r.add '/these/:routes/are/:sweet', :class => 'Upload', :method => 'start'
r.add '/:class/:method/:id', {}
end
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:
lambda{|path|
if Regexp.new('/foo/(.+)/baz/(.+)') =~ path
@sections[:bar] = $1
@sections[:id] = $2
return {:class=>"Test", :method=>"foo"}.merge(@sections)
end
if Regexp.new('/these/(.+)/are/(.+)') =~ path
@sections[:routes] = $1
@sections[:sweet] = $2
return {:class=>"Upload", :method=>"start"}.merge(@sections)
end
if Regexp.new('/(.+)/(.+)/(.+)') =~ path
@sections[:class] = $1
@sections[:method] = $2
@sections[:id] = $3
return {}.merge(@sections)
end
return {:class=>'NoRouteFound', :method=>'noroute'}
}
So now lets look at what happens when an incoming request comes in and matches against our router lambda: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')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’t match it routes to the Noroutefound#noroute controller/action so you can put a custom error handler in place there.
{:class=>"Test", :bar=>"234", :id=>"dsdsd", :method=>"foo"}
{:class=>"Upload", :sweet=>"yup", :routes=>"234", :method=>"start"}
{:class=>"upload", :id=>"12", :method=>"test"}
{:class=>"Norouteround", :method=>"noroute"}
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.
Trac is here: http://merb.devjavu.com/ and you can get the latest from svn here: http://svn.devjavu.com/merb and here is the README
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 ‘merb’ command line util to start your merb apps.
Next feature is easy integration with ActiveRecord or Og ;)
Searching...




