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>
|
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.
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -73,6 +73,20 @@ context "Sinatra" do
|
||||||
|
|
||||||
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
|
||||||
"I am invisible to the world"
|
"I am invisible to the world"
|
||||||
|
|
Loading…
Reference in a new issue