Permalink
Browse files

document Sinatra.new

  • Loading branch information...
1 parent ae5878d commit 6d86f7527eed26eeae69e3d74fd6b6144f67cc18 @rkh rkh committed Mar 28, 2011
Showing with 40 additions and 0 deletions.
  1. +40 −0 README.rdoc
View
@@ -1619,6 +1619,45 @@ application (Rails/Ramaze/Camping/...):
get('/') { "Hello #{session['user_name']}." }
end
+=== Dynamic Application Creation
+
+Sometimes you want to create new applications at runtime without having to
+assign them to a constant, you can do this with `Sinatra.new`:
+
+ require 'sinatra/base'
+ my_app = Sinatra.new { get('/') { "hi" } }
+ my_app.run!
+
+It takes the application to inherit from as optional argument:
+
+ require 'sinatra/base'
+
+ controller = Sinatra.new do
+ enable :logging
+ helpers MyHelpers
+ end
+
+ map('/a') do
+ run Sinatra.new(controller) { get('/') { 'a' } }
+ end
+
+ map('/b') do
+ run Sinatra.new(controller) { get('/') { 'b' } }
+ end
+
+This is especially useful for testing Sinatra extensions or using Sinatra in
+your own library.
+
+This also makes using Sinatra as middleware extremely easy:
+
+ require 'sinatra/base'
+
+ use Sinatra do
+ get('/') { ... }
+ end
+
+ run RailsProject::Application
+
== Scopes and Binding
The scope you are currently in determines what methods and variables are
@@ -1651,6 +1690,7 @@ You have the application scope binding inside:
* Methods defined by extensions
* The block passed to +helpers+
* Procs/blocks used as value for +set+
+* The block passed to <tt>Sinatra.new</tt>
You can reach the scope object (the class) like this:

0 comments on commit 6d86f75

Please sign in to comment.