Class: StrongRoutes::Matcher

Inherits:
Regexp
  • Object
show all
Defined in:
lib/strong_routes/matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(route) ⇒ Matcher

Returns a new instance of Matcher.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/strong_routes/matcher.rb', line 5

def initialize(route)
  if route.is_a?(Regexp)
    super
  else
    escaped_route = Regexp.escape(route)
    # Replace dynamic segments in the route with wildcards (e.g. /:foo/users/:id becomes /.*/users/.*)
    escaped_route.gsub!(/:\w+/, ".*")

    # (:?\?.*)*$ matches the end of the string OR a '?' followed by anything until the end of string
    # validation of query params is left to the app, but this at least prevents a route such as "/"
    # from matching "/foo" while still allowing query params
    super(/\A#{escaped_route}(:?\?.*)*$/i)
  end
end