Class: RapidRunty::Router::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid_runty/router/matcher.rb

Overview

Matches passed in path with array of application paths

Instance Method Summary collapse

Instance Method Details

#controller_action(options) ⇒ Object



37
38
39
40
41
# File 'lib/rapid_runty/router/matcher.rb', line 37

def controller_action(options)
  Hash[
    %w(controller action).zip options.split('#')
  ]
end

#match(path, routes) ⇒ matching_route, ...

Defines the route that matches the path

Example:

RapidRunty::Router::Matcher.new.match("/foo", [{url: "/", to: "root#index"}, {url: "/bar", to: "bar#index"}, {url: "/foo", to: "foo#index"}]) #=> ["/foo", [], { controller: "foo", action: "index" }]
RapidRunty::Router::Matcher.new.match("/04/01/01", [{url: "/:day/:month/:year", to: "date#find"}]) #=> ["/", ["04", "01", "01"], { controller: "date", action: "find" }]

Currently only supporting “:to” options which defines the “controller#action”

Parameters:

  • path (String)

    path from ENV

  • application

    routes [Array] Array of Hash application defined routes

Returns:

  • (matching_route, matched_placeholders, matched_controller_action)

    array



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rapid_runty/router/matcher.rb', line 22

def match(path, routes)
  path = route_parser.new(path)
  url_patterns = routes.map { |route| route_parser.new(route) }

  url_patterns.each do |pattern|
    return [
      pattern.to_s,
      pattern.placeholders,
      controller_action(pattern.options)
    ] if pattern == path
  end

  [nil, {}, {}]
end