mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Changed the body method to accept blocks as well as strings. Also, added example calendar app to be built more in the future. Finally, added the respon_to block. View readme for example
This commit is contained in:
parent
3b491e00ab
commit
2da510039e
5 changed files with 104 additions and 1 deletions
17
examples/calendar/calendar.rb
Normal file
17
examples/calendar/calendar.rb
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
|
||||||
|
require 'sinatra'
|
||||||
|
|
||||||
|
get '/' do
|
||||||
|
html {p "here, b"}
|
||||||
|
p "hi"
|
||||||
|
end
|
||||||
|
|
||||||
|
get 'index' do
|
||||||
|
html {body "in here!"}
|
||||||
|
body do
|
||||||
|
"pancakes"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get 'favicon' do
|
||||||
|
end
|
|
@ -56,8 +56,9 @@ module Sinatra
|
||||||
@status || 200
|
@status || 200
|
||||||
end
|
end
|
||||||
|
|
||||||
def body(value = nil)
|
def body(value = nil, &block)
|
||||||
@body = value if value
|
@body = value if value
|
||||||
|
@body = block.call if block
|
||||||
@body || ''
|
@body || ''
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
17
vendor/respond_to/README
vendored
Normal file
17
vendor/respond_to/README
vendored
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
Respond_to
|
||||||
|
|
||||||
|
Allows for different methods to be called upon in the event context block
|
||||||
|
|
||||||
|
from
|
||||||
|
|
||||||
|
get 'index' do
|
||||||
|
body "hey"
|
||||||
|
end
|
||||||
|
|
||||||
|
to
|
||||||
|
|
||||||
|
get 'index' do
|
||||||
|
html {"hi"}
|
||||||
|
end
|
||||||
|
|
||||||
|
If there is a block with the extension, that will be used as the output, otherwise the body of the block will be used.
|
5
vendor/respond_to/init.rb
vendored
Normal file
5
vendor/respond_to/init.rb
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
require File.dirname(__FILE__) + '/lib/responder'
|
||||||
|
|
||||||
|
Sinatra::EventContext.send(:include, Sinatra::Responder)
|
||||||
|
Sinatra::Event.send(:include, Sinatra::EventResponder)
|
||||||
|
Sinatra::EventManager.send(:extend, Sinatra::DispatcherResponder)
|
63
vendor/respond_to/lib/responder.rb
vendored
Normal file
63
vendor/respond_to/lib/responder.rb
vendored
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
module Sinatra
|
||||||
|
module Responder
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
|
def method_missing(id, *args, &block)
|
||||||
|
if block
|
||||||
|
@responds_to[id] = block unless @responds_to.has_key? id
|
||||||
|
else
|
||||||
|
super
|
||||||
|
end
|
||||||
|
end
|
||||||
|
def initialize_with_responds_to(i)
|
||||||
|
initialize_without_responds_to(i)
|
||||||
|
@responds_to = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.included(base)
|
||||||
|
base.send :include, InstanceMethods
|
||||||
|
base.instance_eval do
|
||||||
|
alias_method :initialize_without_responds_to, :initialize
|
||||||
|
alias_method :initialize, :initialize_with_responds_to
|
||||||
|
end
|
||||||
|
attr_accessor :responds_to
|
||||||
|
end
|
||||||
|
end
|
||||||
|
module EventResponder
|
||||||
|
def self.included(base)
|
||||||
|
base.send :include, InstanceMethods
|
||||||
|
base.instance_eval do
|
||||||
|
alias_method :attend_without_respond_to, :attend
|
||||||
|
alias_method :attend, :attend_with_respond_to
|
||||||
|
end
|
||||||
|
end
|
||||||
|
module InstanceMethods
|
||||||
|
# Pulls the request info from the REQUEST_PATH
|
||||||
|
# it looks for the path extension
|
||||||
|
# For instance, index.html will respond to the html block
|
||||||
|
def respond_request(request)
|
||||||
|
request.env['REQUEST_PATH'].include?('.') ? request.env['REQUEST_PATH'].split('.')[-1].to_sym : :html
|
||||||
|
end
|
||||||
|
def attend_with_respond_to(request)
|
||||||
|
context = EventContext.new(request)
|
||||||
|
begin
|
||||||
|
context.instance_eval(&@block) if @block
|
||||||
|
context.responds_to[respond_request(request)].call if context.responds_to.has_key? respond_request(request)
|
||||||
|
rescue => e
|
||||||
|
context.error e
|
||||||
|
end
|
||||||
|
run_through_after_filters(context)
|
||||||
|
context
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module DispatcherResponder
|
||||||
|
def determine_event(verb, path)
|
||||||
|
EventManager.events.detect(method(:not_found)) do |e|
|
||||||
|
e.path =~ Regexp.new(path.split(".")[0..-2].join("|").gsub(/\//, '')) && e.verb == verb
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue