Class: Fastr::Router
Defined Under Namespace
Modules: Handler
Instance Attribute Summary collapse
-
#route_file ⇒ Object
Returns the value of attribute route_file.
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
- #draw(&block) ⇒ Object
- #file_modified ⇒ Object
- #for(path, *args) ⇒ Object
-
#initialize(app) ⇒ Router
constructor
A new instance of Router.
- #load ⇒ Object
- #match(env) ⇒ Object
Methods included from Log
Constructor Details
#initialize(app) ⇒ Router
Returns a new instance of Router.
7 8 9 10 11 12 |
# File 'lib/fastr/router.rb', line 7 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 ⇒ Object
Returns the value of attribute route_file.
5 6 7 |
# File 'lib/fastr/router.rb', line 5 def route_file @route_file end |
#routes ⇒ Object
Returns the value of attribute routes.
5 6 7 |
# File 'lib/fastr/router.rb', line 5 def routes @routes end |
Instance Method Details
#draw(&block) ⇒ Object
54 55 56 |
# File 'lib/fastr/router.rb', line 54 def draw(&block) block.call(self) end |
#file_modified ⇒ Object
58 59 60 |
# File 'lib/fastr/router.rb', line 58 def file_modified puts "changed!" end |
#for(path, *args) ⇒ Object
44 45 46 47 48 49 50 51 52 |
# File 'lib/fastr/router.rb', line 44 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) self.routes.push({:regex => match[:regex], :args => arg, :vars => match[:vars], :hash => hash}) end |
#load ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/fastr/router.rb', line 36 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) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fastr/router.rb', line 14 def match(env) self.routes.each do |info| 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 |