Permalink
Browse files

Add a note about Sinatra::Base to the README

Edited-By: Ryan Tomayko <rtomayko@gmail.com>
  • Loading branch information...
1 parent f75bee5 commit 536033334f33c6822acb64e18b6cdb4eee2f85fe @sr sr committed with rtomayko Jun 6, 2009
Showing with 50 additions and 0 deletions.
  1. +50 −0 README.rdoc
View
50 README.rdoc
@@ -470,6 +470,56 @@ recommended:
NOTE: The built-in Sinatra::Test module and Sinatra::TestHarness class
are deprecated as of the 0.9.2 release.
+== Sinatra::Base - Middleware, Libraries, and Modular Apps
+
+Defining your app at the top-level works well for micro-apps but has
+considerable drawbacks when building reuseable components such as Rack
+middleware, Rails metal, simple libraries with a server component, or
+even Sinatra extensions. The top-level DSL pollutes the Object namespace
+and assumes a micro-app style configuration (e.g., a single application
+file, ./public and ./views directories, logging, exception detail page,
+etc.). That's where Sinatra::Base comes into play:
+
+ require 'sinatra/base'
+
+ class MyApp < Sinatra::Base
+ set :sessions, true
+ set :foo, 'bar'
+
+ get '/' do
+ 'Hello world!'
+ end
+ end
+
+The MyApp class is an independent Rack component that can act as
+Rack middleware, a Rack application, or Rails metal. You can +use+ or
++run+ this class from a rackup +config.ru+ file; or, control a server
+component shipped as a library:
+
+ MyApp.run! :host => 'localhost', :port => 9090
+
+The methods available to Sinatra::Base subclasses are exactly as those
+available via the top-level DSL. Most top-level apps can be converted to
+Sinatra::Base components with two modifications:
+
+* Your file should require +sinatra/base+ instead of +sinatra+;
+ otherwise, all of Sinatra's DSL methods are imported into the main
+ namespace.
+* Put your app's routes, error handlers, filters, and options in a subclass
+ of Sinatra::Base.
+
++Sinatra::Base+ is a blank slate. Most options are disabled by default,
+including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
+for details on available options and their behavior.
+
+SIDEBAR: Sinatra's top-level DSL is implemented using a simple delegation
+system. The +Sinatra::Application+ class -- a special subclass of
+Sinatra::Base -- receives all :get, :put, :post, :delete, :before,
+:error, :not_found, :configure, and :set messages sent to the
+top-level. Have a look at the code for yourself: here's the
+{Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1064]
+being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/master/lib/sinatra/main.rb#L25].
+
== Command line
Sinatra applications can be run directly:

1 comment on commit 5360333

@benschwarz

Great documentation patch. I struggled to work this stuff out before I decided to dedicate a few hours to putting a middleware together.

Please sign in to comment.