Old Emmanuel Oga's Weblog (new one is at www.emmanueloga.com)

Restfull development simplified

Posted in AutoRest, rails, rest, ruby by emmanueloga on octubre 31, 2007

Today I posted the first public release of AutoRest, a plugin to simplify the development of generic restfull controllers. I have developed it and tested against rails 1.2.5. You can find the project here:

http://code.google.com/p/autorest/

It has NOT a DSL, i didn’t want yet another abstraction over my Controllers (see make_resourceful, resource_controller, resource_controller2), i needed a fast an nice way to manage ever-repeating code that naturally appears on a restfull rails project with many resources.

AutoRest does not use a DSL but simple “method management” (i.e. includes). This may not be as powerfull or elegant as a DSL but the resulting code is easier to follow in my opinion, and fast to customize. The default method implementation and functionality is almost the same as the generated with scaffold_resource, wich meansyou already know how AutoRest works.

For auto-configuration, the plugin monkey-patchs the Rails’ “Resource” class. This only needed if you want to get a fully automatically configured controller (yes, you want that!) without needing to supply any parameter. The patch is very very simple, only storing the Resources objects generated on routes.rb for later usage (rails doesn’t seem to store those objects anywhere once used the to create the routes):

class ActionController::Resources::Resource
alias_method :initialize_before_auto_rest, :initialize

def initialize *args
initialize_before_auto_rest(*args)
AutoRest::RailsResources.add(self) # << This is all I add end end[/sourcecode] The whole idea is to avoid the repetition of the same methods over and over again by including default methods on the needed controller. An average AutoRest managed controller looks like this: [sourcecode language='ruby'] class ZonesController < ApplicationController create_auto_rest end[/sourcecode] The generator bundled with AutoRest creates a views/auto_rest_shared folder where all the default views go. Also it generates a /lib/auto_rest/default_methods directory for the default implementation of methods that will be included on the managed controller. There you can find index.rb for the index method, edit.rb for the edit method, and so on. There is a strong focus on *defaults*, meaning AutoRest gives a lot of smarts defaults but is also very easy to modify the behavior by manual configuration or, in the least case, creation of a custom method (ie if you don't want, say, the default index method, you can define it on the controller).The plugin supports rspec for specing all the generated code, ferret based searches on the resources collection and will_paginate for the index and searches returning many results. I'll write more about how it works later.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: