From 414c80a324b4b3adc1017f42ee00425513b5d22f Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Tue, 27 Nov 2007 23:24:13 -0800 Subject: [PATCH] evaluate in context --- lib/sinatra.rb | 6 +++++- test/application_test.rb | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/sinatra.rb b/lib/sinatra.rb index 5213306..2037f45 100644 --- a/lib/sinatra.rb +++ b/lib/sinatra.rb @@ -31,6 +31,9 @@ module Sinatra end + class EventContext + end + class Application attr_reader :events @@ -50,7 +53,8 @@ module Sinatra def call(env) return [404, {}, 'Not Found'] unless event = lookup(env) - [200, {}, event.block.call] + result = EventContext.new.instance_eval(&event.block) + [200, {}, result] end end diff --git a/test/application_test.rb b/test/application_test.rb index 644ca46..9fe4633 100644 --- a/test/application_test.rb +++ b/test/application_test.rb @@ -62,4 +62,21 @@ context "Calling an app" do result.body.should.equal 'Hello World' end + specify "evaluates events in a clean context" do + Sinatra::EventContext.class_eval do + def foo + 'foo' + end + end + + @app.define_event(:get, '/foo') do + foo + end + + request = Rack::MockRequest.new(@app) + result = request.get('/foo') + result.should.be.ok + result.body.should.equal 'foo' + end + end -- 2.11.4.GIT