A sweet plugin to change your Rails app into a Sinatra app. First, install the plugin in your existing Rails app:
cd vendor/plugins git clone git://github.com/nateware/sinatra_from_rails.git
To generate a “classic” (single-file) app:
rake sinatra:from_rails:classic # writes to main.rb
To generate a modular app:
rake sinatra:from_rails:modular # writes to app/routes
To change the name of the output file for a classic app (default is main.rb):
rake sinatra:from_rails:classic OUTPUT_FILE=application.rb
To change the output directory for a modular app, and the name of the class:
rake sinatra:from_rails:classic OUTPUT_DIR=app/services CLASS_NAME=Services
The following options can be passed to Sinatra::FromRails.convert
. To pass them to the rake task, simply pass them as uppercase to the rake command line. For example:
rake sinatra:from_rails:classic ANY_REQUEST_METHOD=post HEAD_ERROR='raise "Server Error"'
Is the same as
Sinatra::FromRails.convert(:style => :classic, :any_request_method => :post, :head_error => 'raise "Server Error"')
Available Sinatra::FromRails settings with their defaults:
:debug DEBUG false :format FORMAT html :style STYLE classic :output_file OUTPUT_FILE main.rb :output_dir OUTPUT_DIR app/routes :class_name CLASS_NAME Main :controllers CONTROLLERS nil :ignore_routes IGNORE_ROUTES [] :ignore_actions IGNORE_ACTIONS [] :any_request_method ANY_REQUEST_METHOD post :render RENDER erb :head_ok HEAD_OK halt 200 :head_error HEAD_ERROR halt 500
To cutdown on having to specify those every time, define a “convert” task in your local tasks file:
# lib/tasks/convert_tasks.rake namespace :sinatra do namespace :from_rails do task :convert => :load do # Customize this part Sinatra::FromRails.convert(:format => :xml, :style => :modular) end end end
Then:
rake sinatra:from_rails:convert
Viola!
My team at PlayStation writes a ton of XML/JSON services to interact with PS3/PSP game clients. These need to run fast, and at the view layer, just spit out Builder files. We also use the same Rails stack to provide community features - forums, stats, etc. For these, the extended Rails helpers are a big benefit.
As such, we develop our app in Rails, then use this to spit out a Sinatra app for the XML/JSON services.
This plugin works about 95%. If you’re scaffolding, you’re good to go. If you have tons of controller code and weird lambda blocks and such you might have troubles. But you don’t have tons of controller code, right? That’s in your models.
Copyright © 2009 Nate Wiger, nate.wiger.org, released under the Artistic License