mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
* silence warnings or reload
* simple responder * update examples
This commit is contained in:
parent
b215850cbd
commit
49da7fc4c7
8 changed files with 67 additions and 42 deletions
|
@ -2,16 +2,6 @@ $LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
|
|||
require 'sinatra'
|
||||
|
||||
get '/' do
|
||||
html {p "here, b"}
|
||||
format.html {p "here, b"}
|
||||
p "hi"
|
||||
end
|
||||
|
||||
get 'index' do
|
||||
html {body "in here!"}
|
||||
body do
|
||||
"pancakes"
|
||||
end
|
||||
end
|
||||
|
||||
get 'favicon' do
|
||||
end
|
||||
|
|
|
@ -1,32 +1,6 @@
|
|||
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../../lib/'
|
||||
require 'sinatra'
|
||||
|
||||
after_attend :log_fun_stuff
|
||||
|
||||
helpers do
|
||||
def log_fun_stuff
|
||||
logger.debug "THIS IS COOL!"
|
||||
end
|
||||
end
|
||||
|
||||
get '/' do
|
||||
body <<-HTML
|
||||
<form method="POST"><input type="text" name="name"/><input type="submit"></form>
|
||||
HTML
|
||||
end
|
||||
|
||||
get '/hello' do
|
||||
"Hello World!"
|
||||
end
|
||||
|
||||
post '/' do
|
||||
body "You entered #{params[:name]}"
|
||||
end
|
||||
|
||||
get '/erb' do
|
||||
erb :index
|
||||
end
|
||||
|
||||
get '/test' do
|
||||
erb "Hello <%= params[:name] %>"
|
||||
format.xml { body 'blake in xml' }
|
||||
end
|
||||
|
|
16
lib/sinatra/core_ext/kernel.rb
Normal file
16
lib/sinatra/core_ext/kernel.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
module Kernel
|
||||
# (Taken from ActiveSupport)
|
||||
# Sets $VERBOSE to nil for the duration of the block and back to its original value afterwards.
|
||||
#
|
||||
# silence_warnings do
|
||||
# value = noisy_call # no warning voiced
|
||||
# end
|
||||
#
|
||||
# noisy_call # warning voiced
|
||||
def silence_warnings
|
||||
old_verbose, $VERBOSE = $VERBOSE, nil
|
||||
yield
|
||||
ensure
|
||||
$VERBOSE = old_verbose
|
||||
end
|
||||
end
|
|
@ -5,9 +5,11 @@ module Sinatra
|
|||
extend self
|
||||
|
||||
def reload!
|
||||
EventManager.reset!
|
||||
load_files loaded_files
|
||||
load $0
|
||||
silence_warnings do
|
||||
EventManager.reset!
|
||||
load_files loaded_files
|
||||
load $0
|
||||
end
|
||||
end
|
||||
|
||||
def load_files(*files)
|
||||
|
|
|
@ -29,7 +29,7 @@ module Sinatra
|
|||
private
|
||||
|
||||
def extract_format(path)
|
||||
format = (path).split('.')[-1]
|
||||
format = File.extname(path)[1..-1]
|
||||
VALID_FORMATS.include?(format) ? format : 'html'
|
||||
end
|
||||
|
||||
|
|
3
vendor/responder/init.rb
vendored
Normal file
3
vendor/responder/init.rb
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
require File.dirname(__FILE__) + '/lib/responder'
|
||||
|
||||
Sinatra::EventContext.send(:include, Sinatra::Responder)
|
27
vendor/responder/lib/responder.rb
vendored
Normal file
27
vendor/responder/lib/responder.rb
vendored
Normal file
|
@ -0,0 +1,27 @@
|
|||
module Sinatra
|
||||
|
||||
module Responder
|
||||
|
||||
def self.included(parent)
|
||||
parent.send(:include, InstanceMethods)
|
||||
end
|
||||
|
||||
class ResponderContext
|
||||
def initialize(format)
|
||||
@format = format
|
||||
end
|
||||
|
||||
def method_missing(name, *args)
|
||||
yield if name.to_s == @format
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def format
|
||||
@responder_context ||= ResponderContext.new(params[:format])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
13
vendor/responder/test/responder_test.rb
vendored
Normal file
13
vendor/responder/test/responder_test.rb
vendored
Normal file
|
@ -0,0 +1,13 @@
|
|||
require File.dirname(__FILE__) + '/../../../test/helper'
|
||||
|
||||
context "A responder, by default" do
|
||||
specify "should default to html" do
|
||||
path = '/foo/test.xml'
|
||||
context = Sinatra::EventContext.new(stub(:params => { :format => 'xml' }))
|
||||
context.expects(:foo)
|
||||
context.instance_eval do
|
||||
format.xml { foo }
|
||||
format.html { bar }
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue