mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
simple event lookup
This commit is contained in:
parent
5e5f0c021a
commit
b17f5f7008
2 changed files with 55 additions and 0 deletions
34
lib/sinatra.rb
Normal file
34
lib/sinatra.rb
Normal file
|
@ -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
|
21
test/events_test.rb
Normal file
21
test/events_test.rb
Normal file
|
@ -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 a new issue