From b17f5f7008d6caca67ad405180c6359d8f624cf5 Mon Sep 17 00:00:00 2001 From: Blake Mizerany Date: Tue, 27 Nov 2007 20:16:12 -0800 Subject: [PATCH] simple event lookup --- lib/sinatra.rb | 34 ++++++++++++++++++++++++++++++++++ test/events_test.rb | 21 +++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 lib/sinatra.rb create mode 100644 test/events_test.rb diff --git a/lib/sinatra.rb b/lib/sinatra.rb new file mode 100644 index 0000000..d6ba68a --- /dev/null +++ b/lib/sinatra.rb @@ -0,0 +1,34 @@ + +module Sinatra + + class Event + + attr_reader :path, :block + + def initialize(path, &b) + @path = path + @block = b + end + + end + + class Application + + attr_reader :events + + def initialize + @events = Hash.new { |hash, key| hash[key] = [] } + end + + def define_event(method, path, &b) + events[method] << event = Event.new(path, &b) + event + end + + def lookup(method, path) + events[method].find { |e| e.path == path } + end + + end + +end \ No newline at end of file diff --git a/test/events_test.rb b/test/events_test.rb new file mode 100644 index 0000000..451dc76 --- /dev/null +++ b/test/events_test.rb @@ -0,0 +1,21 @@ +require File.dirname(__FILE__) + '/../lib/sinatra' + +require 'rubygems' +require 'test/spec' + +context "Simple Events" do + + specify "return what's at the end" do + + application = Sinatra::Application.new + + route = application.define_event(:get, '/') do + 'Hello' + end + + result = application.lookup(:get, '/') + + result.should.equal route + end + +end -- 2.11.4.GIT