class Opi::Router
Constants
- NAMED_SEGMENTS_PATTERN
- NAMED_SEGMENTS_REPLACEMENT_PATTERN
- WILDCARD_PATTERN
Attributes
routes[RW]
Public Class Methods
new(root)
click to toggle source
# File lib/opi/router.rb, line 9 def initialize(root) @root = root @routes = [] compile_routes(@root) end
Public Instance Methods
route(method, path)
click to toggle source
def route(method, path, options={}, block)
# TODO: remove&replace existing routes (on reload) router.routes.unshift({:method => method, :path => path, :options => options, :block => block})
end
# File lib/opi/router.rb, line 20 def route(method, path) method_routes = self.routes.find_all{|x| x.method == method} method_routes.each do |route| if route.path =~ WILDCARD_PATTERN src = "\\A#{route.path.gsub('*','(.*)')}\\Z" if match = path.match(Regexp.new(src)) return [route, match[1].split('/')] end elsif route.path =~ NAMED_SEGMENTS_PATTERN src = "\\A#{route.path.gsub(NAMED_SEGMENTS_REPLACEMENT_PATTERN, '/(?<\1>[^$/]+)')}\\Z" if match = path.match(Regexp.new(src)) return [route, Hash[match.names.zip(match.captures)]] end elsif path == route.path return [route] end end nil end
Private Instance Methods
compile_routes(root)
click to toggle source
# File lib/opi/router.rb, line 41 def compile_routes(root) root.routes.each{|x| self.routes.unshift x} root.resources.each{|x| compile_routes(x)} end