sinatra 1.1.4 → 1.2.0.a

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of sinatra might be problematic. Click here for more details.

data/README.fr.rdoc CHANGED
@@ -425,6 +425,19 @@ Le gem markaby est nécessaire pour utiliser la fonction de rendu Markaby:
425
425
 
426
426
  Utilisera <tt>./views/index.mab</tt>.
427
427
 
428
+ === Templates Slim
429
+
430
+ Le gem slim est nécessaire pour utiliser la fonction de rendu Slim:
431
+
432
+ ## Chargez la bibliothèque slim dans votre application
433
+ require 'slim'
434
+
435
+ get '/' do
436
+ slim :index
437
+ end
438
+
439
+ Utilisera <tt>./views/index.slim</tt>.
440
+
428
441
  === Templates CoffeeScript
429
442
 
430
443
  Le gem coffee-script et l'exécutable `coffee` sont nécessaires pour utiliser la
data/README.jp.rdoc CHANGED
@@ -419,6 +419,19 @@ Markabyテンプレートを使うにはmarkabyライブラリが必要です:
419
419
 
420
420
  <tt>./views/index.mab</tt>を表示します。
421
421
 
422
+ === Slim テンプレート
423
+
424
+ Slimテンプレートを使うにはslimライブラリが必要です:
425
+
426
+ ## slimを読み込みます
427
+ require 'slim'
428
+
429
+ get '/' do
430
+ slim :index
431
+ end
432
+
433
+ <tt>./views/index.slim</tt>を表示します。
434
+
422
435
  === CoffeeScript テンプレート
423
436
 
424
437
  CoffeeScriptテンプレートを表示するにはcoffee-scriptライブラリと`coffee`バイナリが必要です:
data/README.rdoc CHANGED
@@ -37,6 +37,10 @@ Each route is associated with a block:
37
37
  delete '/' do
38
38
  .. annihilate something ..
39
39
  end
40
+
41
+ options '/' do
42
+ .. appease something ..
43
+ end
40
44
 
41
45
  Routes are matched in the order they are defined. The first route that
42
46
  matches the request is invoked.
@@ -119,7 +123,7 @@ You can easily define your own conditions:
119
123
  "Sorry, you lost."
120
124
  end
121
125
 
122
- === Return Values
126
+ === Return values
123
127
 
124
128
  The return value of a route block determines at least the response body passed
125
129
  on to the HTTP client, or at least the next middleware in the Rack stack.
@@ -170,7 +174,7 @@ directly.
170
174
 
171
175
  === Haml Templates
172
176
 
173
- The <tt>haml</tt> gem/library is required to render HAML templates:
177
+ The haml gem/library is required to render HAML templates:
174
178
 
175
179
  ## You'll need to require haml in your app
176
180
  require 'haml'
@@ -202,11 +206,11 @@ and overridden on an individual basis.
202
206
  erb :index
203
207
  end
204
208
 
205
- Renders <tt>./views/index.erb</tt>.
209
+ Renders <tt>./views/index.erb</tt>
206
210
 
207
- === Erubis Templates
211
+ === Erubis
208
212
 
209
- The <tt>erubis</tt> gem/library is required to render Erubis templates:
213
+ The erubis gem/library is required to render erubis templates:
210
214
 
211
215
  ## You'll need to require erubis in your app
212
216
  require 'erubis'
@@ -215,22 +219,11 @@ The <tt>erubis</tt> gem/library is required to render Erubis templates:
215
219
  erubis :index
216
220
  end
217
221
 
218
- Renders <tt>./views/index.erubis</tt>.
219
-
220
- It is also possible to replace Erb with Erubis:
221
-
222
- require 'erubis'
223
- Tilt.register :erb, Tilt[:erubis]
224
-
225
- get '/' do
226
- erb :index
227
- end
228
-
229
- Renders <tt>./views/index.erb</tt> with Erubis.
222
+ Renders <tt>./views/index.erubis</tt>
230
223
 
231
224
  === Builder Templates
232
225
 
233
- The <tt>builder</tt> gem/library is required to render builder templates:
226
+ The builder gem/library is required to render builder templates:
234
227
 
235
228
  ## You'll need to require builder in your app
236
229
  require 'builder'
@@ -243,7 +236,7 @@ Renders <tt>./views/index.builder</tt>.
243
236
 
244
237
  === Nokogiri Templates
245
238
 
246
- The <tt>nokogiri</tt> gem/library is required to render nokogiri templates:
239
+ The nokogiri gem/library is required to render nokogiri templates:
247
240
 
248
241
  ## You'll need to require nokogiri in your app
249
242
  require 'nokogiri'
@@ -256,7 +249,7 @@ Renders <tt>./views/index.nokogiri</tt>.
256
249
 
257
250
  === Sass Templates
258
251
 
259
- The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Sass templates:
252
+ The haml gem/library is required to render Sass templates:
260
253
 
261
254
  ## You'll need to require haml or sass in your app
262
255
  require 'sass'
@@ -280,7 +273,7 @@ and overridden on an individual basis.
280
273
 
281
274
  === Scss Templates
282
275
 
283
- The <tt>haml</tt> or <tt>sass</tt> gem/library is required to render Scss templates:
276
+ The haml gem/library is required to render Scss templates:
284
277
 
285
278
  ## You'll need to require haml or sass in your app
286
279
  require 'sass'
@@ -304,7 +297,7 @@ and overridden on an individual basis.
304
297
 
305
298
  === Less Templates
306
299
 
307
- The <tt>less</tt> gem/library is required to render Less templates:
300
+ The less gem/library is required to render Less templates:
308
301
 
309
302
  ## You'll need to require less in your app
310
303
  require 'less'
@@ -317,7 +310,7 @@ Renders <tt>./views/stylesheet.less</tt>.
317
310
 
318
311
  === Liquid Templates
319
312
 
320
- The <tt>liquid</tt> gem/library is required to render Liquid templates:
313
+ The liquid gem/library is required to render Liquid templates:
321
314
 
322
315
  ## You'll need to require liquid in your app
323
316
  require 'liquid'
@@ -335,7 +328,7 @@ template, you almost always want to pass locals to it:
335
328
 
336
329
  === Markdown Templates
337
330
 
338
- The <tt>rdiscount</tt> gem/library is required to render Markdown templates:
331
+ The rdiscount gem/library is required to render Markdown templates:
339
332
 
340
333
  ## You'll need to require rdiscount in your app
341
334
  require "rdiscount"
@@ -347,34 +340,18 @@ The <tt>rdiscount</tt> gem/library is required to render Markdown templates:
347
340
  Renders <tt>./views/index.markdown</tt> (+md+ and +mkd+ are also valid file
348
341
  extensions).
349
342
 
350
- It is not possible to call methods from markdown, nor to pass locals to it.
351
- You therefore will usually use it in combination with another rendering
352
- engine:
343
+ It is not possible to call methods from markdown, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
353
344
 
354
345
  erb :overview, :locals => { :text => markdown(:introduction) }
355
346
 
356
- Note that you may also call the +markdown+ method from within other templates:
347
+ Note that you may also call the markdown method from within other templates:
357
348
 
358
349
  %h1 Hello From Haml!
359
350
  %p= markdown(:greetings)
360
351
 
361
- It is also possible to parse Markdown with BlueCloth rather than RDiscount:
362
-
363
- require 'bluecloth'
364
-
365
- Tilt.register 'markdown', BlueClothTemplate
366
- Tilt.register 'mkd', BlueClothTemplate
367
- Tilt.register 'md', BlueClothTemplate
368
-
369
- get '/' do
370
- markdown :index
371
- end
372
-
373
- Renders <tt>./views/index.md</tt> with BlueCloth.
374
-
375
352
  === Textile Templates
376
353
 
377
- The <tt>RedCloth</tt> gem/library is required to render Textile templates:
354
+ The RedCloth gem/library is required to render Textile templates:
378
355
 
379
356
  ## You'll need to require redcloth in your app
380
357
  require "redcloth"
@@ -385,19 +362,18 @@ The <tt>RedCloth</tt> gem/library is required to render Textile templates:
385
362
 
386
363
  Renders <tt>./views/index.textile</tt>.
387
364
 
388
- It is not possible to call methods from textile, nor to pass locals to it. You
389
- therefore will usually use it in combination with another rendering engine:
365
+ It is not possible to call methods from textile, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
390
366
 
391
367
  erb :overview, :locals => { :text => textile(:introduction) }
392
368
 
393
- Note that you may also call the +textile+ method from within other templates:
369
+ Note that you may also call the textile method from within other templates:
394
370
 
395
371
  %h1 Hello From Haml!
396
372
  %p= textile(:greetings)
397
373
 
398
374
  === RDoc Templates
399
375
 
400
- The <tt>rdoc</tt> gem/library is required to render RDoc templates:
376
+ The RDoc gem/library is required to render RDoc templates:
401
377
 
402
378
  ## You'll need to require rdoc in your app
403
379
  require "rdoc"
@@ -408,19 +384,18 @@ The <tt>rdoc</tt> gem/library is required to render RDoc templates:
408
384
 
409
385
  Renders <tt>./views/index.rdoc</tt>.
410
386
 
411
- It is not possible to call methods from rdoc, nor to pass locals to it. You
412
- therefore will usually use it in combination with another rendering engine:
387
+ It is not possible to call methods from rdoc, nor to pass locals to it. You therefore will usually use it in combination with another rendering engine:
413
388
 
414
389
  erb :overview, :locals => { :text => rdoc(:introduction) }
415
390
 
416
- Note that you may also call the +rdoc+ method from within other templates:
391
+ Note that you may also call the rdoc method from within other templates:
417
392
 
418
393
  %h1 Hello From Haml!
419
394
  %p= rdoc(:greetings)
420
395
 
421
396
  === Radius Templates
422
397
 
423
- The <tt>radius</tt> gem/library is required to render Radius templates:
398
+ The radius gem/library is required to render Radius templates:
424
399
 
425
400
  ## You'll need to require radius in your app
426
401
  require 'radius'
@@ -438,7 +413,7 @@ template, you almost always want to pass locals to it:
438
413
 
439
414
  === Markaby Templates
440
415
 
441
- The <tt>markaby</tt> gem/library is required to render Markaby templates:
416
+ The markaby gem/library is required to render Markaby templates:
442
417
 
443
418
  ## You'll need to require markaby in your app
444
419
  require 'markaby'
@@ -449,18 +424,29 @@ The <tt>markaby</tt> gem/library is required to render Markaby templates:
449
424
 
450
425
  Renders <tt>./views/index.mab</tt>.
451
426
 
452
- === CoffeeScript Templates
427
+ If you have Tilt 1.2 or later, you may also use inline markaby:
428
+
429
+ get '/' do
430
+ markaby { h1 "Welcome!" }
431
+ end
453
432
 
454
- The <tt>coffee-script</tt> gem/library and at least <b>one</b> of the
455
- following options to execute JavaScript:
433
+ === Slim Templates
456
434
 
457
- * +node+ (from Node.js) in your path
458
- * you must be running on OSX
459
- * +therubyracer+ gem/library
435
+ The slim gem/library is required to render Slim templates:
460
436
 
461
- See http://github.com/josh/ruby-coffee-script for an updated list of options.
437
+ ## You'll need to require slim in your app
438
+ require 'slim'
462
439
 
463
- Now you can render CoffeeScript templates:
440
+ get '/' do
441
+ slim :index
442
+ end
443
+
444
+ Renders <tt>./views/index.slim</tt>.
445
+
446
+ === CoffeeScript Templates
447
+
448
+ The coffee-script gem/library and the `coffee` binary are required to render
449
+ CoffeeScript templates:
464
450
 
465
451
  ## You'll need to require coffee-script in your app
466
452
  require 'coffee-script'
@@ -471,13 +457,13 @@ Now you can render CoffeeScript templates:
471
457
 
472
458
  Renders <tt>./views/application.coffee</tt>.
473
459
 
474
- === Embedded Templates
460
+ === Inline Templates
475
461
 
476
462
  get '/' do
477
463
  haml '%div.title Hello World'
478
464
  end
479
465
 
480
- Renders the embedded template string.
466
+ Renders the inlined template string.
481
467
 
482
468
  === Accessing Variables in Templates
483
469
 
@@ -539,38 +525,12 @@ Templates may also be defined using the top-level <tt>template</tt> method:
539
525
  end
540
526
 
541
527
  If a template named "layout" exists, it will be used each time a template
542
- is rendered. You can individually disable layouts by passing <tt>:layout => false</tt>
543
- or disable them by default via <tt>set :haml, :layout => false</tt>.
528
+ is rendered. You can disable layouts by passing <tt>:layout => false</tt>.
544
529
 
545
530
  get '/' do
546
531
  haml :index, :layout => !request.xhr?
547
532
  end
548
533
 
549
- === Associating File Extensions
550
-
551
- To associate a file extension with a template engine, use
552
- <tt>Tilt.register</tt>. For instance, if you like to use the file extension
553
- +tt+ for Textile templates, you can do the following:
554
-
555
- Tilt.register :tt, Tilt[:textile]
556
-
557
- === Adding You Own Template Engine
558
-
559
- First, register your engine with Tilt, then create a rendering method:
560
-
561
- Tilt.register :myat, MyAwesomeTemplateEngine
562
-
563
- helpers do
564
- def myat(*args) render(:myat, *args) end
565
- end
566
-
567
- get '/' do
568
- myat :index
569
- end
570
-
571
- Renders <tt>./views/index.myat</tt>. See https://github.com/rtomayko/tilt to
572
- learn more about Tilt.
573
-
574
534
  == Helpers
575
535
 
576
536
  Use the top-level <tt>helpers</tt> method to define helper methods for use in
@@ -610,10 +570,6 @@ and routes are accessible by after filters:
610
570
  puts response.status
611
571
  end
612
572
 
613
- Note: Unless you use the `body` method rather than just returning a String from
614
- the routes, the body will not yet be available in the after filter, since it is
615
- generated later on.
616
-
617
573
  Filters optionally taking a pattern, causing them to be evaluated only if the
618
574
  request path matches that pattern:
619
575
 
@@ -625,6 +581,16 @@ request path matches that pattern:
625
581
  session[:last_slug] = slug
626
582
  end
627
583
 
584
+ Like routes, filters also take conditions:
585
+
586
+ before :agent => /Songbird/ do
587
+ # ...
588
+ end
589
+
590
+ after '/blog/*', :host_name => 'example.com' do
591
+ # ...
592
+ end
593
+
628
594
  == Halting
629
595
 
630
596
  To immediately stop a request within a filter or route use:
@@ -715,20 +681,7 @@ The <tt>request.body</tt> is an IO or StringIO object:
715
681
  Run once, at startup, in any environment:
716
682
 
717
683
  configure do
718
- # setting one option
719
- set :option, 'value'
720
-
721
- # setting multiple options
722
- set :a => 1, :b => 2
723
-
724
- # same as `set :option, true`
725
- enable :option
726
-
727
- # same as `set :option, false`
728
- disable :option
729
-
730
- # you can also have dynamic settings with blocks
731
- set(:css_dir) { File.join(views, 'css') }
684
+ ...
732
685
  end
733
686
 
734
687
  Run only when the environment (RACK_ENV environment variable) is set to
@@ -745,19 +698,7 @@ Run when the environment is set to either <tt>:production</tt> or
745
698
  ...
746
699
  end
747
700
 
748
- You can access those options via <tt>settings</tt>:
749
-
750
- configure do
751
- set :foo, 'bar'
752
- end
753
-
754
- get '/' do
755
- settings.foo? # => true
756
- settings.foo # => 'bar'
757
- ...
758
- end
759
-
760
- == Error Handling
701
+ == Error handling
761
702
 
762
703
  Error handlers run within the same context as routes and before filters, which
763
704
  means you get all the goodies it has to offer, like <tt>haml</tt>,
@@ -817,7 +758,7 @@ Or a range:
817
758
  Sinatra installs special <tt>not_found</tt> and <tt>error</tt> handlers when
818
759
  running under the development environment.
819
760
 
820
- == Mime Types
761
+ == Mime types
821
762
 
822
763
  When using <tt>send_file</tt> or static files you may have mime types Sinatra
823
764
  doesn't understand. Use +mime_type+ to register them by file extension:
@@ -920,6 +861,13 @@ etc.). That's where Sinatra::Base comes into play:
920
861
  end
921
862
  end
922
863
 
864
+ The MyApp class is an independent Rack component that can act as
865
+ Rack middleware, a Rack application, or Rails metal. You can +use+ or
866
+ +run+ this class from a rackup +config.ru+ file; or, control a server
867
+ component shipped as a library:
868
+
869
+ MyApp.run! :host => 'localhost', :port => 9090
870
+
923
871
  The methods available to Sinatra::Base subclasses are exactly as those
924
872
  available via the top-level DSL. Most top-level apps can be converted to
925
873
  Sinatra::Base components with two modifications:
@@ -934,91 +882,6 @@ Sinatra::Base components with two modifications:
934
882
  including the built-in server. See {Options and Configuration}[http://sinatra.github.com/configuration.html]
935
883
  for details on available options and their behavior.
936
884
 
937
- === Modular vs. Classic Style
938
-
939
- Contrary to common believes, there is nothing wrong with classic style. If it
940
- suits your application, you do not have to switch to a modular application.
941
-
942
- There are only two downsides compared to modulare style:
943
-
944
- * You may only have one Sinatra application per Ruby process - if you plan to
945
- use more, switch to modular style.
946
-
947
- * Classic style pollutes Object with delegator methods - if you plan to ship
948
- your application in a library/gem, switch to modular style.
949
-
950
- There is no reason you cannot mix modular and classic style.
951
-
952
- If switching from one style to the other, you should be aware of slight
953
- differences in the setting:
954
-
955
- Setting Classic Modular
956
-
957
- app_file file loading sinatra nil
958
- run $0 == app_file false
959
- logging true false
960
- method_override true false
961
- inline_templates true false
962
-
963
-
964
- === Serving a Modular Application
965
-
966
- There are two common options for starting a modular app, activly starting with
967
- <tt>run!</tt>:
968
-
969
- # my_app.rb
970
- require 'sinatra/base'
971
-
972
- class MyApp < Sinatra::Base
973
- # ... app code here ...
974
-
975
- # start the server if ruby file executed directly
976
- run! if app_file == $0
977
- end
978
-
979
- Start with:
980
-
981
- ruby my_app.rb
982
-
983
- Or with a <tt>config.ru</tt>, which allows using any Rack handler:
984
-
985
- # config.ru
986
- require 'my_app'
987
- run MyApp
988
-
989
- Run:
990
-
991
- rackup -p 4567
992
-
993
- === Using a Classic Style Application with a config.ru
994
-
995
- Write your app file:
996
-
997
- # app.rb
998
- require 'sinatra'
999
-
1000
- get '/' do
1001
- 'Hello world!'
1002
- end
1003
-
1004
- And a corresponding <tt>config.ru</tt>:
1005
-
1006
- require 'app'
1007
- run Sinatra::Application
1008
-
1009
- === When to use a config.ru?
1010
-
1011
- Good signs you probably want to use a <tt>config.ru</tt>:
1012
-
1013
- * You want to deploy with a different Rack handler (Passenger, Unicorn,
1014
- Heroku, ...).
1015
- * You want to use more than one subclass of <tt>Sinatra::Base</tt>.
1016
- * You want to use Sinatra only for middleware, but not as endpoint.
1017
-
1018
- <b>There is no need to switch to a <tt>config.ru</tt> only because you
1019
- switched to modular style, and you don't have to use modular style for running
1020
- with a <tt>config.ru</tt>.</b>
1021
-
1022
885
  === Using Sinatra as Middleware
1023
886
 
1024
887
  Not only is Sinatra able to use other Rack middleware, any Sinatra application
@@ -1118,7 +981,7 @@ scope via the `settings` helper:
1118
981
 
1119
982
  You have the request scope binding inside:
1120
983
 
1121
- * get/head/post/put/delete blocks
984
+ * get/head/post/put/delete/options blocks
1122
985
  * before/after filters
1123
986
  * helper methods
1124
987
  * templates/views
@@ -1141,7 +1004,7 @@ Have a look at the code for yourself: here's the
1141
1004
  {Sinatra::Delegator mixin}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/base.rb#L1128]
1142
1005
  being {included into the main namespace}[http://github.com/sinatra/sinatra/blob/ceac46f0bc129a6e994a06100aa854f606fe5992/lib/sinatra/main.rb#L28].
1143
1006
 
1144
- == Command Line
1007
+ == Command line
1145
1008
 
1146
1009
  Sinatra applications can be run directly:
1147
1010
 
@@ -1157,67 +1020,32 @@ Options are:
1157
1020
  -x # turn on the mutex lock (default is off)
1158
1021
 
1159
1022
  == The Bleeding Edge
1160
- If you would like to use Sinatra's latest bleeding code, feel free to run your
1161
- application against the master branch, it should be rather stable.
1162
-
1163
- We also push out prerelease gems from time to time, so you can do a
1164
-
1165
- gem install sinatra --pre
1166
-
1167
- To get some of the latest features.
1168
-
1169
- === With Bundler
1170
- If you want to run your application with the latest Sinatra, using
1171
- {Bundler}[http://gembundler.com/] is the recommend way.
1172
-
1173
- First, install bundler, if you haven't:
1174
-
1175
- gem install bundler
1176
1023
 
1177
- Then, in you project directory, create a +Gemfile+:
1178
-
1179
- source :rubygems
1180
- gem 'sinatra', :git => "git://github.com/sinatra/sinatra.git"
1181
-
1182
- # other dependencies
1183
- gem 'haml' # for instance, if you use haml
1184
- gem 'activerecord', '~> 3.0' # maybe you also need ActiveRecord 3.x
1185
-
1186
- Note that you will have to list all your applications dependencies in there.
1187
- Sinatra's direct dependencies (Rack and Tilt) will however be automatically
1188
- fetched and added by Bundler.
1189
-
1190
- Now you can run your app like this:
1191
-
1192
- bundle exec ruby myapp.rb
1193
-
1194
- === Roll Your Own
1195
- Create a local clone and run your app with the <tt>sinatra/lib</tt> directory
1196
- on the <tt>LOAD_PATH</tt>:
1024
+ If you would like to use Sinatra's latest bleeding code, create a local
1025
+ clone and run your app with the <tt>sinatra/lib</tt> directory on the
1026
+ <tt>LOAD_PATH</tt>:
1197
1027
 
1198
1028
  cd myapp
1199
1029
  git clone git://github.com/sinatra/sinatra.git
1200
1030
  ruby -Isinatra/lib myapp.rb
1201
1031
 
1202
- To update the Sinatra sources in the future:
1032
+ Alternatively, you can add the <tt>sinatra/lib</tt> directory to the
1033
+ <tt>LOAD_PATH</tt> in your application:
1203
1034
 
1204
- cd myapp/sinatra
1205
- git pull
1206
-
1207
- === Install Globally
1208
-
1209
- You can build the gem on your own:
1035
+ $LOAD_PATH.unshift File.dirname(__FILE__) + '/sinatra/lib'
1036
+ require 'rubygems'
1037
+ require 'sinatra'
1210
1038
 
1211
- git clone git://github.com/sinatra/sinatra.git
1212
- cd sinatra
1213
- rake sinatra.gemspec
1214
- rake install
1039
+ get '/about' do
1040
+ "I'm running version " + Sinatra::VERSION
1041
+ end
1215
1042
 
1216
- If you install gems as root, the last step should be
1043
+ To update the Sinatra sources in the future:
1217
1044
 
1218
- sudo rake install
1045
+ cd myproject/sinatra
1046
+ git pull
1219
1047
 
1220
- == Further Reading
1048
+ == More
1221
1049
 
1222
1050
  * {Project Website}[http://www.sinatrarb.com/] - Additional documentation,
1223
1051
  news, and links to other resources.