From 174d9c8a25b80eef9b92dd4054579ed5788e81e1 Mon Sep 17 00:00:00 2001 From: Chris Schneider Date: Sun, 18 May 2008 11:18:42 -0600 Subject: [PATCH 1/5] Kernel.load $0 breaks when $0 is not the file with sinatra routes, add an :app_file option to define which file to reload --- lib/sinatra.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) mode change 100644 => 100755 lib/sinatra.rb diff --git a/lib/sinatra.rb b/lib/sinatra.rb old mode 100644 new mode 100755 index 1b4fdec6..58fe8541 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -848,7 +848,8 @@ module Sinatra :views => root + '/views', :public => root + '/public', :sessions => false, - :logging => true + :logging => true, + :app_file => $0 } end @@ -932,7 +933,7 @@ module Sinatra @reloading = true clearables.each(&:clear) load_default_events! - Kernel.load $0 + Kernel.load Sinatra.options.app_file @reloading = false Environment.setup! end From a766406d168db2714f7ab7b0bf7adaf7043943ff Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Mon, 19 May 2008 17:25:09 -0400 Subject: [PATCH 2/5] add doc on using Rack middleware to README --- README.rdoc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.rdoc b/README.rdoc index f1dc1013..d21cca3f 100644 --- a/README.rdoc +++ b/README.rdoc @@ -349,6 +349,30 @@ When using send_file or static files you may have mime types Sinatra doesn't und mime :foo, 'text/foo' += Using Rack Middleware + +Sinatra rides on Rack[http://rack.rubyforge.org/], a minimal standard interface for Ruby web frameworks. One of Rack's most interesting capabilities for application developers is support for "middleware" -- components that sit between the server and your application monitoring and/or manipulating the HTTP request/response to provide various types of common functionality. What's more, middleware is portable between web frameworks, so middleware components developed under, e.g., Merb, can be used with Sinatra and vice versa. + +Sinatra makes building Rack middleware pipelines a cinch via a top-level +use+ method: + + require 'sinatra' + require 'my_custom_middleware' + + use Rack::Lint + use MyCustomMiddleware + + get '/hello' do + 'Hello World' + end + +The semantics of +use+ are identical to those defined for the Rack::Builder[http://rack.rubyforge.org/doc/classes/Rack/Builder.html] DSL (most frequently used from rackup files). For example, the +use+ method accepts multiple/variable args as well as blocks: + + use Rack::Auth::Basic do |username, password| + username == 'admin' && password == 'secret' + end + +Rack is distributed with a variety of standard middleware for logging, debugging, URL routing, authentication, and session handling. Sinatra uses many of of these components automatically based on configuration so you typically don't have to +use+ them explicitly. + = Testing === Methods From 418db1ed7cecab3c8f07270c419cef89dc942cde Mon Sep 17 00:00:00 2001 From: Ryan Tomayko Date: Mon, 19 May 2008 14:06:58 -0400 Subject: [PATCH 3/5] minor rdoc formatting fixes --- lib/sinatra.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 39a47dc8..454140bc 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -498,8 +498,8 @@ module Sinatra # database queries, template rendering, complex logic) can dramatically # increase overall throughput with caching clients. # - # === See Also - # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19[RFC2616: ETag], + # ==== See Also + # {RFC2616: ETag}[http://w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.19], # ResponseHelpers#last_modified def entity_tag(value, strength=:strong) value = @@ -1090,8 +1090,8 @@ module Sinatra error NotFound, options, &b end - # Define a request filter. When +type+ is +:before+, execute the block - # in the context of each request before matching event handlers. + # Define a request filter. When type is :before, execute the + # block in the context of each request before matching event handlers. def filter(type, &b) filters[type] << b end From 1ba1ceb7f6af5564bcfcf12d04d274c3030e2f32 Mon Sep 17 00:00:00 2001 From: bmizerany Date: Tue, 20 May 2008 15:08:55 -0700 Subject: [PATCH 4/5] added 'set' to FORWARD_METHODS --- lib/sinatra.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) mode change 100755 => 100644 lib/sinatra.rb diff --git a/lib/sinatra.rb b/lib/sinatra.rb old mode 100755 new mode 100644 index d5cc644a..68b7e97b --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -849,7 +849,7 @@ module Sinatra # (Sinatra::application). FORWARD_METHODS = %w[ get put post delete head template layout before error not_found - configures configure set_options set_option enable disable use + configures configure set set_options set_option enable disable use ] # Create a new Application with a default configuration taken From c2827e3331dd08d890e52a9431810d43d8ad03d2 Mon Sep 17 00:00:00 2001 From: Chris Schneider Date: Tue, 27 May 2008 20:09:45 -0600 Subject: [PATCH 5/5] Add :locals option for erb. General approach and some of the code was borrowed from rails (compilable.rb) --- lib/sinatra.rb | 14 ++++++++++++-- test/erb_test.rb | 20 ++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 68b7e97b..f3544823 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -576,9 +576,19 @@ module Sinatra private def render_erb(content, options = {}) - ::ERB.new(content).result(binding) + locals_opt = options.delete(:locals) || {} + + locals_code = "" + locals_hash = {} + locals_opt.each do |key, value| + locals_code << "#{key} = locals_hash[:#{key}]\n" + locals_hash[:"#{key}"] = value + end + + body = ::ERB.new(content).src + eval("#{locals_code}#{body}", binding) end - + end module Haml diff --git a/test/erb_test.rb b/test/erb_test.rb index 81e79eba..36c1e239 100644 --- a/test/erb_test.rb +++ b/test/erb_test.rb @@ -23,6 +23,26 @@ context "Erb" do body.should == '2' end + + specify "should take an options hash with :locals set with a string" do + get '/locals' do + erb '<%= foo %>', :locals => {:foo => "Bar"} + end + + get_it '/locals' + should.be.ok + body.should == 'Bar' + end + + specify "should take an options hash with :locals set with a complex object" do + get '/locals-complex' do + erb '<%= foo[0] %>', :locals => {:foo => ["foo", "bar", "baz"]} + end + + get_it '/locals-complex' + should.be.ok + body.should == 'foo' + end end context "with layouts" do