diff --git a/lib/sinatra2.rb b/lib/sinatra2.rb new file mode 100644 index 00000000..af6e3c90 --- /dev/null +++ b/lib/sinatra2.rb @@ -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 + +