1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00

routes determined

This commit is contained in:
Blake Mizerany 2007-11-21 02:18:27 -08:00
parent d599c45f5f
commit f6ca4bdef9
4 changed files with 39 additions and 6 deletions

View file

@ -21,26 +21,44 @@ class Array
def to_hash
self.inject({}) { |h, (k, v)| h[k] = v; h }
end
def to_proc
Proc.new { |*args| args.shift.send(self[0], args + self[1..-1]) }
end
end
module Enumerable
def eject(&block)
find { |e| result = block[e] and break result }
end
end
module Sinatra
extend self
def request_types
@request_types ||= %w(GET PUT POST DELETE)
@request_types ||= [:get, :put, :post, :delete]
end
def events
@events ||= Hash.new do |hash, key|
def routes
@routes ||= Hash.new do |hash, key|
hash[key] = [] if request_types.include?(key)
end
end
def determine_event(verb, path)
routes[verb].eject { |r| r.match(path) }
end
class Route
URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
attr_reader :block, :path
def initialize(path, &b)
@path, @block = path, b
@param_keys = []

14
test/dispatching_test.rb Normal file
View file

@ -0,0 +1,14 @@
require File.dirname(__FILE__) + '/helper'
context "Dispatching" do
specify "should return the correct block" do
Sinatra.routes[:get] << r = Sinatra::Route.new('/') do
'main'
end
result = Sinatra.determine_event(:get, '/')
result.block.should.be r.block
end
end

3
test/helper.rb Normal file
View file

@ -0,0 +1,3 @@
require File.dirname(__FILE__) + "/../lib/sinatra"
require 'test/spec'
require 'mocha'

View file

@ -1,6 +1,4 @@
require File.dirname(__FILE__) + "/../lib/sinatra"
require 'test/spec'
require 'mocha'
require File.dirname(__FILE__) + '/helper'
context "A Route in general" do