Class: Fastr::Router
Overview
The router manages the routes for an application.
Routes are configured in the app/config/routes.rb file.
Example
router.draw do |route|
route.for '/:controller/:action'
route.for '/home/:action', :action => '[A-Za-z]+'
route.for '/test', :to => 'home#index'
end
Defined Under Namespace
Modules: Handler
Instance Attribute Summary collapse
-
#route_file ⇒ String
The full path to the routes file.
-
#routes ⇒ Array
The routes for this router.
Instance Method Summary collapse
-
#draw(&block) ⇒ Object
Evaluates the block in the context of the router.
-
#for(path, *args) ⇒ Object
Adds a route for a path and arguments.
-
#initialize(app) ⇒ Router
constructor
A new instance of Router.
-
#load ⇒ Object
Loads the routes from #route_file and evaluates it within the context of Router.
-
#match(env) ⇒ Hash
Searches the routes for a match given a Rack env.
Methods included from Log
create_logger, included, level=
Constructor Details
#initialize(app) ⇒ Router
Returns a new instance of Router.
26 27 28 29 30 31 |
# File 'lib/fastr/router.rb', line 26 def initialize(app) @app = app self.routes = [] self.route_file = "#{@app.app_path}/app/config/routes.rb" setup_watcher end |
Instance Attribute Details
#route_file ⇒ String
The full path to the routes file.
24 25 26 |
# File 'lib/fastr/router.rb', line 24 def route_file @route_file end |
#routes ⇒ Array
The routes for this router.
20 21 22 |
# File 'lib/fastr/router.rb', line 20 def routes @routes end |
Instance Method Details
#draw(&block) ⇒ Object
Evaluates the block in the context of the router.
107 108 109 |
# File 'lib/fastr/router.rb', line 107 def draw(&block) block.call(self) end |
#for(path, *args) ⇒ Object
Adds a route for a path and arguments.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/fastr/router.rb', line 92 def for(path, *args) arg = args[0] log.debug "Adding route, path: #{path}, args: #{args.inspect}" match = get_regex_for_route(path, arg) hash = get_to_hash(arg) route_info = {:regex => match[:regex], :args => arg, :vars => match[:vars], :hash => hash} # Add the HTTP methods for this route if they exist in options route_info[:methods] = arg[:methods] if not arg.nil? and arg.has_key? :methods self.routes.push(route_info) end |
#load ⇒ Object
Loads the routes from #route_file and evaluates it within the context of Fastr::Router.
80 81 82 83 84 85 86 |
# File 'lib/fastr/router.rb', line 80 def load log.debug "Loading routes from: #{self.route_file}" self.routes = [] file = File.open(self.route_file) @app.instance_eval(file.read) end |
#match(env) ⇒ Hash
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fastr/router.rb', line 51 def match(env) self.routes.each do |info| # If the route didn't specify method(s) to limit by, then all HTTP methods are valid. # If the route specified method(s), we check the request's HTTP method and validate # that it exists in that list. next unless info[:methods].nil? or info[:methods].include?(env["REQUEST_METHOD"].downcase.to_sym) match = env['PATH_INFO'].match(info[:regex]) # See if a route matches if not match.nil? # Map any parameters in our matched string vars = {} info[:vars].each_index do |i| var = info[:vars][i] vars[var] = match[i+1] end return {:ok => vars.merge!(info[:hash]) } end end {:error => :not_found} end |