1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
This commit is contained in:
Blake Mizerany 2007-11-21 01:42:22 -08:00
parent b62130824d
commit 03626a1ec4

65
lib/sinatra2.rb Normal file
View file

@ -0,0 +1,65 @@
require "rubygems"
require "rack"
class String
def to_param
URI.escape(self)
end
def from_param
URI.unescape(self)
end
end
class Symbol
def to_proc
Proc.new { |*args| args.shift.__send__(self, *args) }
end
end
class Array
def to_hash
self.inject({}) { |h, (k, v)| h[k] = v; h }
end
end
module Sinatra
extend self
def request_types
@request_types ||= %w(GET PUT POST DELETE)
end
def events
@events ||= Hash.new do |hash, key|
hash[key] = [] if request_types.include?(key)
end
end
class Route
URI_CHAR = '[^/?:,&#]'.freeze unless defined?(URI_CHAR)
PARAM = /:(#{URI_CHAR}+)/.freeze unless defined?(PARAM)
def initialize(path, &b)
@path, @block = path, b
@param_keys = []
regex = path.to_s.gsub(PARAM) do
@param_keys << $1.intern
"(#{URI_CHAR}+)"
end
@pattern = /^#{regex}$/
@struct = Struct.new(:block, :params)
end
def match(path)
return nil unless path =~ @pattern
params = @param_keys.zip($~.captures.map(&:from_param)).to_hash
@struct.new(@block, params)
end
end
end