Class: SimpleRouter::Engines::SimpleEngine::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_router/engines/simple_engine.rb

Class Method Summary collapse

Class Method Details

.match(path, routes) ⇒ Object

Finds a route definition that matches a path

Arguments
  • path: actual path to match (e.g. ENV)

  • routes: array of ‘routes’, where each route is composed of [pattern, options]. If route isn’t an array, an empty options hash is assumed

Currently, this engine implementation ignores route options.

Returns

Array of two elements:

  • index 0: first matching route

  • index 1: array of values for the matched route’s variables (in the order they were specified in the route)

Examples
SimpleEngine.match('/foo', ['/', '/foo', '/bar/baz'])   #=> ['/foo', []]
SimpleEngine.match('/80/07/01', ['/:year/:month/:day']) #=> ['/foo', ['80', '07', '01']]


31
32
33
34
35
36
37
38
39
40
# File 'lib/simple_router/engines/simple_engine.rb', line 31

def self.match(path, routes)
  path     = Path.new(path)
  patterns = routes.map {|route| Pattern.new(Array(route).first) }

  patterns.each do |pattern|
    return [pattern.to_s, pattern.vars] if pattern == path
  end

  [nil, []]
end