Skip to content

Commit

Permalink
Matching routes and updating params; added :default routes group; add…
Browse files Browse the repository at this point in the history
…ed :any verb
  • Loading branch information
Mike Dvorkin committed Jan 14, 2012
1 parent fedd0db commit 6912cb6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
15 changes: 9 additions & 6 deletions lib/dio/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Controller

def initialize(app)
@request, @response, @params = app.request, app.response, app.params
self.class.routes(:default)
self.class.routes(:restful) unless routes?
end

Expand All @@ -32,10 +33,10 @@ def router

def route!
puts "route!(#{params.inspect})"
# method = router.match(@request, action)
# puts "router match => #{method.inspect}"
# ap @request.params
# __send__(method || action)
action = router.match(request, params)
puts "router match => #{action.inspect}"
ap params
__send__(action)
end

class << self
Expand Down Expand Up @@ -73,7 +74,7 @@ def anonymous_routes(&block)
@router ||= begin
router = Dio::Router.new
self.class.instance_eval do
[ :get, :post, :put, :delete ].each do |verb|
[ :get, :post, :put, :delete, :any ].each do |verb|
define_method verb do |rule|
pattern, action = rule.to_a.flatten
router.__send__(verb, pattern, action)
Expand All @@ -87,7 +88,9 @@ def anonymous_routes(&block)

#--------------------------------------------------------------------------
def named_routes(group, scope = {})
if group == :restful
if group == :default
any "/:action?/?:id?" => :dynamic
elsif group == :restful
only = scope[:only] ? Array(scope[:only]) : [ :index, :new, :create, :show, :edit, :update, :destroy ]
except = scope[:except] ? Array(scope[:except]) : []

Expand Down
38 changes: 27 additions & 11 deletions lib/dio/router.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require 'rubygems'
require 'awesome_print'
require "uri"

module Dio
class Router
Expand All @@ -10,15 +9,15 @@ def initialize
end

#--------------------------------------------------------------------------
[ :get, :post, :put, :delete ].each do |verb|
[ :get, :post, :put, :delete, :any ].each do |verb|
define_method verb do |pattern, action|
keys = []
unless pattern.is_a?(Regexp)
pattern = pattern.gsub(/:(\w+)|\*/) do # Match all named parameters and '*'s.
keys << ($1 || :wildcard) # Save named parameter or :wildcard for '*' match.
$1 ? "([^/?#]+)" : "(.+?)" # Replace named parameter or '*' with appropriate matchers.
end
pattern = /^#{pattern}/
pattern = /^#{pattern}$/
end
@rules[verb] << { :pattern => pattern, :keys => keys, :action => action }
ap '-------------------'
Expand All @@ -27,15 +26,32 @@ def initialize
end
end

# Get an array of { :pattern, :keys, :action } hashes for given request
# method, find matching pattern, update params keys, and return the action.
#--------------------------------------------------------------------------
def match(request, action)
routing_table = @rules[request.request_method]
routing_table.each do |path, method|
# Causes undefined method `params='
# request.params = { :controller => :test, :action => :cancel, :id => 123 }
return method if path =~ /^\/#{action}/
def match(request, params)
path = request.path_info.sub(/^\/\w+/, "") # Remove controller part from the path.
path = "/" if path.empty? # Remaining portion should at least be "/".
rules = @rules[request.request_method.downcase.to_sym] # Get the rules array for a given verb.
rules += @rules[:any] # Append the rules for any type of request.
rules.each do |rule|
if path =~ rule[:pattern]
if $~.captures.any?
captures = $~.captures.map { |c| URI.decode(c) if c }
params.merge!(:captures => captures)
rule[:keys].zip(captures) do |key, value|
next unless value
if key != :wildcard
params[key.to_sym] = value
else
(params[key] ||= []) << value
end
end
end
return rule[:action]
end
end
nil
:not_found
end
end
end
12 changes: 9 additions & 3 deletions samples/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ class Test < Dio::Controller
# routes :restful, :only => [:index, :new]

routes do
get "/:action/:id/*" => :index
get "/list" => :list
get "/cancel/:id" => :cancel
get "/:action/*/:id/*" => :index
get "/list" => :list
get "/cancel/:id" => :cancel

# get "/" => :index
# get "/new" => :new
# post "/" => :create
Expand All @@ -29,4 +30,9 @@ def cancel
puts "test/cancel"
ap params
end

def dynamic
puts "test/dynamic"
ap params
end
end

0 comments on commit 6912cb6

Please sign in to comment.