fixes for Tilt 1.3

This commit is contained in:
Konstantin Haase 2011-04-26 22:19:03 +02:00
parent 6207962fc1
commit 5e1375a5cc
6 changed files with 32 additions and 25 deletions

View File

@ -7,19 +7,20 @@ module Sinatra
include Sinatra::EngineTracking include Sinatra::EngineTracking
DUMMIES = { DUMMIES = {
Tilt::HamlTemplate => "!= capture_haml(*args, &block)", :haml => "!= capture_haml(*args, &block)",
Tilt::ERBTemplate => "<% @capture = yield(*args) %>", :erb => "<% @capture = yield(*args) %>",
Tilt::ErubisTemplate => "<%= yield(*args) %>", :slim => "== yield(*args)"
:slim => "== yield(*args)"
} }
DUMMIES[:erubis] = DUMMIES[:erb]
def capture(*args, &block) def capture(*args, &block)
@capture = nil @capture = nil
if current_engine == :ruby if current_engine == :ruby
result = block[*args] result = block[*args]
else else
clean_up = eval '_buf, @_buf_was = "", _buf if defined?(_buf)', block.binding clean_up = eval '_buf, @_buf_was = "", _buf if defined?(_buf)', block.binding
dummy = DUMMIES[Tilt[current_engine]] || DUMMIES.fetch(current_engine) dummy = DUMMIES.fetch(current_engine)
options = { :layout => false, :locals => {:args => args, :block => block }} options = { :layout => false, :locals => {:args => args, :block => block }}
result = render(current_engine, dummy, options, &block) result = render(current_engine, dummy, options, &block)
end end

View File

@ -8,6 +8,7 @@ module Sinatra
# Sinatra::Application by default. # Sinatra::Application by default.
module Common module Common
register :ConfigFile register :ConfigFile
register :CSRF
register :Namespace register :Namespace
register :RespondWith register :RespondWith

View File

@ -167,7 +167,9 @@ module Sinatra
settings.template_engines[ext].each { |e| possible << [e, name] } settings.template_engines[ext].each { |e| possible << [e, name] }
end end
possible.each do |engine, template| possible.each do |engine, template|
find_template(settings.views, template, Tilt[engine]) do |file| # not exactly like Tilt[engine], but does not trigger a require
klass = Tilt.mappings[Tilt.normalize(engine)].first
find_template(settings.views, template, klass) do |file|
next unless File.exist? file next unless File.exist? file
return settings.rendering_method(engine) << template.to_sym return settings.rendering_method(engine) << template.to_sym
end end

View File

@ -94,8 +94,9 @@ Gem::Specification.new do |s|
"spec/spec_helper.rb" "spec/spec_helper.rb"
] ]
s.add_dependency "sinatra", "~> 1.2.2" s.add_dependency "sinatra", "~> 1.2.3"
s.add_dependency "backports", ">= 2.0" s.add_dependency "backports", ">= 2.0"
s.add_dependency "tilt", "~> 1.3"
s.add_dependency "rack-test" s.add_dependency "rack-test"
s.add_development_dependency "rspec", "~> 2.3" s.add_development_dependency "rspec", "~> 2.3"

View File

@ -9,6 +9,8 @@ describe Sinatra::ContentFor do
end.new! end.new!
end end
Tilt.prefer Tilt::ERBTemplate
extend Forwardable extend Forwardable
def_delegators :subject, :content_for, :yield_content def_delegators :subject, :content_for, :yield_content
def render(engine, template) def render(engine, template)

View File

@ -87,20 +87,20 @@ describe Sinatra::Reloader do
end end
it "doesn't mess up the application" do it "doesn't mess up the application" do
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
it "knows when a route has been modified" do it "knows when a route has been modified" do
update_app_file(:routes => ['get("/foo") { "bar" }']) update_app_file(:routes => ['get("/foo") { "bar" }'])
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
it "knows when a route has been added" do it "knows when a route has been added" do
update_app_file( update_app_file(
:routes => ['get("/foo") { "foo" }', 'get("/bar") { "bar" }'] :routes => ['get("/foo") { "foo" }', 'get("/bar") { "bar" }']
) )
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
get('/bar').body.should == 'bar' get('/bar').body.strip.should == 'bar'
end end
it "knows when a route has been removed" do it "knows when a route has been removed" do
@ -113,7 +113,7 @@ describe Sinatra::Reloader do
:routes => ['get("/foo") { erb :foo }'], :routes => ['get("/foo") { erb :foo }'],
:inline_templates => { :foo => 'bar' } :inline_templates => { :foo => 'bar' }
) )
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
it "reloads inline templates in other file" do it "reloads inline templates in other file" do
@ -124,17 +124,17 @@ describe Sinatra::Reloader do
end end
require template_file_path require template_file_path
app_const.inline_templates= template_file_path app_const.inline_templates= template_file_path
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
update_file(template_file_path) do |f| update_file(template_file_path) do |f|
f.write "__END__\n\n@@foo\nbar" f.write "__END__\n\n@@foo\nbar"
end end
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
it "doesn't try to reload a removed file" do it "doesn't try to reload a removed file" do
update_app_file(:routes => ['get("/foo") { "i shall not be reloaded" }']) update_app_file(:routes => ['get("/foo") { "i shall not be reloaded" }'])
FileUtils.rm app_file_path FileUtils.rm app_file_path
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
end end
@ -149,20 +149,20 @@ describe Sinatra::Reloader do
it "allows to specify a file to stop from being reloaded" do it "allows to specify a file to stop from being reloaded" do
app_const.dont_reload app_file_path app_const.dont_reload app_file_path
update_app_file(:routes => ['get("/foo") { "bar" }']) update_app_file(:routes => ['get("/foo") { "bar" }'])
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
it "allows to specify a glob to stop matching files from being reloaded" do it "allows to specify a glob to stop matching files from being reloaded" do
app_const.dont_reload '**/*.rb' app_const.dont_reload '**/*.rb'
update_app_file(:routes => ['get("/foo") { "bar" }']) update_app_file(:routes => ['get("/foo") { "bar" }'])
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
it "doesn't interfere with other application's reloading policy" do it "doesn't interfere with other application's reloading policy" do
app_const.dont_reload '**/*.rb' app_const.dont_reload '**/*.rb'
setup_example_app(:routes => ['get("/foo") { "foo" }']) setup_example_app(:routes => ['get("/foo") { "foo" }'])
update_app_file(:routes => ['get("/foo") { "bar" }']) update_app_file(:routes => ['get("/foo") { "bar" }'])
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
end end
@ -179,19 +179,19 @@ describe Sinatra::Reloader do
end end
it "allows to specify a file to be reloaded" do it "allows to specify a file to be reloaded" do
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
update_file(@foo_path) do |f| update_file(@foo_path) do |f|
f.write 'class Foo; def self.foo() "bar" end end' f.write 'class Foo; def self.foo() "bar" end end'
end end
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
it "allows to specify glob to reaload matching files" do it "allows to specify glob to reaload matching files" do
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
update_file(@foo_path) do |f| update_file(@foo_path) do |f|
f.write 'class Foo; def self.foo() "bar" end end' f.write 'class Foo; def self.foo() "bar" end end'
end end
get('/foo').body.should == 'bar' get('/foo').body.strip.should == 'bar'
end end
it "doesn't try to reload a removed file" do it "doesn't try to reload a removed file" do
@ -199,17 +199,17 @@ describe Sinatra::Reloader do
f.write 'class Foo; def self.foo() "bar" end end' f.write 'class Foo; def self.foo() "bar" end end'
end end
FileUtils.rm @foo_path FileUtils.rm @foo_path
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
it "doesn't interfere with other application's reloading policy" do it "doesn't interfere with other application's reloading policy" do
app_const.also_reload '**/*.rb' app_const.also_reload '**/*.rb'
setup_example_app(:routes => ['get("/foo") { Foo.foo }']) setup_example_app(:routes => ['get("/foo") { Foo.foo }'])
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
update_file(@foo_path) do |f| update_file(@foo_path) do |f|
f.write 'class Foo; def self.foo() "bar" end end' f.write 'class Foo; def self.foo() "bar" end end'
end end
get('/foo').body.should == 'foo' get('/foo').body.strip.should == 'foo'
end end
end end
end end