Permalink
Browse files

Merge hoboken branch for 0.9.0 development

  • Loading branch information...
2 parents 4aefc7d + eef4cb3 commit 8b8de75eb1f91b3e5b258db23eac8587519dd21d @rtomayko rtomayko committed Jan 7, 2009
Showing with 3,463 additions and 2,274 deletions.
  1. +3 −0 .gitignore
  2. +27 −46 README.rdoc
  3. +21 −8 Rakefile
  4. +11 −10 {test → compat}/app_test.rb
  5. +10 −5 {test → compat}/application_test.rb
  6. +101 −0 compat/builder_test.rb
  7. 0 {test → compat}/custom_error_test.rb
  8. +136 −0 compat/erb_test.rb
  9. +12 −2 {test → compat}/events_test.rb
  10. +30 −0 compat/filter_test.rb
  11. +233 −0 compat/haml_test.rb
  12. +18 −0 compat/helper.rb
  13. +72 −0 compat/mapped_error_test.rb
  14. +9 −4 {test → compat}/pipeline_test.rb
  15. 0 {test → compat}/public/foo.xml
  16. +57 −0 compat/sass_test.rb
  17. 0 {test → compat}/sessions_test.rb
  18. +4 −1 {test → compat}/streaming_test.rb
  19. 0 {test → compat}/sym_params_test.rb
  20. 0 {test → compat}/template_test.rb
  21. 0 {test → compat}/use_in_file_templates_test.rb
  22. 0 {test → compat}/views/foo.builder
  23. 0 {test → compat}/views/foo.erb
  24. 0 {test → compat}/views/foo.haml
  25. 0 {test → compat}/views/foo.sass
  26. 0 {test → compat}/views/foo_layout.erb
  27. 0 {test → compat}/views/foo_layout.haml
  28. 0 {test → compat}/views/layout_test/foo.builder
  29. 0 {test → compat}/views/layout_test/foo.erb
  30. 0 {test → compat}/views/layout_test/foo.haml
  31. 0 {test → compat}/views/layout_test/foo.sass
  32. 0 {test → compat}/views/layout_test/layout.builder
  33. 0 {test → compat}/views/layout_test/layout.erb
  34. 0 {test → compat}/views/layout_test/layout.haml
  35. 0 {test → compat}/views/layout_test/layout.sass
  36. 0 {test → compat}/views/no_layout/no_layout.builder
  37. 0 {test → compat}/views/no_layout/no_layout.haml
  38. +3 −1,486 lib/sinatra.rb
  39. +775 −0 lib/sinatra/base.rb
  40. +141 −0 lib/sinatra/compat.rb
  41. BIN { → lib/sinatra}/images/404.png
  42. BIN { → lib/sinatra}/images/500.png
  43. +48 −0 lib/sinatra/main.rb
  44. +110 −0 lib/sinatra/test.rb
  45. +0 −76 lib/sinatra/test/methods.rb
  46. +1 −9 lib/sinatra/test/rspec.rb
  47. +1 −9 lib/sinatra/test/spec.rb
  48. +3 −5 lib/sinatra/test/unit.rb
  49. +65 −35 sinatra.gemspec
  50. +49 −0 test/base_test.rb
  51. +56 −89 test/builder_test.rb
  52. +3 −0 test/data/reload_app_file.rb
  53. +44 −125 test/erb_test.rb
  54. +0 −15 test/event_context_test.rb
  55. +29 −20 test/filter_test.rb
  56. +57 −218 test/haml_test.rb
  57. +0 −9 test/helper.rb
  58. +368 −0 test/helpers_test.rb
  59. +121 −52 test/mapped_error_test.rb
  60. +63 −0 test/middleware_test.rb
  61. +103 −0 test/options_test.rb
  62. +65 −0 test/reload_test.rb
  63. +11 −0 test/request_test.rb
  64. +92 −0 test/result_test.rb
  65. +305 −0 test/routing_test.rb
  66. +33 −50 test/sass_test.rb
  67. +15 −0 test/sinatra_test.rb
  68. +60 −0 test/static_test.rb
  69. +84 −0 test/templates_test.rb
  70. +1 −0 test/views/hello.builder
  71. +1 −0 test/views/hello.erb
  72. +1 −0 test/views/hello.haml
  73. +2 −0 test/views/hello.sass
  74. +1 −0 test/views/hello.test
  75. +3 −0 test/views/layout2.builder
  76. +2 −0 test/views/layout2.erb
  77. +2 −0 test/views/layout2.haml
  78. +1 −0 test/views/layout2.test
View
@@ -4,3 +4,6 @@
/book
/doc/api
/doc/*.html
+.#*
+\#*
+.emacs*
View
@@ -36,16 +36,12 @@ Run with <tt>ruby myapp.rb</tt> and view at <tt>http://localhost:4567</tt>
end
-NOTE: <tt>put</tt> and <tt>delete</tt> are also triggered when a
-<tt>_method</tt> parameter is set to PUT or DELETE and the HTTP request method
-is POST
-
== Routes
Routes are matched based on the order of declaration. The first route that
matches the request is invoked.
-Simple:
+Basic routes:
get '/hi' do
...
@@ -54,19 +50,20 @@ Simple:
Named parameters:
get '/:name' do
- # matches /sinatra and the like and sets params[:name]
+ # matches "GET /foo" and "GET /bar"
+ # params[:name] is 'foo' or 'bar'
end
Splat parameters:
get '/say/*/to/*' do
# matches /say/hello/to/world
- params["splat"] # => ["hello", "world"]
+ params[:splat] # => ["hello", "world"]
end
get '/download/*.*' do
# matches /download/path/to/file.xml
- params["splat"] # => ["path/to/file", "xml"]
+ params[:splat] # => ["path/to/file", "xml"]
end
User agent matching:
@@ -76,25 +73,29 @@ User agent matching:
end
get '/foo' do
- # matches non-songbird browsers
+ # Matches non-songbird browsers
end
-== Static files
+== Static Files
Put all of your static content in the ./public directory
root
\ public
-If a file exists that maps to the REQUEST_PATH then it is served and the
-request ends. Otherwise, Sinatra will look for an event that matches the
-path.
+To use a different static directory:
+
+ set :public, File.dirname(__FILE__) + '/static'
== Views
-Views are searched for in a "views" directory in the same location as
+Views are searched for in a ./views directory in the same location as
your main application.
+To use a different views directory:
+
+ set :views, File.dirname(__FILE__) + '/templates'
+
=== Haml Templates
get '/' do
@@ -190,8 +191,7 @@ method:
== Helpers
-The top-level <tt>helpers</tt> method takes a block and extends all
-EventContext instances with the methods defined:
+The top-level <tt>helpers</tt> method
helpers do
def bar(name)
@@ -205,27 +205,27 @@ EventContext instances with the methods defined:
== Filters
-These are run in Sinatra::EventContext before every event.
+Before filters are evaluated in request context before routing is performed:
before do
- .. this code will run before each event ..
+ @note = 'Hi!'
+ request.path_info = '/foo/bar/baz'
+ end
+
+ get '/foo/*' do
+ @note #=> 'Hi!'
+ params[:splat] #=> 'bar/baz'
end
+Before filters can modify the request and response; instance variables are
+available to routes.
+
== Halt!
To immediately stop a request during a before filter or event use:
throw :halt
-Set the body to the result of a helper method
-
- throw :halt, :helper_method
-
-Set the body to the result of a helper method after sending it parameters from
-the local scope
-
- throw :halt, [:helper_method, foo, bar]
-
Set the body to a simple string
throw :halt, 'this will be the body'
@@ -234,30 +234,11 @@ Set status then the body
throw :halt, [401, 'go away!']
-Set the status then call a helper method with params from local scope
-
- throw :halt, [401, [:helper_method, foo, bar]]
-
Run a proc inside the Sinatra::EventContext instance and set the body to the
result
throw :halt, lambda { puts 'In a proc!'; 'I just wrote to $stdout!' }
-Create you own to_result
-
- class MyResultObject
- def to_result(event_context, *args)
- event_context.body = 'This will be the body!
- end
- end
-
- get '/' do
- throw :halt, MyResultObject.new
- end
-
-Get the gist? If you want more fun with this then checkout <tt>to_result</tt>
-on Array, Symbol, Fixnum, NilClass.
-
== Configuration and Reloading
Sinatra supports multiple environments and reloading. Reloading happens
View
@@ -8,13 +8,19 @@ task :default => :test
desc 'Run specs with story style output'
task :spec do
- sh 'specrb --specdox -Ilib:test test/*_test.rb'
+ pattern = ENV['TEST'] || '.*'
+ sh "specrb --testcase '#{pattern}' --specdox -Ilib:test test/*_test.rb"
end
desc 'Run specs with unit test style output'
-task :test => FileList['test/*_test.rb'] do |t|
- suite = t.prerequisites.map{|f| "-r#{f.chomp('.rb')}"}.join(' ')
- sh "ruby -Ilib:test #{suite} -e ''", :verbose => false
+task :test do |t|
+ sh "specrb -Ilib:test test/*_test.rb"
+end
+
+desc 'Run compatibility specs'
+task :compat do |t|
+ pattern = ENV['TEST'] || '.*'
+ sh "specrb --testcase '#{pattern}' -Ilib:test compat/*_test.rb"
end
# PACKAGING ============================================================
@@ -141,11 +147,18 @@ task 'doc:book' => ['book/output/sinatra-book.html']
# Gemspec Helpers ====================================================
+def source_version
+ line = File.read('lib/sinatra/base.rb')[/^\s*VERSION = .*/]
+ line.match(/.*VERSION = '(.*)'/)[1]
+end
+
file 'sinatra.gemspec' => FileList['{lib,test,images}/**','Rakefile'] do |f|
# read spec file and split out manifest section
spec = File.read(f.name)
- parts = spec.split(" # = MANIFEST =\n")
- fail 'bad spec' if parts.length != 3
+ head, manifest, tail = spec.split(" # = MANIFEST =\n")
+ # replace version and date
+ head.sub!(/\.version = '.*'/, ".version = '#{source_version}'")
+ head.sub!(/\.date = '.*'/, ".date = '#{Date.today.to_s}'")
# determine file list from git ls-files
files = `git ls-files`.
split("\n").
@@ -155,8 +168,8 @@ file 'sinatra.gemspec' => FileList['{lib,test,images}/**','Rakefile'] do |f|
map{ |file| " #{file}" }.
join("\n")
# piece file back together and write...
- parts[1] = " s.files = %w[\n#{files}\n ]\n"
- spec = parts.join(" # = MANIFEST =\n")
+ manifest = " s.files = %w[\n#{files}\n ]\n"
+ spec = [head,manifest,tail].join(" # = MANIFEST =\n")
File.open(f.name, 'w') { |io| io.write(spec) }
puts "updated #{f.name}"
end
@@ -108,7 +108,9 @@
end
specify "renders a body with a redirect" do
- Sinatra::EventContext.any_instance.expects(:foo).returns('blah')
+ helpers do
+ def foo ; 'blah' ; end
+ end
get "/" do
redirect 'foo', :foo
end
@@ -130,13 +132,10 @@
end
specify "stop sets content and ends event" do
-
- Sinatra::EventContext.any_instance.expects(:foo).never
-
get '/set_body' do
stop 'Hello!'
stop 'World!'
- foo
+ fail 'stop should have halted'
end
get_it '/set_body'
@@ -146,8 +145,13 @@
end
- specify "should set status then call helper with a var" do
- Sinatra::EventContext.any_instance.expects(:foo).once.with(1).returns('bah!')
+ # Deprecated. WTF was going on here? What's the 1 in [:foo, 1] do?
+ xspecify "should set status then call helper with a var" do
+ helpers do
+ def foo
+ 'bah!'
+ end
+ end
get '/set_body' do
stop [404, [:foo, 1]]
@@ -252,8 +256,6 @@
assert_equal 'puted', body
end
- # Some Ajax libraries downcase the _method parameter value. Make
- # sure we can handle that.
specify "rewrites POSTs with lowercase _method param to PUT" do
put '/' do
'puted'
@@ -262,7 +264,6 @@
body.should.equal 'puted'
end
- # Ignore any _method parameters specified in GET requests or on the query string in POST requests.
specify "does not rewrite GETs with _method param to PUT" do
get '/' do
'getted'
@@ -16,7 +16,8 @@ def each
Sinatra.application = nil
end
- specify "returns what's at the end" do
+ # Deprecated. The lookup method is no longer used.
+ xspecify "returns what's at the end" do
block = Proc.new { 'Hello' }
get '/', &block
@@ -31,7 +32,8 @@ def each
result.block.should.be block
end
- specify "takes params in path" do
+ # Deprecated. The lookup method is no longer used.
+ xspecify "takes params in path" do
block = Proc.new { 'Hello' }
get '/:foo', &block
@@ -83,7 +85,8 @@ def each
end
- specify "the body set if set before the last" do
+ # Deprecated. The body method no longer halts.
+ xspecify "the body set if set before the last" do
get '/' do
body 'Blake'
@@ -142,14 +145,16 @@ def each
context "Default Application Configuration" do
- specify "includes 404 and 500 error handlers" do
+ # Sinatra::ServerError is no longer used
+ xspecify "includes 404 and 500 error handlers" do
Sinatra.application.errors.should.include(Sinatra::ServerError)
Sinatra.application.errors[Sinatra::ServerError].should.not.be.nil
Sinatra.application.errors.should.include(Sinatra::NotFound)
Sinatra.application.errors[Sinatra::NotFound].should.not.be.nil
end
- specify "includes Static event" do
+ # Deprecated. No such thing as a Static event anymore.
+ xspecify "includes Static event" do
assert Sinatra.application.events[:get].any? { |e| Sinatra::Static === e }
end
Oops, something went wrong.

0 comments on commit 8b8de75

Please sign in to comment.