getting mare advanced with to_result

This commit is contained in:
Blake Mizerany 2008-03-24 17:35:42 -07:00
parent df800b5c26
commit 1f204991c3
3 changed files with 27 additions and 8 deletions

View File

@ -81,6 +81,10 @@ This is more ideal for rendering templates as partials from within templates
This works like Haml except you use <tt>erb</tt> instead of <tt>haml</tt> This works like Haml except you use <tt>erb</tt> instead of <tt>haml</tt>
=== Builder
See Sinatra::Builder
= Helpers = Helpers
It is ill-advised to create helpers on (main). Use the handy <tt>helpers</tt> to install helper methods on Sinatra::EventContext for use inside events and templates. It is ill-advised to create helpers on (main). Use the handy <tt>helpers</tt> to install helper methods on Sinatra::EventContext for use inside events and templates.

View File

@ -205,8 +205,8 @@ module Sinatra
@path, @options = path, options @path, @options = path, options
end end
def to_result(cx) def to_result(cx, *args)
cx.body = self self
end end
def each def each
@ -545,8 +545,8 @@ module Sinatra
@params ||= @route_params.merge(@request.params).symbolize_keys @params ||= @route_params.merge(@request.params).symbolize_keys
end end
def stop(content) def stop(*args)
throw :halt, content throw :halt, args
end end
def complete(returned) def complete(returned)
@ -934,12 +934,14 @@ end
class Proc class Proc
def to_result(cx, *args) def to_result(cx, *args)
cx.instance_eval(&self) cx.instance_eval(&self)
args.shift.to_result(cx, *args)
end end
end end
class String class String
def to_result(cx, *args) def to_result(cx, *args)
cx.body = self args.shift.to_result(cx, *args)
self
end end
end end
@ -958,14 +960,13 @@ end
class Fixnum class Fixnum
def to_result(cx, *args) def to_result(cx, *args)
cx.status self cx.status self
cx.body args.first args.shift.to_result(cx, *args)
end end
end end
class NilClass class NilClass
def to_result(cx, *args) def to_result(cx, *args)
cx.body = '' ''
# log warning here
end end
end end

View File

@ -72,6 +72,20 @@ context "Sinatra" do
body.should.equal 'Hello!' body.should.equal 'Hello!'
end end
specify "should set status then call helper with a var" do
Sinatra::EventContext.any_instance.expects(:foo).once.with(1).returns('bah!')
get '/set_body' do
stop [404, [:foo, 1]]
end
get_it '/set_body'
should.be.not_found
body.should.equal 'bah!'
end
specify "delegates HEAD requests to GET handlers" do specify "delegates HEAD requests to GET handlers" do
get '/invisible' do get '/invisible' do