Class: Opi::Router
- Inherits:
-
Object
- Object
- Opi::Router
- Defined in:
- lib/opi/router.rb
Constant Summary collapse
- WILDCARD_PATTERN =
/\/\*/
- NAMED_SEGMENTS_PATTERN =
/\/:([^$\/]+)/
- NAMED_SEGMENTS_REPLACEMENT_PATTERN =
/\/:([^$\/]+)/
Instance Attribute Summary collapse
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
-
#initialize(root) ⇒ Router
constructor
A new instance of Router.
-
#route(method, path) ⇒ Object
def route(method, path, options={}, block) # TODO: remove&replace existing routes (on reload) router.routes.unshift(=> method, :path => path, :options => options, :block => block) end.
Constructor Details
Instance Attribute Details
#routes ⇒ Object
Returns the value of attribute routes.
3 4 5 |
# File 'lib/opi/router.rb', line 3 def routes @routes end |
Instance Method Details
#route(method, path) ⇒ Object
def route(method, path, options={}, block)
# TODO: remove&replace existing routes (on reload)
router.routes.unshift({:method => method, :path => path, :options => , :block => block})
end
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# 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 |