simple event lookup
This commit is contained in:
parent
5e5f0c021a
commit
b17f5f7008
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue