mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
getting mare advanced with to_result
This commit is contained in:
parent
df800b5c26
commit
1f204991c3
3 changed files with 27 additions and 8 deletions
|
@ -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>
|
||||
|
||||
=== Builder
|
||||
|
||||
See Sinatra::Builder
|
||||
|
||||
= 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.
|
||||
|
|
|
@ -205,8 +205,8 @@ module Sinatra
|
|||
@path, @options = path, options
|
||||
end
|
||||
|
||||
def to_result(cx)
|
||||
cx.body = self
|
||||
def to_result(cx, *args)
|
||||
self
|
||||
end
|
||||
|
||||
def each
|
||||
|
@ -545,8 +545,8 @@ module Sinatra
|
|||
@params ||= @route_params.merge(@request.params).symbolize_keys
|
||||
end
|
||||
|
||||
def stop(content)
|
||||
throw :halt, content
|
||||
def stop(*args)
|
||||
throw :halt, args
|
||||
end
|
||||
|
||||
def complete(returned)
|
||||
|
@ -934,12 +934,14 @@ end
|
|||
class Proc
|
||||
def to_result(cx, *args)
|
||||
cx.instance_eval(&self)
|
||||
args.shift.to_result(cx, *args)
|
||||
end
|
||||
end
|
||||
|
||||
class String
|
||||
def to_result(cx, *args)
|
||||
cx.body = self
|
||||
args.shift.to_result(cx, *args)
|
||||
self
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -958,14 +960,13 @@ end
|
|||
class Fixnum
|
||||
def to_result(cx, *args)
|
||||
cx.status self
|
||||
cx.body args.first
|
||||
args.shift.to_result(cx, *args)
|
||||
end
|
||||
end
|
||||
|
||||
class NilClass
|
||||
def to_result(cx, *args)
|
||||
cx.body = ''
|
||||
# log warning here
|
||||
''
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -72,6 +72,20 @@ context "Sinatra" do
|
|||
body.should.equal 'Hello!'
|
||||
|
||||
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
|
||||
get '/invisible' do
|
||||
|
|
Loading…
Reference in a new issue