class Orbit::Router

Attributes

routes[RW]

Public Class Methods

add(verb, path, controller, action) click to toggle source
# File lib/orbit/router.rb, line 47
def self.add(verb, path, controller, action)
  instance.add(verb, path, controller, action)
end
match(verb, path) click to toggle source
# File lib/orbit/router.rb, line 12
def self.match(verb, path)
  instance.match(verb, path)
end
new() click to toggle source
# File lib/orbit/router.rb, line 6
def initialize
  instantiate

  @routes = {}
end
routes() click to toggle source
# File lib/orbit/router.rb, line 43
def self.routes
  instance.routes
end

Public Instance Methods

add(verb, path, controller, action) click to toggle source
# File lib/orbit/router.rb, line 51
def add(verb, path, controller, action)
  verb = verb.downcase.to_sym
  path = path.gsub('//', '/')

  routes[verb] ||= {}
  routes[verb][path] = { class: controller, action: action, route: Orbit::Config.route_class.new(path) }

  re_sort_routes(verb)
end
match(verb, path) click to toggle source
# File lib/orbit/router.rb, line 16
def match(verb, path)
  verb = verb.downcase.to_sym

  matching_route(verb, path)
end
matching_route(verb, path) click to toggle source
# File lib/orbit/router.rb, line 28
def matching_route(verb, path)
  routes_for_verb = routes_for_verb(verb)
  route = routes_for_verb.fetch(path, nil)

  unless route
    route = routes_for_verb.find do |pattern, options|
      options[:route].path.regex.match(path)
    end

    route = route.last if route
  end

  route
end
routes_for_verb(verb) click to toggle source
# File lib/orbit/router.rb, line 22
def routes_for_verb(verb)
  @routes_for_verb ||= {}

  @routes_for_verb[verb] ||= routes.fetch(verb, {})
end

Private Instance Methods

re_sort_routes(verb) click to toggle source
# File lib/orbit/router.rb, line 62
def re_sort_routes(verb)
  return unless routes[verb]

  root = []

  verb_routes = routes[verb].sort do |a, b|
    root.push(a) if a.first == "/"
    b.first <=> a.first
  end

  new_routes = {}

  (root + (verb_routes - root)).each { |key, value| new_routes[key] = value }

  routes[verb] = new_routes
end