Skip to content

Commit

Permalink
rafactored the regex related code in the mapper class
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoddaka committed Jun 7, 2011
1 parent 72cca79 commit 2e07c71
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -49,6 +49,9 @@ def constraint_args(constraint, request)

class Mapping #:nodoc:
IGNORE_OPTIONS = [:to, :as, :via, :on, :constraints, :defaults, :only, :except, :anchor, :shallow, :shallow_path, :shallow_prefix]
ANCHOR_CHARACTERS_REGEX = %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
SHORTHAND_REGEX = %r{^/[\w\/]+$}
WILDCARD_PATH = %r{\*([^\/]+)$}

def initialize(set, scope, path, options)
@set, @scope = set, scope
Expand Down Expand Up @@ -77,18 +80,18 @@ def normalize_options!
# segment_keys.include?(k.to_s) || k == :controller
next unless Regexp === requirement && !constraints[name]

if requirement.source =~ %r{\A(\\A|\^)|(\\Z|\\z|\$)\Z}
if requirement.source =~ ANCHOR_CHARACTERS_REGEX
raise ArgumentError, "Regexp anchor characters are not allowed in routing requirements: #{requirement.inspect}"
end
if requirement.multiline?
raise ArgumentError, "Regexp multiline option not allowed in routing requirements: #{requirement.inspect}"
end
end
end
end

# match "account/overview"
def using_match_shorthand?(path, options)
path && options.except(:via, :anchor, :to, :as).empty? && path =~ %r{^/[\w\/]+$}
path && options.except(:via, :anchor, :to, :as).empty? && path =~ SHORTHAND_REGEX
end

def normalize_path(path)
Expand All @@ -107,7 +110,7 @@ def normalize_path(path)

# Add a constraint for wildcard route to make it non-greedy and match the
# optional format part of the route by default
if path.match(/\*([^\/]+)$/) && @options[:format] != false
if path.match(WILDCARD_PATH) && @options[:format] != false
@options.reverse_merge!(:"#{$1}" => /.+?/)
end

Expand Down

0 comments on commit 2e07c71

Please sign in to comment.