1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Pare down object creation during route building

This commit is contained in:
Jeremy Kemper 2008-11-10 17:41:07 -08:00
parent 335a315240
commit 5db9f9b3ad
2 changed files with 27 additions and 30 deletions

View file

@ -1,23 +1,16 @@
module ActionController module ActionController
module Routing module Routing
class RouteBuilder #:nodoc: class RouteBuilder #:nodoc:
attr_accessor :separators, :optional_separators attr_reader :separators, :optional_separators
attr_reader :separator_regexp, :nonseparator_regexp, :interval_regexp
def initialize def initialize
self.separators = Routing::SEPARATORS @separators = Routing::SEPARATORS
self.optional_separators = %w( / ) @optional_separators = %w( / )
end
def separator_pattern(inverted = false) @separator_regexp = /[#{Regexp.escape(separators.join)}]/
"[#{'^' if inverted}#{Regexp.escape(separators.join)}]" @nonseparator_regexp = /\A([^#{Regexp.escape(separators.join)}]+)/
end @interval_regexp = /(.*?)(#{separator_regexp}|$)/
def interval_regexp
Regexp.new "(.*?)(#{separators.source}|$)"
end
def multiline_regexp?(expression)
expression.options & Regexp::MULTILINE == Regexp::MULTILINE
end end
# Accepts a "route path" (a string defining a route), and returns the array # Accepts a "route path" (a string defining a route), and returns the array
@ -30,7 +23,7 @@ module ActionController
rest, segments = path, [] rest, segments = path, []
until rest.empty? until rest.empty?
segment, rest = segment_for rest segment, rest = segment_for(rest)
segments << segment segments << segment
end end
segments segments
@ -39,20 +32,20 @@ module ActionController
# A factory method that returns a new segment instance appropriate for the # A factory method that returns a new segment instance appropriate for the
# format of the given string. # format of the given string.
def segment_for(string) def segment_for(string)
segment = case string segment =
when /\A:(\w+)/ case string
key = $1.to_sym when /\A:(\w+)/
case key key = $1.to_sym
when :controller then ControllerSegment.new(key) key == :controller ? ControllerSegment.new(key) : DynamicSegment.new(key)
else DynamicSegment.new key when /\A\*(\w+)/
end PathSegment.new($1.to_sym, :optional => true)
when /\A\*(\w+)/ then PathSegment.new($1.to_sym, :optional => true) when /\A\?(.*?)\?/
when /\A\?(.*?)\?/ StaticSegment.new($1, :optional => true)
StaticSegment.new($1, :optional => true) when nonseparator_regexp
when /\A(#{separator_pattern(:inverted)}+)/ then StaticSegment.new($1) StaticSegment.new($1)
when Regexp.new(separator_pattern) then when separator_regexp
DividerSegment.new($&, :optional => (optional_separators.include? $&)) DividerSegment.new($&, :optional => optional_separators.include?($&))
end end
[segment, $~.post_match] [segment, $~.post_match]
end end
@ -98,7 +91,7 @@ module ActionController
if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z} if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}" raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
end end
if multiline_regexp?(requirement) if requirement.multiline?
raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}" raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
end end
segment.regexp = requirement segment.regexp = requirement

View file

@ -27,6 +27,10 @@ class Regexp #:nodoc:
Regexp.new("|#{source}").match('').captures.length Regexp.new("|#{source}").match('').captures.length
end end
def multiline?
options & MULTILINE == MULTILINE
end
class << self class << self
def optionalize(pattern) def optionalize(pattern)
case unoptionalize(pattern) case unoptionalize(pattern)